@@ -26,6 +26,7 @@ import { deepEqual, getDefaultEmulatorHostnameAndPort } from '@firebase/util';
2626import { User } from '../auth/user' ;
2727import {
2828 IndexedDbOfflineComponentProvider ,
29+ LruGcMemoryOfflineComponentProvider ,
2930 MultiTabOfflineComponentProvider ,
3031 OfflineComponentProvider ,
3132 OnlineComponentProvider
@@ -286,28 +287,65 @@ export function configureFirestore(firestore: Firestore): void {
286287}
287288
288289/**
289- * Attempts to enable persistent storage, if possible .
290+ * Attempts to enable the LRU garbage collector for memory persistence .
290291 *
291292 * Must be called before any other functions (other than
292293 * {@link initializeFirestore}, {@link (getFirestore:1)} or
293294 * {@link clearIndexedDbPersistence}.
294295 *
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.
298298 *
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.
305302 *
306303 * @param firestore - The {@link Firestore} instance to enable persistence for.
307- * @param persistenceSettings - Optional settings object to configure
308- * persistence.
309304 * @returns A `Promise` that represents successfully enabling persistent storage.
310305 */
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+ */
311349export function enableIndexedDbPersistence (
312350 firestore : Firestore ,
313351 persistenceSettings ?: PersistenceSettings
0 commit comments