Skip to content

Commit a05f186

Browse files
committed
WIP to send full object on 405 or 440 error.
1 parent 9023375 commit a05f186

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

src/simperium/channel.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import { parseMessage, parseVersionMessage, change as change_util } from './util
55
import JSONDiff from './jsondiff'
66
import uuid from 'node-uuid'
77

8-
const jsondiff = new JSONDiff( {list_diff: false} )
8+
const jsondiff = new JSONDiff( {list_diff: false} );
99

10-
const UNKNOWN_CV = '?'
10+
const UNKNOWN_CV = '?';
11+
const CODE_INVALID_VERSION = 405;
12+
const CODE_EMPTY_RESPONSE = 412;
13+
const CODE_INVALID_DIFF = 440;
1114

1215
var operation = {
1316
MODIFY: 'M',
@@ -193,7 +196,18 @@ internal.applyChange = function( change, ghost ) {
193196

194197
internal.handleChangeError = function( err, change, acknowledged ) {
195198
switch ( err.code ) {
196-
case 412: // Change causes no change, just acknowledge it
199+
case CODE_INVALID_VERSION:
200+
case CODE_INVALID_DIFF: // Invalid version or diff, send full object back to server
201+
if ( ! change.hasSentFullObject ) {
202+
change.d = this.store.get( change.id );
203+
change.hasSentFullObject = true;
204+
this.localQueue.queue( change );
205+
} else {
206+
this.localQueue.dequeueChangesFor( change.id );
207+
}
208+
209+
break;
210+
case CODE_EMPTY_RESPONSE: // Change causes no change, just acknowledge it
197211
internal.updateAcknowledged.call( this, acknowledged );
198212
break;
199213
default:
@@ -306,7 +320,6 @@ inherits( Channel, EventEmitter );
306320

307321
Channel.prototype.handleMessage = function( data ) {
308322
var message = parseMessage( data );
309-
310323
this.message.emit( message.command, message.data );
311324
};
312325

test/simperium/channel_test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,26 @@ describe( 'Channel', function() {
383383
channel.localQueue.queue( change );
384384
} );
385385
} );
386+
387+
it( 'should send full object on 405 error', function( done ) {
388+
// if a change is sent and a 405 is returned, the full object should be sent
389+
// Add an object to the store
390+
channel.store.put( 'thing', 1, {} );
391+
392+
// channel should not emit error during this change
393+
channel.on( 'error', function( e ) {
394+
done( e );
395+
} );
396+
397+
// ensure that a change with a `d` property is added to the queue
398+
channel.localQueue.once( 'queued', function( id, change, queue ) {
399+
assert.ok( queue[0].d );
400+
done();
401+
} );
402+
403+
// send a 405 error
404+
channel.handleMessage( 'c:' + JSON.stringify( [{error: 405, id: 'thing', ccids: ['abc']}] ) );
405+
} );
386406
} );
387407

388408
it( 'should request index when cv is unknown', done => {

0 commit comments

Comments
 (0)