@@ -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' ;
@@ -46,9 +49,9 @@ import { OnlineStateSource } from './types';
46
49
import { LruParams , LruScheduler } from '../local/lru_garbage_collector' ;
47
50
import { IndexFreeQueryEngine } from '../local/index_free_query_engine' ;
48
51
import {
49
- indexedDbStoragePrefix ,
52
+ indexedDbClearPersistence ,
50
53
IndexedDbPersistence ,
51
- indexedDbClearPersistence
54
+ indexedDbStoragePrefix
52
55
} from '../local/indexeddb_persistence' ;
53
56
import {
54
57
MemoryEagerDelegate ,
@@ -59,6 +62,7 @@ import { newSerializer } from '../platform/serializer';
59
62
import { getDocument , getWindow } from '../platform/dom' ;
60
63
import { CredentialsProvider } from '../api/credentials' ;
61
64
import { Connection } from '../remote/connection' ;
65
+
62
66
const MEMORY_ONLY_PERSISTENCE_ERROR_MESSAGE =
63
67
'You are using the memory-only build of Firestore. Persistence support is ' +
64
68
'only available via the @firebase/firestore bundle or the ' +
@@ -200,7 +204,8 @@ export class MemoryComponentProvider implements ComponentProvider {
200
204
this . datastore ,
201
205
this . sharedClientState ,
202
206
cfg . initialUser ,
203
- cfg . maxConcurrentLimboResolutions
207
+ cfg . maxConcurrentLimboResolutions ,
208
+ /* isPrimary= */ true
204
209
) ;
205
210
}
206
211
@@ -229,17 +234,6 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider {
229
234
) ;
230
235
}
231
236
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
237
createGarbageCollectionScheduler (
244
238
cfg : ComponentConfiguration
245
239
) : GarbageCollectionScheduler | null {
@@ -296,17 +290,13 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider {
296
290
* `synchronizeTabs` will be enabled.
297
291
*/
298
292
export class MultiTabIndexedDbComponentProvider extends IndexedDbComponentProvider {
299
- syncEngine ! : MultiTabSyncEngine ;
300
-
301
293
async initialize ( cfg : ComponentConfiguration ) : Promise < void > {
302
294
await super . initialize ( cfg ) ;
303
295
304
296
// NOTE: This will immediately call the listener, so we make sure to
305
297
// set it after localStore / remoteStore are started.
306
298
await this . persistence . setPrimaryStateListener ( async isPrimary => {
307
- await ( this . syncEngine as MultiTabSyncEngine ) . applyPrimaryState (
308
- isPrimary
309
- ) ;
299
+ await applyPrimaryState ( this . syncEngine , isPrimary ) ;
310
300
if ( this . gcScheduler ) {
311
301
if ( isPrimary && ! this . gcScheduler . started ) {
312
302
this . gcScheduler . start ( this . localStore ) ;
@@ -324,16 +314,28 @@ export class MultiTabIndexedDbComponentProvider extends IndexedDbComponentProvid
324
314
}
325
315
326
316
createSyncEngine ( cfg : ComponentConfiguration ) : SyncEngine {
327
- const syncEngine = newMultiTabSyncEngine (
317
+ const startsAsPrimary =
318
+ ! cfg . persistenceSettings . durable ||
319
+ ! cfg . persistenceSettings . synchronizeTabs ;
320
+ const syncEngine = newSyncEngine (
328
321
this . localStore ,
329
322
this . remoteStore ,
330
323
this . datastore ,
331
324
this . sharedClientState ,
332
325
cfg . initialUser ,
333
- cfg . maxConcurrentLimboResolutions
326
+ cfg . maxConcurrentLimboResolutions ,
327
+ startsAsPrimary
334
328
) ;
335
329
if ( this . sharedClientState instanceof WebStorageSharedClientState ) {
336
- this . sharedClientState . syncEngine = syncEngine ;
330
+ this . sharedClientState . syncEngine = {
331
+ applyBatchState : applyBatchState . bind ( null , syncEngine ) ,
332
+ applyTargetState : applyTargetState . bind ( null , syncEngine ) ,
333
+ applyActiveTargetsChange : applyActiveTargetsChange . bind (
334
+ null ,
335
+ syncEngine
336
+ ) ,
337
+ getActiveClients : getActiveClients . bind ( null , syncEngine )
338
+ } ;
337
339
}
338
340
return syncEngine ;
339
341
}
0 commit comments