UNPKG

@equinor/fusion-query

Version:
188 lines (187 loc) 7.82 kB
import { type ActionInstanceMap, type ActionTypes } from '@equinor/fusion-observable'; import type { CacheSortFn, QueryCacheMutation, QueryCacheRecord } from './types'; /** * Creates a set of actions to manipulate cache entries. * * @template TType The type of the value being cached. * @template TArgs The type of the arguments associated with the cache entry. * @returns An object containing the cache actions. */ export default function createActions<TType = any, TArgs = any>(): { /** * Action to set a cache entry. * * @param key The cache key. * @param record The cache record to set. * @returns An action object with the cache key and record. */ set: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[key: string, record: QueryCacheRecord<TType, TArgs>], { key: string; record: QueryCacheRecord<TType, TArgs>; }, "cache/set", never, never>; /** * Action to set a cache entry. * * @param key The cache key. * @param entry The cache entry, including value, arguments, and transaction identifier. * @returns An action object with the cache key and entry. */ insert: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[key: string, entry: { value: TType; args: TArgs; transaction: string; }], { key: string; entry: { value: TType; args: TArgs; transaction: string; }; }, "cache/insert", never, never>; /** * Action to remove a cache entry. * * @param key The cache key to remove. * @returns An action object with the cache key. */ remove: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[key: string], string, "cache/remove", never, never>; /** * Action to invalidate a cache entry. * * @param key The cache key to invalidate. * @returns An action object with the cache key. */ invalidate: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[key?: string | undefined, item?: QueryCacheRecord | undefined], string | undefined, "cache/invalidate", never, { item: QueryCacheRecord | undefined; }>; /** * Action to mutate a cache entry. * * @param changes An object containing the cache key, new value, and optional updated timestamp and transaction identifier. * @param current The current cache record, if available. * @returns An action object with the changes to be applied to the cache entry and the current cache record as metadata. */ mutate: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[key: string, changes: QueryCacheMutation<TType>, item?: QueryCacheRecord<TType, TArgs> | undefined], { key: string; value: TType; updated?: number; }, "cache/mutate", never, { item: QueryCacheRecord<TType, TArgs> | undefined; }>; /** * Action to trim the cache based on certain criteria. * * @param payload Object containing optional sort function, validation function, and size limit. * @returns An action object with the payload. */ trim: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[payload: { sort?: CacheSortFn<TType, TArgs>; validate?: (item: QueryCacheRecord<TType, TArgs>) => boolean; size?: number; }], { sort?: CacheSortFn<TType, TArgs>; validate?: (item: QueryCacheRecord<TType, TArgs>) => boolean; size?: number; }, "cache/trim", never, never>; }; /** * Type representing the builder function for cache actions. * * @template TType The type of the value being cached. * @template TArgs The type of the arguments associated with the cache entry. */ export type ActionBuilder<TType = any, TArgs = any> = ReturnType<typeof createActions<TType, TArgs>>; /** * The singleton actions object containing all cache manipulation actions. */ export declare const actions: { /** * Action to set a cache entry. * * @param key The cache key. * @param record The cache record to set. * @returns An action object with the cache key and record. */ set: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[key: string, record: QueryCacheRecord<any, any>], { key: string; record: QueryCacheRecord<any, any>; }, "cache/set", never, never>; /** * Action to set a cache entry. * * @param key The cache key. * @param entry The cache entry, including value, arguments, and transaction identifier. * @returns An action object with the cache key and entry. */ insert: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[key: string, entry: { value: any; args: any; transaction: string; }], { key: string; entry: { value: any; args: any; transaction: string; }; }, "cache/insert", never, never>; /** * Action to remove a cache entry. * * @param key The cache key to remove. * @returns An action object with the cache key. */ remove: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[key: string], string, "cache/remove", never, never>; /** * Action to invalidate a cache entry. * * @param key The cache key to invalidate. * @returns An action object with the cache key. */ invalidate: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[key?: string | undefined, item?: QueryCacheRecord | undefined], string | undefined, "cache/invalidate", never, { item: QueryCacheRecord | undefined; }>; /** * Action to mutate a cache entry. * * @param changes An object containing the cache key, new value, and optional updated timestamp and transaction identifier. * @param current The current cache record, if available. * @returns An action object with the changes to be applied to the cache entry and the current cache record as metadata. */ mutate: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[key: string, changes: QueryCacheMutation<any>, item?: QueryCacheRecord<any, any> | undefined], { key: string; value: any; updated?: number; }, "cache/mutate", never, { item: QueryCacheRecord<any, any> | undefined; }>; /** * Action to trim the cache based on certain criteria. * * @param payload Object containing optional sort function, validation function, and size limit. * @returns An action object with the payload. */ trim: import("@equinor/fusion-observable").ActionCreatorWithPreparedPayload<[payload: { sort?: CacheSortFn<any, any> | undefined; validate?: ((item: QueryCacheRecord<any, any>) => boolean) | undefined; size?: number; }], { sort?: CacheSortFn<any, any> | undefined; validate?: ((item: QueryCacheRecord<any, any>) => boolean) | undefined; size?: number; }, "cache/trim", never, never>; }; /** * Type representing the map of action instances for a given value and argument types. * * @template TType The type of the value being cached. * @template TArgs The type of the arguments associated with the cache entry. */ export type ActionMap<TType = any, TArgs = any> = ActionInstanceMap<ActionBuilder<TType, TArgs>>; /** * Type representing the actions available for cache manipulation. * * @template TType The type of the value being cached. * @template TArgs The type of the arguments associated with the cache entry. */ export type Actions<TType = any, TArgs = any> = ActionTypes<ActionMap<TType, TArgs>>;