@@ -126,6 +126,7 @@ export interface StreamingSyncImplementation extends BaseObserver<StreamingSyncI
126126 triggerCrudUpload : ( ) => void ;
127127 waitForReady ( ) : Promise < void > ;
128128 waitForStatus ( status : SyncStatusOptions ) : Promise < void > ;
129+ waitUntilStatusMatches ( predicate : ( status : SyncStatus ) => boolean ) : Promise < void > ;
129130}
130131
131132export const DEFAULT_CRUD_UPLOAD_THROTTLE_MS = 1000 ;
@@ -184,24 +185,35 @@ export abstract class AbstractStreamingSyncImplementation
184185 async waitForReady ( ) { }
185186
186187 waitForStatus ( status : SyncStatusOptions ) : Promise < void > {
188+ return this . waitUntilStatusMatches ( ( currentStatus ) => {
189+ /**
190+ * Match only the partial status options provided in the
191+ * matching status
192+ */
193+ const matchPartialObject = ( compA : object , compB : object ) => {
194+ return Object . entries ( compA ) . every ( ( [ key , value ] ) => {
195+ const comparisonBValue = compB [ key ] ;
196+ if ( typeof value == 'object' && typeof comparisonBValue == 'object' ) {
197+ return matchPartialObject ( value , comparisonBValue ) ;
198+ }
199+ return value == comparisonBValue ;
200+ } ) ;
201+ } ;
202+
203+ return matchPartialObject ( status , currentStatus ) ;
204+ } ) ;
205+ }
206+
207+ waitUntilStatusMatches ( predicate : ( status : SyncStatus ) => boolean ) : Promise < void > {
187208 return new Promise ( ( resolve ) => {
209+ if ( predicate ( this . syncStatus ) ) {
210+ resolve ( ) ;
211+ return ;
212+ }
213+
188214 const l = this . registerListener ( {
189215 statusChanged : ( updatedStatus ) => {
190- /**
191- * Match only the partial status options provided in the
192- * matching status
193- */
194- const matchPartialObject = ( compA : object , compB : object ) => {
195- return Object . entries ( compA ) . every ( ( [ key , value ] ) => {
196- const comparisonBValue = compB [ key ] ;
197- if ( typeof value == 'object' && typeof comparisonBValue == 'object' ) {
198- return matchPartialObject ( value , comparisonBValue ) ;
199- }
200- return value == comparisonBValue ;
201- } ) ;
202- } ;
203-
204- if ( matchPartialObject ( status , updatedStatus . toJSON ( ) ) ) {
216+ if ( predicate ( updatedStatus ) ) {
205217 resolve ( ) ;
206218 l ?.( ) ;
207219 }
@@ -524,6 +536,7 @@ The next upload iteration will be delayed.`);
524536 // The stream has closed while waiting
525537 return { retry : true } ;
526538 }
539+
527540 // A connection is active and messages are being received
528541 if ( ! this . syncStatus . connected ) {
529542 // There is a connection now
@@ -538,7 +551,7 @@ The next upload iteration will be delayed.`);
538551 const bucketsToDelete = new Set < string > ( bucketMap . keys ( ) ) ;
539552 const newBuckets = new Map < string , BucketDescription > ( ) ;
540553 for ( const checksum of line . checkpoint . buckets ) {
541- newBuckets . set ( checksum . bucket , { name : checksum . bucket , priority : checksum . priority } )
554+ newBuckets . set ( checksum . bucket , { name : checksum . bucket , priority : checksum . priority } ) ;
542555 bucketsToDelete . delete ( checksum . bucket ) ;
543556 }
544557 if ( bucketsToDelete . size > 0 ) {
@@ -567,14 +580,14 @@ The next upload iteration will be delayed.`);
567580 lastSyncedAt : new Date ( ) ,
568581 dataFlow : {
569582 downloading : false
570- } ,
583+ }
571584 } ) ;
572585 }
573586
574587 validatedCheckpoint = targetCheckpoint ;
575588 } else if ( isStreamingSyncCheckpointPartiallyComplete ( line ) ) {
576- this . logger . debug ( 'Partial checkpoint complete' , targetCheckpoint ) ;
577589 const priority = line . partial_checkpoint_complete . priority ;
590+ this . logger . debug ( 'Partial checkpoint complete' , priority ) ;
578591 const result = await this . options . adapter . syncLocalDatabase ( targetCheckpoint ! , priority ) ;
579592 if ( ! result . checkpointValid ) {
580593 // This means checksums failed. Start again with a new checkpoint.
@@ -592,16 +605,12 @@ The next upload iteration will be delayed.`);
592605 priorityStates . push ( {
593606 priority,
594607 lastSyncedAt : new Date ( ) ,
595- hasSynced : true ,
608+ hasSynced : true
596609 } ) ;
597610
598611 this . updateSyncStatus ( {
599612 connected : true ,
600- lastSyncedAt : new Date ( ) ,
601- statusInPriority : priorityStates ,
602- dataFlow : {
603- downloading : false
604- } ,
613+ statusInPriority : priorityStates
605614 } ) ;
606615 }
607616 } else if ( isStreamingSyncCheckpointDiff ( line ) ) {
@@ -628,10 +637,13 @@ The next upload iteration will be delayed.`);
628637 } ;
629638 targetCheckpoint = newCheckpoint ;
630639
631- bucketMap = new Map ( newBuckets . entries ( ) . map ( ( [ name , checksum ] ) => [ name , {
632- name : checksum . bucket ,
633- priority : checksum . priority ,
634- } ] ) ) ;
640+ bucketMap = new Map ( ) ;
641+ newBuckets . forEach ( ( checksum , name ) =>
642+ bucketMap . set ( name , {
643+ name : checksum . bucket ,
644+ priority : checksum . priority
645+ } )
646+ ) ;
635647
636648 const bucketsToDelete = diff . removed_buckets ;
637649 if ( bucketsToDelete . length > 0 ) {
@@ -667,7 +679,7 @@ The next upload iteration will be delayed.`);
667679 this . updateSyncStatus ( {
668680 connected : true ,
669681 lastSyncedAt : new Date ( ) ,
670- statusInPriority : [ ] ,
682+ statusInPriority : [ ]
671683 } ) ;
672684 } else if ( validatedCheckpoint === targetCheckpoint ) {
673685 const result = await this . options . adapter . syncLocalDatabase ( targetCheckpoint ! ) ;
@@ -709,7 +721,7 @@ The next upload iteration will be delayed.`);
709721 ...this . syncStatus . dataFlowStatus ,
710722 ...options . dataFlow
711723 } ,
712- statusInPriority : options . statusInPriority ,
724+ statusInPriority : options . statusInPriority ?? this . syncStatus . statusInPriority
713725 } ) ;
714726
715727 if ( ! this . syncStatus . isEqual ( updatedStatus ) ) {
0 commit comments