Skip to content

Commit a259b84

Browse files
committed
Deletes current cv and requests new index when cv unknown
1 parent 00a8553 commit a259b84

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/simperium/channel.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,7 @@ Channel.prototype.onAuth = function( data ) {
336336
this.localQueue.start();
337337
this.sendChangeVersionRequest( cv );
338338
} else {
339-
this.bucket.isIndexing = true;
340-
this.bucket.emit( 'indexing' );
341-
this.sendIndexRequest();
339+
this.startIndexing();
342340
}
343341
};
344342

@@ -348,6 +346,13 @@ Channel.prototype.onAuth = function( data ) {
348346
}
349347
};
350348

349+
Channel.prototype.startIndexing = function() {
350+
this.localQueue.pause();
351+
this.bucket.isIndexing = true;
352+
this.bucket.emit( 'indexing' );
353+
this.sendIndexRequest();
354+
}
355+
351356
Channel.prototype.onConnect = function() {
352357
var init = {
353358
name: this.bucket.name,
@@ -410,9 +415,8 @@ Channel.prototype.onChanges = function( data ) {
410415

411416
Channel.prototype.onChangeVersion = function( data ) {
412417
if ( data === UNKNOWN_CV ) {
413-
// TODO: delete current cv
414-
// Start new index
415-
throw new Error( 'not implemented' );
418+
this.store.setChangeVersion( null )
419+
.then( () => this.startIndexing() )
416420
}
417421
}
418422

@@ -494,6 +498,10 @@ LocalQueue.prototype.start = function() {
494498
}
495499
}
496500

501+
LocalQueue.prototype.pause = function() {
502+
this.ready = false;
503+
}
504+
497505
LocalQueue.prototype.acknowledge = function( change ) {
498506
if ( this.sent[change.id] === change ) {
499507
delete this.sent[change.id];

test/simperium/channel_test.js

Lines changed: 10 additions & 8 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
@@ -434,14 +444,6 @@ describe( 'Channel', function() {
434444
} );
435445
} );
436446

437-
it( 'should request index when cv is unknown', done => {
438-
channel.once( 'send', ( data ) => {
439-
equal( data, '' );
440-
done()
441-
} )
442-
channel.handleMessage( 'cv:?' );
443-
} )
444-
445447
it( 'should emit index and ready event when index complete', () => new Promise( resolve => {
446448
var page = 'i:{"index":[{"id":"objectid","v":1,"d":{"title":"Hello World"}}],"current":"cv"}';
447449
let indexed = false

0 commit comments

Comments
 (0)