Code Monkey home page Code Monkey logo

Comments (8)

curran avatar curran commented on August 11, 2024 3

@g-borgulya @codingisacopingstrategy See this related thread josephg/ShareJS#12

From there, here's an implementation of getSnapshotAtRevision by @luto . The algorithm is:

  • Fetch the current snapshot
  • Fetch the ops between the current snapshot and the desired snapshot
  • For each op in reverse order, invert it and apply it to the snapshot.

Here's a copy of the implementation (authored by @luto):

function getSnapshotAtRevision(docname, v, cb)
{
  var snapshot
    , ops = []

  async.waterfall(
    [
      // get latest revision
      function (cb)
      {
        model.getSnapshot(docname,
          function (err, _snapshot)
          {
            snapshot = _snapshot;
            cb(err);
          }
        );
      },
      // get ops that happend between `v` and `snapshot.v`
      function (cb)
      {
        if(v == snapshot.v)
          return cb();

        model.getOps(docname, v, snapshot.v,
          function (err, _ops)
          {
            ops = _ops;
            cb(err);
          }
        );
      },
      // invert and apply ops
      function (cb)
      {
        var json = sharejs.types.json;
        var content = snapshot.snapshot;
        var err = null;

        try
        {
          for (var i = ops.length - 1; i >= 0; i--) // reverse order
          {
            var op = ops[i].op;
            op = json.invert(op);
            content = json.apply(content, op);
          }
        }
        catch (_err)
        {
          err = _err;
        }

        cb(err, content);
      }
    ],
    cb
  );
}

from sharedb.

g-borgulya avatar g-borgulya commented on August 11, 2024

@codingisacopingstrategy do you have a workaround for this?

from sharedb.

g-borgulya avatar g-borgulya commented on August 11, 2024

Nice!! Thank you, @curran .

from sharedb.

curran avatar curran commented on August 11, 2024

@g-borgulya My pleasure! It would be amazing to integrate something like this into the project. One possible stumbling block is that I'm not sure all OT Types are invertable. Maybe there could be a variant that builds up the doc by replaying all ops in the forward direction from the beginning.

from sharedb.

curran avatar curran commented on August 11, 2024

I started packaging the above code into a library: https://github.com/curran/sharedb-snapshot/tree/master

from sharedb.

nickasd avatar nickasd commented on August 11, 2024

I agree that this would be a useful feature. I'm implementing a search and replace feature and in this case it would be useful being able to apply the replace operations on the snapshot version at the time of the search; applying the replace operations on the current version could not work if a change is applied in the meantime.

from sharedb.

luto avatar luto commented on August 11, 2024

@curran nice, thank you ;)

from sharedb.

alecgibson avatar alecgibson commented on August 11, 2024

This was implemented in #220

from sharedb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.