@@ -71,8 +71,9 @@ export interface Settings extends LiteSettings {
71
71
}
72
72
73
73
/**
74
- * The root reference to the Firestore database and the entry point for the
75
- * tree-shakeable SDK.
74
+ * The Cloud Firestore service interface.
75
+ *
76
+ * Do not call this constructor directly. Instead, use {@link getFirestore()}.
76
77
*/
77
78
export class FirebaseFirestore
78
79
extends LiteFirestore
@@ -173,6 +174,17 @@ export class FirebaseFirestore
173
174
}
174
175
}
175
176
177
+ /**
178
+ * Initializes a new instance of Cloud Firestore with the provided settings.
179
+ * Can only be called before any other function, including
180
+ * {@link getFirestore()}. If the custom settings are empty, this function is
181
+ * equivalent to calling {@link getFirestore()}.
182
+ *
183
+ * @param app The {@link FirebaseApp} with which the `Firestore` instance will
184
+ * be associated.
185
+ * @param settings A settings object to configure the `Firestore` instance.
186
+ * @return A newly initialized `Firestore` instance.
187
+ */
176
188
export function initializeFirestore (
177
189
app : FirebaseApp ,
178
190
settings : Settings
@@ -197,10 +209,41 @@ export function initializeFirestore(
197
209
return firestore ;
198
210
}
199
211
212
+ /**
213
+ * Returns the existing instance of Firestore that is associated with the
214
+ * provided {@link FirebaseApp}. If no instance exists, initializes a new
215
+ * instance with default settings.
216
+ *
217
+ * @param app The {@link FirebaseApp} instance that the returned Firestore
218
+ * instance is associated with.
219
+ * @return The `Firestore` instance of the provided app.
220
+ */
200
221
export function getFirestore ( app : FirebaseApp ) : FirebaseFirestore {
201
222
return _getProvider ( app , 'firestore-exp' ) . getImmediate ( ) as FirebaseFirestore ;
202
223
}
203
224
225
+ /**
226
+ * Attempts to enable persistent storage, if possible.
227
+ *
228
+ * Must be called before any other functions (other than
229
+ * {@link initializeFirestore()}, {@link getFirestore()} or
230
+ * {@link clearIndexedDbPersistence()}.
231
+ *
232
+ * If this fails, `enableIndexedDbPersistence()` will reject the promise it
233
+ * returns. Note that even after this failure, the `Firestore` instance will
234
+ * remain usable, however offline persistence will be disabled.
235
+ *
236
+ * There are several reasons why this can fail, which can be identified by
237
+ * the `code` on the error.
238
+ *
239
+ * * failed-precondition: The app is already open in another browser tab.
240
+ * * unimplemented: The browser is incompatible with the offline
241
+ * persistence implementation.
242
+ *
243
+ * @param firestore The `Firestore` instance to enable persistence for.
244
+ * @param persistenceSettings Optional settings object to configure persistence.
245
+ * @return A promise that represents successfully enabling persistent storage.
246
+ */
204
247
export function enableIndexedDbPersistence (
205
248
firestore : FirebaseFirestore ,
206
249
persistenceSettings ?: PersistenceSettings
@@ -234,6 +277,28 @@ export function enableIndexedDbPersistence(
234
277
} ) ;
235
278
}
236
279
280
+ /**
281
+ * Attempts to enable multi-tab persistent storage, if possible. If enabled
282
+ * across all tabs, all operations share access to local persistence, including
283
+ * shared execution of queries and latency-compensated local document updates
284
+ * across all connected instances.
285
+ *
286
+ * If this fails, `enableMultiTabIndexedDbPersistence()` will reject the promise
287
+ * it returns. Note that even after this failure, the `Firestore` instance will
288
+ * remain usable, however offline persistence will be disabled.
289
+ *
290
+ * There are several reasons why this can fail, which can be identified by
291
+ * the `code` on the error.
292
+ *
293
+ * * failed-precondition: The app is already open in another browser tab and
294
+ * multi-tab is not enabled.
295
+ * * unimplemented: The browser is incompatible with the offline
296
+ * persistence implementation.
297
+ *
298
+ * @param firestore The `Firestore` instance to enable persistence for.
299
+ * @return A promise that represents successfully enabling persistent
300
+ * storage.
301
+ */
237
302
export function enableMultiTabIndexedDbPersistence (
238
303
firestore : FirebaseFirestore
239
304
) : Promise < void > {
@@ -265,6 +330,28 @@ export function enableMultiTabIndexedDbPersistence(
265
330
} ) ;
266
331
}
267
332
333
+ /**
334
+ * Clears the persistent storage. This includes pending writes and cached
335
+ * documents.
336
+ *
337
+ * Must be called while the `Firestore` instance is not started (after the app is
338
+ * terminated or when the app is first initialized). On startup, this function
339
+ * must be called before other functions (other than {@link
340
+ * initializeFirestore()} or {@link getFirestore()})). If the `Firestore`
341
+ * instance is still running, the promise will be rejected with the error code
342
+ * of `failed-precondition`.
343
+ *
344
+ * Note: `clearIndexedDbPersistence()` is primarily intended to help write
345
+ * reliable tests that use Cloud Firestore. It uses an efficient mechanism for
346
+ * dropping existing data but does not attempt to securely overwrite or
347
+ * otherwise make cached data unrecoverable. For applications that are sensitive
348
+ * to the disclosure of cached data in between user sessions, we strongly
349
+ * recommend not enabling persistence at all.
350
+ *
351
+ * @param firestore The `Firestore` instance to clear persistence for.
352
+ * @return A promise that is resolved when the persistent storage is
353
+ * cleared. Otherwise, the promise is rejected with an error.
354
+ */
268
355
export function clearIndexedDbPersistence (
269
356
firestore : FirebaseFirestore
270
357
) : Promise < void > {
@@ -290,6 +377,22 @@ export function clearIndexedDbPersistence(
290
377
return deferred . promise ;
291
378
}
292
379
380
+ /**
381
+ * Waits until all currently pending writes for the active user have been
382
+ * acknowledged by the backend.
383
+ *
384
+ * The returned Promise resolves immediately if there are no outstanding writes.
385
+ * Otherwise, the Promise waits for all previously issued writes (including
386
+ * those written in a previous app session), but it does not wait for writes
387
+ * that were added after the function is called. If you want to wait for
388
+ * additional writes, call `waitForPendingWrites()` again.
389
+ *
390
+ * Any outstanding `waitForPendingWrites()` Promises are rejected during user
391
+ * changes.
392
+ *
393
+ * @return A Promise which resolves when all currently pending writes have been
394
+ * acknowledged by the backend.
395
+ */
293
396
export function waitForPendingWrites (
294
397
firestore : FirebaseFirestore
295
398
) : Promise < void > {
@@ -303,6 +406,12 @@ export function waitForPendingWrites(
303
406
return deferred . promise ;
304
407
}
305
408
409
+ /**
410
+ * Re-enables use of the network for this Firestore instance after a prior
411
+ * call to {@link disableNetwork()}.
412
+ *
413
+ * @return A promise that is resolved once the network has been enabled.
414
+ */
306
415
export function enableNetwork ( firestore : FirebaseFirestore ) : Promise < void > {
307
416
firestore . _verifyNotTerminated ( ) ;
308
417
@@ -314,6 +423,14 @@ export function enableNetwork(firestore: FirebaseFirestore): Promise<void> {
314
423
} ) ;
315
424
}
316
425
426
+ /**
427
+ * Disables network usage for this instance. It can be re-enabled via {@link
428
+ * enableNetwork()}. While the network is disabled, any snapshot listeners,
429
+ * `getDoc()` or `getDocs()` calls will return results from cache, and any write
430
+ * operations will be queued until the network is restored.
431
+ *
432
+ * @return A promise that is resolved once the network has been disabled.
433
+ */
317
434
export function disableNetwork ( firestore : FirebaseFirestore ) : Promise < void > {
318
435
firestore . _verifyNotTerminated ( ) ;
319
436
@@ -325,6 +442,28 @@ export function disableNetwork(firestore: FirebaseFirestore): Promise<void> {
325
442
} ) ;
326
443
}
327
444
445
+ /**
446
+ * Terminates the provided Firestore instance.
447
+ *
448
+ * After calling `terminate()` only the `clearIndexedDbPersistence()` function
449
+ * may be used. Any other function will throw a `FirestoreError`.
450
+ *
451
+ * To restart after termination, create a new instance of FirebaseFirestore with
452
+ * {@link getFirestore()}.
453
+ *
454
+ * Termination does not cancel any pending writes, and any promises that are
455
+ * awaiting a response from the server will not be resolved. If you have
456
+ * persistence enabled, the next time you start this instance, it will resume
457
+ * sending these writes to the server.
458
+ *
459
+ * Note: Under normal circumstances, calling `terminate()` is not required. This
460
+ * function is useful only when you want to force this instance to release all
461
+ * of its resources or in combination with `clearIndexedDbPersistence()` to
462
+ * ensure that all local state is destroyed between test runs.
463
+ *
464
+ * @return A promise that is resolved when the instance has been successfully
465
+ * terminated.
466
+ */
328
467
export function terminate ( firestore : FirebaseFirestore ) : Promise < void > {
329
468
_removeServiceInstance ( firestore . app , 'firestore-exp' ) ;
330
469
return firestore . _delete ( ) ;
0 commit comments