Skip to content

Commit

Permalink
Merge 38ff9c9 into 46c4aba
Browse files Browse the repository at this point in the history
  • Loading branch information
curran committed Sep 28, 2021
2 parents 46c4aba + 38ff9c9 commit 6d58093
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
10 changes: 10 additions & 0 deletions lib/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var logger = require('../logger');
var ShareDBError = require('../error');
var types = require('../types');
var util = require('../util');
var clone = util.clone;
var deepEqual = require('fast-deep-equal');

var ERROR_CODE = ShareDBError.CODES;
Expand Down Expand Up @@ -919,6 +920,15 @@ Doc.prototype.resume = function() {
this.flush();
};

// Create a snapshot that can be serialized, deserialized, and passed into `Doc.ingestSnapshot`.
Doc.prototype.toSnapshot = function() {
return {
v: this.version,
data: clone(this.data),
type: this.type.uri
};
};

// *** Receiving operations

// This is called when the server acknowledges an operation from the client.
Expand Down
5 changes: 1 addition & 4 deletions lib/db/memory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var DB = require('./index');
var Snapshot = require('../snapshot');
var util = require('../util');
var clone = util.clone;

// In-memory ShareDB database
//
Expand Down Expand Up @@ -172,7 +173,3 @@ MemoryDB.prototype._getVersionSync = function(collection, id) {
var collectionOps = this.ops[collection];
return (collectionOps && collectionOps[id] && collectionOps[id].length) || 0;
};

function clone(obj) {
return (obj === undefined) ? undefined : JSON.parse(JSON.stringify(obj));
}
5 changes: 5 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,8 @@ exports.nextTick = function(callback) {
callback.apply(null, args);
});
};

exports.clone = function(obj) {
return (obj === undefined) ? undefined : JSON.parse(JSON.stringify(obj));
};

26 changes: 26 additions & 0 deletions test/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,30 @@ describe('Doc', function() {
], done);
});
});

describe('toSnapshot', function() {
var doc;
beforeEach(function(done) {
doc = this.connection.get('dogs', 'scooby');
doc.create({name: 'Scooby'}, done);
});

it('generates a snapshot', function() {
expect(doc.toSnapshot()).to.eql({
v: 1,
data: {name: 'Scooby'},
type: 'http://sharejs.org/types/JSONv0'
});
});

it('clones snapshot data to guard against mutation', function() {
var snapshot = doc.toSnapshot();
doc.data.name = 'Shaggy';
expect(snapshot).to.eql({
v: 1,
data: {name: 'Scooby'},
type: 'http://sharejs.org/types/JSONv0'
});
});
});
});
5 changes: 1 addition & 4 deletions test/projections.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var expect = require('chai').expect;
var projections = require('../lib/projections');
var type = require('../lib/types').defaultType.uri;
var clone = require('../lib/util').clone;

describe('projection utility methods', function() {
describe('projectSnapshot', function() {
Expand Down Expand Up @@ -385,7 +386,3 @@ describe('projection utility methods', function() {
});
});
});

function clone(obj) {
return (obj === undefined) ? undefined : JSON.parse(JSON.stringify(obj));
}

0 comments on commit 6d58093

Please sign in to comment.