@@ -7,6 +7,8 @@ import uuid from 'node-uuid'
77
88const jsondiff = new JSONDiff ( { list_diff : false } )
99
10+ const UNKNOWN_CV = '?'
11+
1012var 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+
348356Channel . 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
389396Channel . 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
409422Channel . 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+
487504LocalQueue . prototype . acknowledge = function ( change ) {
488505 if ( this . sent [ change . id ] === change ) {
489506 delete this . sent [ change . id ] ;
0 commit comments