@equinor/fusion-query
Version:
121 lines • 4.45 kB
JavaScript
import { FlowSubject } from '@equinor/fusion-observable';
import { actions } from './actions';
import createReducer from './create-reducer';
/**
* 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 class QueryCache {
/**
* The internal state of the query cache, represented as a FlowSubject.
*/
#state;
/**
* Retrieves the current state of the query cache as a record.
* @returns {Record<string, QueryCacheRecord<TType, TArgs>>} The current state.
*/
get state() {
return this.#state.value;
}
/**
* 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$() {
return this.#state.asObservable();
}
/**
* 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$() {
return this.#state.action$;
}
/**
* Creates a new instance of QueryCache.
* @param {QueryCacheCtorArgs<TType, TArgs>} args - The constructor arguments.
*/
constructor(args) {
const { trimming, initial } = args ?? {};
this.#state = new FlowSubject(createReducer(actions, initial));
if (trimming) {
this.#state.addEffect('cache/set', () => this.#state.next(actions.trim(trimming)));
}
}
/**
* 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) {
return key in this.#state.value;
}
/**
* 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, record) {
const { args, transaction, value } = record;
this.#state.next(actions.insert(key, { args, transaction, value }));
}
/**
* 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) {
return this.#state.value[key];
}
/**
* Removes an item from the query cache by key.
* @param {string} key - The key of the item to remove.
*/
removeItem(key) {
this.#state.next(actions.remove(key));
}
/**
* 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) {
const item = key ? this.#state.value[key] : undefined;
this.#state.next(actions.invalidate(key, item));
}
/**
* 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, changes) {
const current = key in this.#state.value ? this.#state.value[key] : undefined;
if (!current) {
throw new Error(`Cannot mutate cache item with key ${key}: item not found`);
}
const next = typeof changes === 'function' ? changes(current?.value) : changes;
this.#state.next(actions.mutate(key, next, current));
return () => this.#state.next(actions.set(key, current));
}
/**
* Trims the query cache based on the provided options.
* @param {TrimOptions<TType, TArgs>} options - The options for trimming the cache.
*/
trim(options) {
this.#state.next(actions.trim(options));
}
/**
* Resets the query cache to its initial state.
*/
reset() {
this.#state.reset();
}
/**
* Completes the query cache, signaling that no more items will be added to the cache.
*/
complete() {
this.#state.complete();
}
}
export default QueryCache;
//# sourceMappingURL=QueryCache.js.map