Skip to content

Commit 63dd7bb

Browse files
Solving startup race (#999)
1 parent 49420f0 commit 63dd7bb

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export class FirestoreClient {
394394

395395
// NOTE: This will immediately call the listener, so we make sure to
396396
// set it after localStore / remoteStore are started.
397-
this.persistence.setPrimaryStateListener(isPrimary =>
397+
await this.persistence.setPrimaryStateListener(isPrimary =>
398398
this.syncEngine.applyPrimaryState(isPrimary)
399399
);
400400
});

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,11 @@ export class IndexedDbPersistence implements Persistence {
185185
});
186186
}
187187

188-
setPrimaryStateListener(primaryStateListener: PrimaryStateListener): void {
188+
setPrimaryStateListener(
189+
primaryStateListener: PrimaryStateListener
190+
): Promise<void> {
189191
this.primaryStateListener = primaryStateListener;
190-
primaryStateListener(this.isPrimary);
192+
return primaryStateListener(this.isPrimary);
191193
}
192194

193195
/**

packages/firestore/src/local/memory_persistence.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ export class MemoryPersistence implements Persistence {
7070
return [this.clientId];
7171
}
7272

73-
setPrimaryStateListener(primaryStateListener: PrimaryStateListener): void {
73+
setPrimaryStateListener(
74+
primaryStateListener: PrimaryStateListener
75+
): Promise<void> {
7476
// All clients using memory persistence act as primary.
75-
primaryStateListener(true);
77+
return primaryStateListener(true);
7678
}
7779

7880
getMutationQueue(user: User): MutationQueue {

packages/firestore/src/local/persistence.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ export interface Persistence {
106106
*
107107
* PORTING NOTE: This is only used for Web multi-tab.
108108
*/
109-
setPrimaryStateListener(primaryStateListener: PrimaryStateListener): void;
109+
setPrimaryStateListener(
110+
primaryStateListener: PrimaryStateListener
111+
): Promise<void>;
110112

111113
/**
112114
* Returns the IDs of the clients that are currently active. If multi-tab

packages/firestore/test/unit/specs/spec_test_runner.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ abstract class TestRunner {
478478
await this.remoteStore.start();
479479
await this.syncEngine.start();
480480

481-
this.persistence.setPrimaryStateListener(isPrimary =>
481+
await this.persistence.setPrimaryStateListener(isPrimary =>
482482
this.syncEngine.applyPrimaryState(isPrimary)
483483
);
484484

@@ -872,15 +872,9 @@ abstract class TestRunner {
872872
await this.syncEngine.start();
873873
await this.sharedClientState.start();
874874

875-
const deferred = new Deferred<void>();
876-
// We need to wait for the processing in `applyPrimaryState` to complete,
877-
// but `setPrimaryStateListener` doesn't return a promise.
878-
this.persistence.setPrimaryStateListener(isPrimary => {
879-
return this.syncEngine
880-
.applyPrimaryState(isPrimary)
881-
.then(deferred.resolve);
882-
});
883-
await deferred.promise;
875+
await this.persistence.setPrimaryStateListener(isPrimary =>
876+
this.syncEngine.applyPrimaryState(isPrimary)
877+
);
884878
});
885879
}
886880

0 commit comments

Comments
 (0)