Skip to content

Commit 28451cd

Browse files
authored
Merge pull request #42 from /issues/41-unknown-cv
Handling unknown cv
2 parents 7dbe13c + 898c9ce commit 28451cd

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/simperium/channel.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import uuid from 'node-uuid'
77

88
const jsondiff = new JSONDiff( {list_diff: false} )
99

10+
const UNKNOWN_CV = '?'
11+
1012
var operation = {
1113
MODIFY: 'M',
1214
REMOVE: '-'
@@ -218,6 +220,7 @@ export default function Channel( appid, access_token, bucket, store ) {
218220
message.on( 'i', this.onIndex.bind( this ) );
219221
message.on( 'c', this.onChanges.bind( this ) );
220222
message.on( 'e', this.onVersion.bind( this ) );
223+
message.on( 'cv', this.onChangeVersion.bind( this ) );
221224
message.on( 'o', function() {} );
222225

223226
this.networkQueue = new NetworkQueue();
@@ -333,9 +336,7 @@ Channel.prototype.onAuth = function( data ) {
333336
this.localQueue.start();
334337
this.sendChangeVersionRequest( cv );
335338
} else {
336-
this.bucket.isIndexing = true;
337-
this.bucket.emit( 'indexing' );
338-
this.sendIndexRequest();
339+
this.startIndexing();
339340
}
340341
};
341342

@@ -345,6 +346,13 @@ Channel.prototype.onAuth = function( data ) {
345346
}
346347
};
347348

349+
Channel.prototype.startIndexing = function() {
350+
this.localQueue.pause();
351+
this.bucket.isIndexing = true;
352+
this.bucket.emit( 'indexing' );
353+
this.sendIndexRequest();
354+
};
355+
348356
Channel.prototype.onConnect = function() {
349357
var init = {
350358
name: this.bucket.name,
@@ -383,8 +391,7 @@ Channel.prototype.onIndex = function( data ) {
383391
} else {
384392
this.sendIndexRequest( mark );
385393
}
386-
}
387-
;
394+
};
388395

389396
Channel.prototype.sendIndexRequest = function( mark ) {
390397
this.send( format( 'i:1:%s::10', mark ? mark : '' ) );
@@ -403,8 +410,14 @@ Channel.prototype.onChanges = function( data ) {
403410
} );
404411
// emit ready after all server changes have been applied
405412
this.emit( 'ready' );
406-
}
407-
;
413+
};
414+
415+
Channel.prototype.onChangeVersion = function( data ) {
416+
if ( data === UNKNOWN_CV ) {
417+
this.store.setChangeVersion( null )
418+
.then( () => this.startIndexing() );
419+
}
420+
};
408421

409422
Channel.prototype.onVersion = function( data ) {
410423
var ghost = parseVersionMessage( data );
@@ -484,6 +497,10 @@ LocalQueue.prototype.start = function() {
484497
}
485498
}
486499

500+
LocalQueue.prototype.pause = function() {
501+
this.ready = false;
502+
};
503+
487504
LocalQueue.prototype.acknowledge = function( change ) {
488505
if ( this.sent[change.id] === change ) {
489506
delete this.sent[change.id];

test/simperium/channel_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,16 @@ describe( 'Channel', function() {
385385
} );
386386
} );
387387

388+
it( 'should request index when cv is unknown', done => {
389+
channel.once( 'send', ( data ) => {
390+
ok( !store.cv );
391+
ok( bucket.isIndexing );
392+
equal( data, 'i:1:::10' );
393+
done();
394+
} );
395+
channel.handleMessage( 'cv:?' );
396+
} );
397+
388398
// TODO: handle auth failures
389399
// <= 0:auth:expired
390400
// => 0:i:1:::10

0 commit comments

Comments
 (0)