Skip to content

Commit 9023375

Browse files
authored
Merge pull request #43 from Simperium/issue/download-ghost-when-outdated
Bucket requests ghost at "sv" when local version is not up to date
2 parents 28451cd + c13e491 commit 9023375

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/simperium/channel.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ internal.findAcknowledgedChange = function( change ) {
139139
}
140140
};
141141

142+
internal.requestObjectVersion = function( id, version ) {
143+
return new Promise( resolve => {
144+
this.once( `version.${ id }.${ version }`, data => {
145+
resolve( data );
146+
} );
147+
this.send( `e:${ id }.${ version }` );
148+
} );
149+
};
150+
142151
internal.applyChange = function( change, ghost ) {
143152
var acknowledged = internal.findAcknowledgedChange.bind( this )( change ),
144153
error,
@@ -166,14 +175,15 @@ internal.applyChange = function( change, ghost ) {
166175

167176
if ( change.o === operation.MODIFY ) {
168177
if ( ghost && ( ghost.version !== change.sv ) ) {
169-
// throw new Error( "Source version and ghost version do not match" );
178+
internal.requestObjectVersion.call( this, change.id, change.sv ).then( data => {
179+
internal.applyChange.call( this, change, { version: change.sv, data } )
180+
} );
170181
return;
171182
}
172183

173184
original = ghost.data;
174185
patch = change.v;
175186
modified = jsondiff.apply_object_diff( original, patch );
176-
177187
return internal.updateObjectVersion.bind( this )( change.id, change.ev, modified, original, patch, acknowledged )
178188
.then( emit );
179189
} else if ( change.o === operation.REMOVE ) {
@@ -424,6 +434,7 @@ Channel.prototype.onVersion = function( data ) {
424434

425435
this.emit( 'version', ghost.id, ghost.version, ghost.data );
426436
this.emit( 'version.' + ghost.id, ghost.id, ghost.version, ghost.data );
437+
this.emit( 'version.' + ghost.id + '.' + ghost.version, ghost.data );
427438
};
428439

429440
function NetworkQueue() {

test/simperium/channel_test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,19 @@ describe( 'Channel', function() {
470470
} );
471471
channel.handleMessage( 'auth:user@example.com' );
472472
} );
473+
474+
it( 'should request entire object when source version is out of date', ( done ) => {
475+
var change = {o: 'M', id: 'thing', sv: 1, ev: 2, ccid: 'abc', cv: 'new-cv', v: diff( { hello: 'mundo'}, {hello: 'world'} ) };
476+
channel.once( 'send', ( data ) => {
477+
equal( data, `e:${change.id}.${change.sv}` );
478+
channel.once( 'change-version', ( cv ) => {
479+
equal( cv, 'new-cv' );
480+
done();
481+
} );
482+
channel.handleMessage( `e:${change.id}.${change.sv}\n${JSON.stringify( { data: { hello: 'mundo'} } )} ` );
483+
} );
484+
channel.handleMessage( `c:[${JSON.stringify( change )}]` );
485+
} );
473486
} );
474487
} );
475488

0 commit comments

Comments
 (0)