Skip to content

Commit 50f1182

Browse files
committed
Requests and updates ghost at sv and reapplies patch
1 parent 2db1f73 commit 50f1182

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/simperium/channel.js

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

140+
internal.requestObjectVersion = function( id, version ) {
141+
return new Promise( resolve => {
142+
this.once( `version.${ id }.${ version }`, data => {
143+
resolve( data );
144+
} )
145+
this.send( `e:${ id }.${ version }` )
146+
} )
147+
}
148+
140149
internal.applyChange = function( change, ghost ) {
141150
var acknowledged = internal.findAcknowledgedChange.bind( this )( change ),
142151
error,
@@ -164,14 +173,15 @@ internal.applyChange = function( change, ghost ) {
164173

165174
if ( change.o === operation.MODIFY ) {
166175
if ( ghost && ( ghost.version !== change.sv ) ) {
167-
// throw new Error( "Source version and ghost version do not match" );
176+
internal.requestObjectVersion.call( this, change.id, change.sv ).then( data => {
177+
internal.applyChange.call( this, change, { version: change.sv, data } )
178+
} );
168179
return;
169180
}
170181

171182
original = ghost.data;
172183
patch = change.v;
173184
modified = jsondiff.apply_object_diff( original, patch );
174-
175185
return internal.updateObjectVersion.bind( this )( change.id, change.ev, modified, original, patch, acknowledged )
176186
.then( emit );
177187
} else if ( change.o === operation.REMOVE ) {
@@ -411,6 +421,7 @@ Channel.prototype.onVersion = function( data ) {
411421

412422
this.emit( 'version', ghost.id, ghost.version, ghost.data );
413423
this.emit( 'version.' + ghost.id, ghost.id, ghost.version, ghost.data );
424+
this.emit( 'version.' + ghost.id + '.' + ghost.version, ghost.data );
414425
};
415426

416427
function NetworkQueue() {

0 commit comments

Comments
 (0)