Skip to content

Commit e0486ff

Browse files
[Multi-Tab] Master Election (#501)
1 parent 94c20ef commit e0486ff

23 files changed

+1138
-325
lines changed

packages/firestore/src/core/firestore_client.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,12 @@ export class FirestoreClient {
241241
const serializer = new JsonProtoSerializer(this.databaseInfo.databaseId, {
242242
useProto3Json: true
243243
});
244-
this.persistence = new IndexedDbPersistence(storagePrefix, serializer);
244+
this.persistence = new IndexedDbPersistence(
245+
storagePrefix,
246+
this.platform,
247+
this.asyncQueue,
248+
serializer
249+
);
245250
return this.persistence.start();
246251
}
247252

@@ -252,7 +257,7 @@ export class FirestoreClient {
252257
*/
253258
private startMemoryPersistence(): Promise<void> {
254259
this.garbageCollector = new EagerGarbageCollector();
255-
this.persistence = new MemoryPersistence();
260+
this.persistence = new MemoryPersistence(this.asyncQueue);
256261
return this.persistence.start();
257262
}
258263

@@ -309,6 +314,13 @@ export class FirestoreClient {
309314
})
310315
.then(() => {
311316
return this.remoteStore.start();
317+
})
318+
.then(() => {
319+
// NOTE: This will immediately call the listener, so we make sure to
320+
// set it after localStore / remoteStore are started.
321+
this.persistence.setPrimaryStateListener(isPrimary =>
322+
this.syncEngine.applyPrimaryState(isPrimary)
323+
);
312324
});
313325
}
314326

packages/firestore/src/core/sync_engine.ts

+11
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,19 @@ export class SyncEngine implements RemoteSyncer {
121121
[uidKey: string]: SortedMap<BatchId, Deferred<void>>;
122122
};
123123
private targetIdGenerator = TargetIdGenerator.forSyncEngine();
124+
private isPrimary = false;
124125

125126
constructor(
126127
private localStore: LocalStore,
127128
private remoteStore: RemoteStore,
128129
private currentUser: User
129130
) {}
130131

132+
// Only used for testing.
133+
get isPrimaryClient() {
134+
return this.isPrimary;
135+
}
136+
131137
/** Subscribes view and error handler. Can be called only once. */
132138
subscribe(viewHandler: ViewHandler, errorHandler: ErrorHandler): void {
133139
assert(
@@ -616,4 +622,9 @@ export class SyncEngine implements RemoteSyncer {
616622
return this.remoteStore.handleUserChange(user);
617623
});
618624
}
625+
626+
applyPrimaryState(isPrimary: boolean): Promise<void> {
627+
this.isPrimary = isPrimary;
628+
return Promise.resolve();
629+
}
619630
}

0 commit comments

Comments
 (0)