Skip to content

Commit

Permalink
add options parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
janmonschke committed Apr 29, 2015
1 parent a8d635f commit 789cbde
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ When your application has changed the state of this object, the `sync` method of

The [diffsync-todos app](https://github.com/janmonschke/diffsync-todos) provides an example client-side integration of diffsync into a todo list application. Check it out to find out how to integrate it into your existing application. In a nutshell, it makes use of `Object.observe` (and a polyfill for it) to track changes from within the app that are then synced to the server.

As a third optional parameter you can pass an options object to the constructor, that will then be applied to the internal diff-library. For a list of options, please check <https://github.com/benjamine/jsondiffpatch#options>.

### Server

Setting up the server in a very minimal way (with express):
Expand All @@ -130,6 +132,8 @@ Setting up the server in a very minimal way (with express):

This is all that is needed for running the server part. There is no further addition necessary. Most of the logic is happening in the `DataAdapter`, which is described in the next section.

As a third optional parameter you can pass an options object to the constructor, that will then be applied to the internal diff-library. For a list of options, please check <https://github.com/benjamine/jsondiffpatch#options>.

### DataAdapter

A `DataAdapter` is used by the server component internally to fetch data to initialize the synchronization and to save the data periodically. The simple interface allows to write a custom data provider for which ever data source you are using in your web app.
Expand Down
23 changes: 16 additions & 7 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
var isEmpty = require('lodash.isempty'),
var assign = require('lodash.assign'),
bind = require('lodash.bind'),
isEmpty = require('lodash.isempty'),
EventEmitter = require('events').EventEmitter,
jsondiffpatch = require('jsondiffpatch').create({
objectHash: function(obj) { return obj.id || obj._id || JSON.stringify(obj); }
}),
jsondiffpatch = require('jsondiffpatch'),

COMMANDS = require('./commands'),
utils = require('./utils'),
Client;

Client = function(socket, room){
Client = function(socket, room, diffOptions){
if(!socket){ throw new Error('No socket specified'); }
if(!room){ room = ''; }
if(!diffOptions){ diffOptions = {}; }

this.socket = socket;
this.room = room;
Expand All @@ -26,6 +26,15 @@ Client = function(socket, room){
edits: []
};

// set up the jsondiffpatch options
// see here for options: https://github.com/benjamine/jsondiffpatch#options
diffOptions = assign({
objectHash: function(obj) { return obj.id || obj._id || JSON.stringify(obj); }
}, diffOptions);

this.jsondiffpatch = jsondiffpatch.create(diffOptions);

// let client be an EventEmitter
EventEmitter.call(this);

// bind functions
Expand Down Expand Up @@ -154,7 +163,7 @@ Client.prototype.syncWithServer = function(){
* @return {Diff} The diff of both documents
*/
Client.prototype.createDiff = function(docA, docB){
return jsondiffpatch.diff(docA, docB);
return this.jsondiffpatch.diff(docA, docB);
};

/**
Expand All @@ -164,7 +173,7 @@ Client.prototype.createDiff = function(docA, docB){
* @param {Diff} patch
*/
Client.prototype.applyPatchTo = function(obj, patch){
jsondiffpatch.patch(obj, patch);
this.jsondiffpatch.patch(obj, patch);
};

/**
Expand Down
30 changes: 18 additions & 12 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var isEmpty = require('lodash.isempty'),
var assign = require('lodash.assign'),
bind = require('lodash.bind'),
jsondiffpatch = require('jsondiffpatch').create({
objectHash: function(obj) { return obj.id || obj._id || JSON.stringify(obj); }
}),
isEmpty = require('lodash.isempty'),
jsondiffpatch = require('jsondiffpatch'),

COMMANDS = require('./commands'),
utils = require('./utils'),
Server;

Server = function(adapter, transport){
Server = function(adapter, transport, diffOptions){
if(!(adapter && transport)){ throw new Error('Need to specify an adapter and a transport'); }
if(!diffOptions){ diffOptions = {}; }

this.adapter = adapter;
this.transport = transport;
Expand All @@ -18,6 +18,14 @@ Server = function(adapter, transport){
// bind functions
this.trackConnection = bind(this.trackConnection, this);

// set up the jsondiffpatch options
// see here for options: https://github.com/benjamine/jsondiffpatch#options
diffOptions = assign({
objectHash: function(obj) { return obj.id || obj._id || JSON.stringify(obj); }
}, diffOptions);

this.jsondiffpatch = jsondiffpatch.create(diffOptions);

this.transport.on('connection', this.trackConnection);
};

Expand Down Expand Up @@ -124,12 +132,12 @@ Server.prototype.receiveEdit = function(connection, editMessage, sendToClient){

// 3) patch the shadow
// var snapshot = utils.deepCopy(clientDoc.shadow.doc);
jsondiffpatch.patch(clientDoc.shadow.doc, utils.deepCopy(edit.diff));
this.jsondiffpatch.patch(clientDoc.shadow.doc, utils.deepCopy(edit.diff));
// clientDoc.shadow.doc = snapshot;

// apply the patch to the server's document
// snapshot = utils.deepCopy(doc.serverCopy);
jsondiffpatch.patch(doc.serverCopy, utils.deepCopy(edit.diff));
this.jsondiffpatch.patch(doc.serverCopy, utils.deepCopy(edit.diff));
// doc.serverCopy = snapshot;

// 3.a) increase the version number for the shadow if diff not empty
Expand All @@ -142,7 +150,7 @@ Server.prototype.receiveEdit = function(connection, editMessage, sendToClient){
console.log('error', 'patch rejected!!', edit.serverVersion, '->', clientDoc.shadow.serverVersion, ':',
edit.localVersion, '->', clientDoc.shadow.localVersion);
}
});
}.bind(this));

// 4) save a snapshot of the document
this.saveSnapshot(editMessage.room);
Expand All @@ -166,9 +174,7 @@ Server.prototype.saveSnapshot = function(room){

Server.prototype.sendServerChanges = function(doc, clientDoc, send){
// create a diff from the current server version to the client's shadow
// important: use deepcopied versions
// var diff = jsondiffpatch.diff(utils.deepCopy(clientDoc.shadow.doc), utils.deepCopy(doc.serverCopy));
var diff = jsondiffpatch.diff(clientDoc.shadow.doc, doc.serverCopy);
var diff = this.jsondiffpatch.diff(clientDoc.shadow.doc, doc.serverCopy);
var basedOnServerVersion = clientDoc.shadow.serverVersion;

// add the difference to the server's edit stack
Expand All @@ -182,7 +188,7 @@ Server.prototype.sendServerChanges = function(doc, clientDoc, send){
clientDoc.shadow.serverVersion++;

// apply the patch to the server shadow
jsondiffpatch.patch(clientDoc.shadow.doc, utils.deepCopy(diff));
this.jsondiffpatch.patch(clientDoc.shadow.doc, utils.deepCopy(diff));
}

// we explicitly want empty diffs to get sent as well
Expand Down
6 changes: 6 additions & 0 deletions test/client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ describe('DiffSync Client', function(){
assert.notStrictEqual(testClient().room, null);
assert.notStrictEqual(testClient().room, undefined);
});

it('should apply the correct options to jsondiffpatch', function(){
var client = new Client({}, 1, { textDiff: { minLength: 2 }});

assert(client.jsondiffpatch.options().textDiff.minLength === 2);
});
});

describe('initialize', function(){
Expand Down
6 changes: 6 additions & 0 deletions test/server_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ describe('DiffSync Server', function(){
new Server(testAdapter(), testTransport());
});
});

it('should apply the correct options to jsondiffpatch', function(){
var client = new Server(testAdapter(), testTransport(), { textDiff: { minLength: 2 }});

assert(client.jsondiffpatch.options().textDiff.minLength === 2);
});
});

describe('trackConnection', function(){
Expand Down

0 comments on commit 789cbde

Please sign in to comment.