UNPKG

@equinor/fusion-query

Version:
101 lines (100 loc) 4.26 kB
import type { Observable } from 'rxjs'; import type { QueryCacheMutation, QueryCacheRecord, QueryCacheStateData } from './types'; import type { ActionMap, Actions } from './actions'; /** * Defines the options used for trimming the query cache. * @template TType - The type of the response data. * @template TArgs - The type of the arguments used to fetch the data. */ type TrimOptions<TType, TArgs> = ActionMap<TType, TArgs>['trim']['payload']; /** * Constructor arguments for creating a new instance of QueryCache. * @template TType - The type of the response data. * @template TArgs - The type of the arguments used to fetch the data. */ export type QueryCacheCtorArgs<TType, TArgs> = { /** * The initial state data for the query cache. */ initial?: QueryCacheStateData<TType, TArgs>; /** * The options used for trimming the query cache. */ trimming?: TrimOptions<TType, TArgs>; }; /** * A class that represents a cache for storing query results. * @template TType - The type of the response data. * @template TArgs - The type of the arguments used to fetch the data. */ export declare class QueryCache<TType, TArgs> { #private; /** * Retrieves the current state of the query cache as a record. * @returns {Record<string, QueryCacheRecord<TType, TArgs>>} The current state. */ get state(): Record<string, QueryCacheRecord<TType, TArgs>>; /** * Retrieves an Observable that emits the current state of the query cache. * @returns {Observable<Record<string, QueryCacheRecord<TType, TArgs>>>} An observable of the current state. */ get state$(): Observable<Record<string, QueryCacheRecord<TType, TArgs>>>; /** * Retrieves an Observable that emits the actions dispatched to the query cache. * @returns {Observable<Actions<TType, TArgs>>} An observable of the dispatched actions. */ get action$(): Observable<Actions<TType, TArgs>>; /** * Creates a new instance of QueryCache. * @param {QueryCacheCtorArgs<TType, TArgs>} args - The constructor arguments. */ constructor(args?: QueryCacheCtorArgs<TType, TArgs>); /** * Checks if an item exists in the query cache by key. * @param {string} key - The key of the item to check. * @returns {boolean} True if the item exists, otherwise false. */ has(key: string): boolean; /** * Sets the value of an item in the query cache. * @param {string} key - The key of the item to set. * @param {Pick<QueryCacheRecord<TType, TArgs>, 'value' | 'args' | 'transaction'>} record - The new value of the item. */ setItem(key: string, record: Pick<QueryCacheRecord<TType, TArgs>, 'value' | 'args' | 'transaction'>): void; /** * Retrieves an item from the query cache by key. * @param {string} key - The key of the item to retrieve. * @returns {QueryCacheRecord<TType, TArgs> | undefined} The cached item or undefined if not found. */ getItem(key: string): QueryCacheRecord<TType, TArgs> | undefined; /** * Removes an item from the query cache by key. * @param {string} key - The key of the item to remove. */ removeItem(key: string): void; /** * Invalidates an item in the query cache by key, causing it to be refetched on next request. * @param {string} key - The key of the item to invalidate. */ invalidate(key?: string): void; /** * Mutates an item in the query cache by key. * @param {string} key - The key of the item to mutate. * @param {QueryCacheMutation | ((current: TType) => QueryCacheMutation)} changes - The changes to apply to the item. */ mutate(key: string, changes: QueryCacheMutation<TType> | ((current?: TType) => QueryCacheMutation<TType>)): VoidFunction; /** * Trims the query cache based on the provided options. * @param {TrimOptions<TType, TArgs>} options - The options for trimming the cache. */ trim(options: TrimOptions<TType, TArgs>): void; /** * Resets the query cache to its initial state. */ reset(): void; /** * Completes the query cache, signaling that no more items will be added to the cache. */ complete(): void; } export default QueryCache;