import { type Observable } from 'rxjs'; import { type Maybe, type Milliseconds } from '@dereekb/util'; import { type FirestoreCollectionType, type FirestoreModelKey } from '../collection/collection'; import { type FirestoreContextCacheFactory, type FirestoreCacheEntry, type FirestoreCacheEvent, type FirestoreContextCache } from './cache'; /** * Default TTL used by the in-memory cache when no TTL is specified. * * Set to 5 minutes. */ export declare const IN_MEMORY_CACHE_DEFAULT_TTL: Milliseconds; /** * Delegate that handles actual cache storage for a single collection. * * Implementations provide the storage mechanism (e.g. in-memory Map, no storage for logging). * The shared {@link makeFirestoreCollectionCache} handles TTL checking, event emission, * and enabled/disabled state around the delegate. * * @template T - The document data type */ export interface FirestoreCollectionCacheDelegate { /** * Gets a raw cached entry by key, without TTL or enabled checks. */ get(key: FirestoreModelKey): Maybe>; /** * Stores a cache entry by key. */ set(key: FirestoreModelKey, entry: FirestoreCacheEntry): void; /** * Deletes a cache entry by key. */ delete(key: FirestoreModelKey): void; /** * Clears all entries. */ clear(): void; } /** * Creates an in-memory {@link FirestoreCollectionCacheDelegate} backed by a Map. * * @returns A Map-backed delegate instance. * * @example * ```ts * const delegate = inMemoryFirestoreCollectionCacheDelegate(); * ``` */ export declare function inMemoryFirestoreCollectionCacheDelegate(): FirestoreCollectionCacheDelegate; /** * Creates a no-storage {@link FirestoreCollectionCacheDelegate} that discards all data. * * Used by {@link readLoggingFirestoreContextCache} where only event emission matters. * * @returns A no-storage delegate that discards all data. * * @example * ```ts * const delegate = noopFirestoreCollectionCacheDelegate(); * ``` */ export declare function noopFirestoreCollectionCacheDelegate(): FirestoreCollectionCacheDelegate; /** * Configuration for {@link makeFirestoreContextCache}. */ export interface MakeFirestoreContextCacheConfig { /** * Default TTL applied to all collections unless overridden per-collection. */ readonly defaultTtl: Milliseconds; /** * Creates a delegate for a new collection cache. * * Called once per collection type when the cache is first requested. */ readonly createDelegate: (collectionType: FirestoreCollectionType) => FirestoreCollectionCacheDelegate; /** * Optional function that transforms the raw events observable before it is exposed as `events$`. * * Can be used to filter, map, or augment events. For example, the read-logging cache * uses this to filter out 'miss' events since they are expected and not meaningful. * * When not provided, the raw events observable is used directly. */ readonly mapEvents$?: (events$: Observable) => Observable; } /** * Creates a {@link FirestoreContextCache} with shared event handling, enable/disable controls, * and per-type management. The actual cache storage for each collection is handled by * delegates created via {@link MakeFirestoreContextCacheConfig.createDelegate}. * * This is the shared foundation used by both {@link inMemoryFirestoreContextCache} and * {@link readLoggingFirestoreContextCache}. * * @param config - Configuration including default TTL and delegate factory * @returns A new context cache * * @example * ```ts * const contextCache = makeFirestoreContextCache({ * defaultTtl: 60000, * createDelegate: () => inMemoryFirestoreCollectionCacheDelegate() * }); * ``` */ export declare function makeFirestoreContextCache(config: MakeFirestoreContextCacheConfig): FirestoreContextCache; /** * Creates a {@link FirestoreContextCacheFactory} that stores cached documents in memory. * * Uses simple Map-based storage with TTL-based expiration. Suitable for * client-side caching where the application lifecycle matches the cache lifecycle. * * @returns A factory function that creates in-memory context caches * * @example * ```ts * const factory = inMemoryFirestoreContextCacheFactory(); * const contextCache = factory({ defaultTtl: 60000 }); * ``` */ export declare function inMemoryFirestoreContextCacheFactory(): FirestoreContextCacheFactory; /** * Configuration for creating an in-memory {@link FirestoreContextCache}. */ export interface InMemoryFirestoreContextCacheConfig { /** * Default TTL applied to all collections unless overridden per-collection. */ readonly defaultTtl?: Milliseconds; } /** * Creates an in-memory {@link FirestoreContextCache} that manages per-collection * caches with TTL-based expiration, per-type enable/disable, and event streaming. * * @param config - Optional configuration * @returns A new context cache * * @example * ```ts * const contextCache = inMemoryFirestoreContextCache({ defaultTtl: 60000 }); * const userCache = contextCache.cacheForCollection('user', { defaultTtl: 30000 }); * userCache.set('users/abc', { data: userData }); * ``` */ export declare function inMemoryFirestoreContextCache(config?: InMemoryFirestoreContextCacheConfig): FirestoreContextCache; /** * Creates a {@link FirestoreContextCacheFactory} that emits events for all cache interactions * but does not actually store any data. * * Useful for analytics, debugging, and monitoring read patterns without the * memory overhead of actual caching. * * @returns A factory function that creates read-logging context caches * * @example * ```ts * const factory = readLoggingFirestoreContextCacheFactory(); * const contextCache = factory(); * contextCache.events$.subscribe((event) => console.log(event)); * ``` */ export declare function readLoggingFirestoreContextCacheFactory(): FirestoreContextCacheFactory; /** * Configuration for creating a read-logging {@link FirestoreContextCache}. */ export interface ReadLoggingFirestoreContextCacheConfig { /** * Default TTL applied to all collections. Since no data is stored, * this only affects what gets reported as a "miss" vs "hit" (always miss). */ readonly defaultTtl?: Milliseconds; } /** * Creates a {@link FirestoreContextCache} that emits events for all cache interactions * but does not actually store any data. Every `get()` call results in a 'miss' event. * * Useful for tracking read patterns, debugging, and monitoring which documents * are accessed and how often, without the memory overhead of actual caching. * * @param config - Optional configuration * @returns A new read-logging context cache * * @example * ```ts * const contextCache = readLoggingFirestoreContextCache(); * contextCache.events$.subscribe((event) => { * console.log(`${event.type}: ${event.collectionType} ${event.key}`); * }); * ``` */ export declare function readLoggingFirestoreContextCache(config?: ReadLoggingFirestoreContextCacheConfig): FirestoreContextCache;