UNPKG

browser-cache-async

Version:

The browser-cache-async package provides a highly customizable, asynchronous caching system for client-side data management. Leveraging the power of the browser's IndexedDB, it enables efficient storage and retrieval of API responses, significantly reduci

48 lines (47 loc) 1.8 kB
import { IRecordID } from 'browser-keyval-stores'; import { ICacheIfFn, IStringValue, IQueryOptions, IProcessedQueryOptions } from './shared/types.js'; import { IBrowserCache } from './types.js'; /** * Browser Cache * Object in charge of managing the caching of data in the browser. */ declare class BrowserCache<T> implements IBrowserCache<T> { private __store; private __debugMode; constructor(id: string, debugMode?: boolean); /** * Retrieves and unwraps the data from the cache. It returns undefined if the data is not found or * is stale. * Note: this is a stable method, it will not throw errors. * @param id * @returns Promise<T | undefined> */ private __get; /** * Stores the data in the cache. * Note: this is a stable method, it will not throw errors. * @param id * @param data * @param revalidate * @returns Promise<void> */ private __set; /** * Verifies if the query has already been cached and that its data is still fresh. Otherwise, it * executes the query and caches the data. * @param options * @returns Promise<T> * @throws * - INVALID_QUERY_FUNCTION: If the query function is not a function. * - INVALID_REVALIDATE_VALUE: If the revalidate value is not a valid number or StringValue. */ run(options: IQueryOptions<T>): Promise<T>; /** * Deletes a record from the cache - forcing a revalidation the next time it is retrieved. * Note: this is a stable method, it will not throw errors. * @param id * @returns Promise<void> */ revalidate(id?: IRecordID): Promise<void>; } export { type ICacheIfFn, type IStringValue, type IQueryOptions, type IProcessedQueryOptions, type IBrowserCache, BrowserCache, };