import { Observable } from 'rxjs'; import { Predicate } from './Predicate'; import { ISystemClock, SystemOverrides } from './System'; import { IDataCompartmentSource } from './IDataCompartmentSource'; import { IApplicationCacheManager } from './Caching'; import { DataStoreToken } from './DataStoreToken'; export declare type EventListener = (listener: () => void) => void; export declare type CompartmentComparer = (a: T, b: T) => boolean; export declare function defaultComparer(a: T, b: T): boolean; export declare type LoadingStrategy = 'auto' | 'lazy' | 'manual'; export declare type LoadingOptions = { strategy: LoadingStrategy; predicate?: Predicate; }; export interface RetentionOptions { policies: IRetentionPolicy[]; } export declare class RetentionContext { readonly appCache: IApplicationCacheManager; readonly clock: ISystemClock; constructor(appCache: IApplicationCacheManager, clock: ISystemClock); isExpired(compartment: IDataCompartment): boolean; } export interface IRetentionPolicy { isExpired(compartment: IDataCompartment, context: RetentionContext): boolean; markForExpiration(compartment: IDataCompartment, context: RetentionContext): void; } export declare class ApplicationCacheManagerRetentionPolicy implements IRetentionPolicy { isExpired(compartment: IDataCompartment, context: RetentionContext): boolean; markForExpiration(): void; } export declare function cacheForSeconds(seconds: number): IRetentionPolicy; type ActionWithArgs = (value: T) => void; export interface DataCompartmentState { key: string; token: DataStoreToken; options: DataCompartmentOptions; lastLoaded: number; value?: any; initialized: boolean; loading: boolean; error?: unknown; } export type ChangeRecord = { previous?: T; current: T; }; export type ChangeSubscription = (change: ChangeRecord) => void; /** * Provides strongly-typed configuration options for an individual data compartment. */ export interface DataCompartmentOptions { /** * Provides fine-grained control over how/when the compartment loads. * If no options are specified, the compartment will auto load. */ loadingOptions?: LoadingOptions; /** * The default value to seed the cache. */ defaultValue: T; /** * Optional configuration for controlling how the data is retained. */ retention?: RetentionOptions; /** * The data source used to fill a compartment. */ source: IDataCompartmentSource; /** * The comparer to use to determine whether to publish the next value. * Publishes if the value is false. * @default defaultComparer */ comparer?: CompartmentComparer; /** * Optional callback when a compartment has finished loading. * @param value The data loaded from the source. */ onLoad?: ActionWithArgs; /** * Optional callback when an error occurs while loading the compartment. * @param error The error thrown by the source. */ onError?: ActionWithArgs; /** * Optional system overrides (mostly used for testing but could prove useful otherwise) */ system?: SystemOverrides; } export interface IDataCompartment { /** * The unique identifier of the compartment. */ key: string; /** * The unique identifier of the compartment. */ token: DataStoreToken; /** * Provides an observable that emits true when initialization is complete. * @returns An observable that emits true when initialization is complete. */ initialized$: Observable; /** * Provides an observable that emits true when the compartment is loading. * @returns An observable that emits true when initialization is complete. */ loading$: Observable; /** * The options used to configure the compartment. */ options: DataCompartmentOptions; /** * Gets a snapshot of the current state of the compartment. If the compartment is configured as lazy loading, this will not trigger any loading. * @returns DataCompartmentState */ getCompartmentState(): DataCompartmentState; /** * Provides an observable that emits a timestamp when the compartment was last loaded. */ lastLoaded$: Observable; /** * Reloads the compartment. * Note: This triggers the `reload` event. */ reload: () => Promise; /** * Resets the compartment to the default value. * If a retention policy was configured, this will also bust the token. */ reset: () => Promise; } export {};