@@ -26,6 +26,7 @@ import { deepEqual, getDefaultEmulatorHostnameAndPort } from '@firebase/util';
26
26
import { User } from '../auth/user' ;
27
27
import {
28
28
IndexedDbOfflineComponentProvider ,
29
+ LruGcMemoryOfflineComponentProvider ,
29
30
MultiTabOfflineComponentProvider ,
30
31
OfflineComponentProvider ,
31
32
OnlineComponentProvider
@@ -286,28 +287,65 @@ export function configureFirestore(firestore: Firestore): void {
286
287
}
287
288
288
289
/**
289
- * Attempts to enable persistent storage, if possible .
290
+ * Attempts to enable the LRU garbage collector for memory persistence .
290
291
*
291
292
* Must be called before any other functions (other than
292
293
* {@link initializeFirestore}, {@link (getFirestore:1)} or
293
294
* {@link clearIndexedDbPersistence}.
294
295
*
295
- * If this fails, `enableIndexedDbPersistence()` will reject the promise it
296
- * returns. Note that even after this failure, the {@link Firestore} instance will
297
- * remain usable, however offline persistence will be disabled.
296
+ * By default, any documents that are not part of an active query result or
297
+ * with no mutation attached to them are removed from memory immediately.
298
298
*
299
- * There are several reasons why this can fail, which can be identified by
300
- * the `code` on the error.
301
- *
302
- * * failed-precondition: The app is already open in another browser tab.
303
- * * unimplemented: The browser is incompatible with the offline
304
- * persistence implementation.
299
+ * This function changes the default behavior, to enable a least-recent-used
300
+ * garbage collector. Documents will be collected when their total size exceeds
301
+ * `Settings.cacheSizeBytes`, with least recently used documents get removed first.
305
302
*
306
303
* @param firestore - The {@link Firestore} instance to enable persistence for.
307
- * @param persistenceSettings - Optional settings object to configure
308
- * persistence.
309
304
* @returns A `Promise` that represents successfully enabling persistent storage.
310
305
*/
306
+ export function enableMemoryLRUGarbageCollection (
307
+ firestore : Firestore
308
+ ) : Promise < void > {
309
+ firestore = cast ( firestore , Firestore ) ;
310
+ verifyNotInitialized ( firestore ) ;
311
+
312
+ const client = ensureFirestoreConfigured ( firestore ) ;
313
+ const settings = firestore . _freezeSettings ( ) ;
314
+
315
+ const onlineComponentProvider = new OnlineComponentProvider ( ) ;
316
+ const offlineComponentProvider = new LruGcMemoryOfflineComponentProvider (
317
+ settings . cacheSizeBytes
318
+ ) ;
319
+ return setPersistenceProviders (
320
+ client ,
321
+ onlineComponentProvider ,
322
+ offlineComponentProvider
323
+ ) ;
324
+ }
325
+
326
+ /**
327
+ * Attempts to enable persistent storage, if possible. //
328
+ * //
329
+ * Must be called before any other functions (other than //
330
+ * {@link initializeFirestore}, {@link (getFirestore:1)} or //
331
+ * {@link clearIndexedDbPersistence}. //
332
+ * //
333
+ * If this fails, `enableIndexedDbPersistence()` will reject the promise it //
334
+ * returns. Note that even after this failure, the {@link Firestore} instance will //
335
+ * remain usable, however offline persistence will be disabled. //
336
+ * //
337
+ * There are several reasons why this can fail, which can be identified by //
338
+ * the `code` on the error. //
339
+ * //
340
+ * * failed-precondition: The app is already open in another browser tab. //
341
+ * * unimplemented: The browser is incompatible with the offline //
342
+ * persistence implementation. //
343
+ * //
344
+ * @param firestore - The {@link Firestore} instance to enable persistence for. //
345
+ * @param persistenceSettings - Optional settings object to configure //
346
+ * persistence. //
347
+ * @returns A `Promise` that represents successfully enabling persistent storage. //
348
+ */
311
349
export function enableIndexedDbPersistence (
312
350
firestore : Firestore ,
313
351
persistenceSettings ?: PersistenceSettings
0 commit comments