@@ -59,6 +59,8 @@ import {
59
59
WebStorageSharedClientState
60
60
} from '../local/shared_client_state' ;
61
61
import { AutoId } from '../util/misc' ;
62
+ import { PersistenceSettings } from '../api/database' ;
63
+ import { assert } from '../util/assert' ;
62
64
63
65
const LOG_TAG = 'FirestoreClient' ;
64
66
@@ -135,13 +137,14 @@ export class FirestoreClient {
135
137
* fallback succeeds we signal success to the async queue even though the
136
138
* start() itself signals failure.
137
139
*
138
- * @param usePersistence Whether or not to attempt to enable persistence.
140
+ * @param persistenceSettings Settings object to configure offline
141
+ * persistence.
139
142
* @returns A deferred result indicating the user-visible result of enabling
140
143
* offline persistence. This method will reject this if IndexedDB fails to
141
144
* start for any reason. If usePersistence is false this is
142
145
* unconditionally resolved.
143
146
*/
144
- start ( usePersistence : boolean ) : Promise < void > {
147
+ start ( persistenceSettings : PersistenceSettings ) : Promise < void > {
145
148
// We defer our initialization until we get the current user from
146
149
// setUserChangeListener(). We block the async queue until we got the
147
150
// initial user and the initialization is completed. This will prevent
@@ -164,7 +167,7 @@ export class FirestoreClient {
164
167
if ( ! initialized ) {
165
168
initialized = true ;
166
169
167
- this . initializePersistence ( usePersistence , persistenceResult , user )
170
+ this . initializePersistence ( persistenceSettings , persistenceResult , user )
168
171
. then ( ( ) => this . initializeRest ( user ) )
169
172
. then ( initializationDone . resolve , initializationDone . reject ) ;
170
173
} else {
@@ -200,7 +203,7 @@ export class FirestoreClient {
200
203
* platform can't possibly support our implementation then this method rejects
201
204
* the persistenceResult and falls back on memory-only persistence.
202
205
*
203
- * @param usePersistence indicates whether or not to use offline persistence
206
+ * @param persistenceSettings Settings object to configure offline persistence
204
207
* @param persistenceResult A deferred result indicating the user-visible
205
208
* result of enabling offline persistence. This method will reject this if
206
209
* IndexedDB fails to start for any reason. If usePersistence is false
@@ -210,12 +213,12 @@ export class FirestoreClient {
210
213
* succeeded.
211
214
*/
212
215
private initializePersistence (
213
- usePersistence : boolean ,
216
+ persistenceSettings : PersistenceSettings ,
214
217
persistenceResult : Deferred < void > ,
215
218
user : User
216
219
) : Promise < void > {
217
- if ( usePersistence ) {
218
- return this . startIndexedDbPersistence ( user )
220
+ if ( persistenceSettings . enabled ) {
221
+ return this . startIndexedDbPersistence ( user , persistenceSettings )
219
222
. then ( persistenceResult . resolve )
220
223
. catch ( error => {
221
224
// Regardless of whether or not the retry succeeds, from an user
@@ -278,7 +281,15 @@ export class FirestoreClient {
278
281
*
279
282
* @returns A promise indicating success or failure.
280
283
*/
281
- private startIndexedDbPersistence ( user : User ) : Promise < void > {
284
+ private startIndexedDbPersistence (
285
+ user : User ,
286
+ settings : PersistenceSettings
287
+ ) : Promise < void > {
288
+ assert (
289
+ settings . enabled ,
290
+ 'Should only start IndexedDb persitence with offline persistence enabled.'
291
+ ) ;
292
+
282
293
// TODO(http://b/33384523): For now we just disable garbage collection
283
294
// when persistence is enabled.
284
295
this . garbageCollector = new NoOpGarbageCollector ( ) ;
@@ -291,32 +302,35 @@ export class FirestoreClient {
291
302
} ) ;
292
303
293
304
return Promise . resolve ( ) . then ( ( ) => {
294
- this . persistence = new IndexedDbPersistence (
305
+ const persistence : IndexedDbPersistence = new IndexedDbPersistence (
295
306
storagePrefix ,
296
307
this . clientId ,
297
308
this . platform ,
298
309
this . asyncQueue ,
299
310
serializer
300
311
) ;
301
- if ( WebStorageSharedClientState . isAvailable ( this . platform ) ) {
302
- this . sharedClientState = new WebStorageSharedClientState (
303
- this . asyncQueue ,
304
- this . platform ,
305
- storagePrefix ,
306
- this . clientId ,
307
- user
312
+ this . persistence = persistence ;
313
+
314
+ if (
315
+ settings . synchronizeTabs &&
316
+ ! WebStorageSharedClientState . isAvailable ( this . platform )
317
+ ) {
318
+ throw new FirestoreError (
319
+ Code . UNIMPLEMENTED ,
320
+ 'IndexedDB persistence is only available on platforms that support LocalStorage.'
308
321
) ;
309
- } else {
310
- if ( process . env . USE_MOCK_PERSISTENCE !== 'YES' ) {
311
- throw new FirestoreError (
312
- Code . UNIMPLEMENTED ,
313
- 'IndexedDB persistence is only available on platforms that support LocalStorage.'
314
- ) ;
315
- }
316
- debug ( LOG_TAG , 'Starting Persistence in test-only non multi-tab mode' ) ;
317
- this . sharedClientState = new MemorySharedClientState ( ) ;
318
322
}
319
- return this . persistence . start ( ) ;
323
+
324
+ this . sharedClientState = settings . synchronizeTabs
325
+ ? new WebStorageSharedClientState (
326
+ this . asyncQueue ,
327
+ this . platform ,
328
+ storagePrefix ,
329
+ this . clientId ,
330
+ user
331
+ )
332
+ : new MemorySharedClientState ( ) ;
333
+ return persistence . start ( settings . synchronizeTabs ) ;
320
334
} ) ;
321
335
}
322
336
@@ -420,11 +434,11 @@ export class FirestoreClient {
420
434
return this . asyncQueue . enqueue ( async ( ) => {
421
435
// PORTING NOTE: LocalStore does not need an explicit shutdown on web.
422
436
await this . syncEngine . shutdown ( ) ;
423
- await this . remoteStore . shutdown ( ) ;
424
437
await this . sharedClientState . shutdown ( ) ;
425
438
await this . persistence . shutdown (
426
439
options && options . purgePersistenceWithDataLoss
427
440
) ;
441
+ await this . remoteStore . shutdown ( ) ;
428
442
429
443
// `removeUserChangeListener` must be called after shutting down the
430
444
// RemoteStore as it will prevent the RemoteStore from retrieving
0 commit comments