@equinor/fusion-query
Version:
Reactive data fetching and caching library with observable streams and comprehensive event system
128 lines (127 loc) • 5.54 kB
TypeScript
import type { Observable } from 'rxjs';
import type { QueryCacheMutation, QueryCacheRecord, QueryCacheStateData } from './types';
import type { ActionMap, Actions } from './actions';
import { QueryCacheEvent, type QueryCacheEventData, type QueryCacheEvents } from './events';
/**
* 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>>;
/**
* Protected method to emit QueryCache lifecycle events.
*
* This method creates and emits events that track the various operations performed
* on the cache, allowing external observers to monitor cache changes and mutations.
*
* @template TType - The specific event type from QueryCacheEvents
* @param type - The event type identifier
* @param key - The cache key associated with this event
* @param data - Optional event-specific data payload (type-safe based on event type)
* @protected
*/
protected _registerEvent<TType extends keyof QueryCacheEvents>(type: TType, args?: {
key?: string;
data?: QueryCacheEventData<TType>;
}): void;
/**
* An Observable stream of QueryCache lifecycle events.
*
* This stream emits events that track the various operations performed on the cache,
* including entry set, inserted, removed, invalidated, mutated, and trimmed events.
* Subscribers can monitor cache changes and handle different lifecycle events.
*
* @returns An Observable stream of QueryCache events
*/
get event$(): Observable<QueryCacheEvent>;
/**
* 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;