@@ -27,8 +27,11 @@ import {
27
27
synchronizeLastDocumentChangeReadTime
28
28
} from '../local/local_store' ;
29
29
import {
30
- MultiTabSyncEngine ,
31
- newMultiTabSyncEngine ,
30
+ applyActiveTargetsChange ,
31
+ applyBatchState ,
32
+ applyPrimaryState ,
33
+ applyTargetState ,
34
+ getActiveClients ,
32
35
newSyncEngine ,
33
36
SyncEngine
34
37
} from './sync_engine' ;
@@ -45,6 +48,7 @@ import { Code, FirestoreError } from '../util/error';
45
48
import { OnlineStateSource } from './types' ;
46
49
import { LruParams , LruScheduler } from '../local/lru_garbage_collector' ;
47
50
import { IndexFreeQueryEngine } from '../local/index_free_query_engine' ;
51
+ import { MemorySharedClientStateSyncer } from '../local/shared_client_state_syncer' ;
48
52
import {
49
53
indexedDbStoragePrefix ,
50
54
IndexedDbPersistence ,
@@ -194,14 +198,19 @@ export class MemoryComponentProvider implements ComponentProvider {
194
198
}
195
199
196
200
createSyncEngine ( cfg : ComponentConfiguration ) : SyncEngine {
197
- return newSyncEngine (
201
+ const syncEngine = newSyncEngine (
198
202
this . localStore ,
199
203
this . remoteStore ,
200
204
this . datastore ,
201
205
this . sharedClientState ,
202
206
cfg . initialUser ,
203
- cfg . maxConcurrentLimboResolutions
207
+ cfg . maxConcurrentLimboResolutions ,
208
+ /* isPrimary= */ true
204
209
) ;
210
+ this . sharedClientState . syncEngine = new MemorySharedClientStateSyncer ( [
211
+ cfg . clientId
212
+ ] ) ;
213
+ return syncEngine ;
205
214
}
206
215
207
216
clearPersistence (
@@ -229,17 +238,6 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider {
229
238
) ;
230
239
}
231
240
232
- createSyncEngine ( cfg : ComponentConfiguration ) : SyncEngine {
233
- return newSyncEngine (
234
- this . localStore ,
235
- this . remoteStore ,
236
- this . datastore ,
237
- this . sharedClientState ,
238
- cfg . initialUser ,
239
- cfg . maxConcurrentLimboResolutions
240
- ) ;
241
- }
242
-
243
241
createGarbageCollectionScheduler (
244
242
cfg : ComponentConfiguration
245
243
) : GarbageCollectionScheduler | null {
@@ -296,17 +294,13 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider {
296
294
* `synchronizeTabs` will be enabled.
297
295
*/
298
296
export class MultiTabIndexedDbComponentProvider extends IndexedDbComponentProvider {
299
- syncEngine ! : MultiTabSyncEngine ;
300
-
301
297
async initialize ( cfg : ComponentConfiguration ) : Promise < void > {
302
298
await super . initialize ( cfg ) ;
303
299
304
300
// NOTE: This will immediately call the listener, so we make sure to
305
301
// set it after localStore / remoteStore are started.
306
302
await this . persistence . setPrimaryStateListener ( async isPrimary => {
307
- await ( this . syncEngine as MultiTabSyncEngine ) . applyPrimaryState (
308
- isPrimary
309
- ) ;
303
+ await applyPrimaryState ( this . syncEngine , isPrimary ) ;
310
304
if ( this . gcScheduler ) {
311
305
if ( isPrimary && ! this . gcScheduler . started ) {
312
306
this . gcScheduler . start ( this . localStore ) ;
@@ -324,17 +318,24 @@ export class MultiTabIndexedDbComponentProvider extends IndexedDbComponentProvid
324
318
}
325
319
326
320
createSyncEngine ( cfg : ComponentConfiguration ) : SyncEngine {
327
- const syncEngine = newMultiTabSyncEngine (
321
+ const startsAsPrimary =
322
+ ! cfg . persistenceSettings . durable ||
323
+ ! cfg . persistenceSettings . synchronizeTabs ;
324
+ const syncEngine = newSyncEngine (
328
325
this . localStore ,
329
326
this . remoteStore ,
330
327
this . datastore ,
331
328
this . sharedClientState ,
332
329
cfg . initialUser ,
333
- cfg . maxConcurrentLimboResolutions
330
+ cfg . maxConcurrentLimboResolutions ,
331
+ startsAsPrimary
334
332
) ;
335
- if ( this . sharedClientState instanceof WebStorageSharedClientState ) {
336
- this . sharedClientState . syncEngine = syncEngine ;
337
- }
333
+ this . sharedClientState . syncEngine = {
334
+ applyBatchState : applyBatchState . bind ( null , syncEngine ) ,
335
+ applyTargetState : applyTargetState . bind ( null , syncEngine ) ,
336
+ applyActiveTargetsChange : applyActiveTargetsChange . bind ( null , syncEngine ) ,
337
+ getActiveClients : getActiveClients . bind ( null , syncEngine )
338
+ } ;
338
339
return syncEngine ;
339
340
}
340
341
0 commit comments