@naturalcycles/js-lib
Version:
Standard library for universal (browser + Node.js) javascript
41 lines (40 loc) • 1.56 kB
TypeScript
import type { CommonLogger } from '../log/commonLogger.js';
import type { AnyAsyncFunction, AnyFunction, AnyObject, MaybeParameters } from '../types.js';
import type { AsyncMemoCache, MethodDecorator } from './memo.util.js';
export interface AsyncMemoOptions<FN> {
/**
* Provide a custom implementation of AsyncMemoCache.
* Function that creates an instance of `AsyncMemoCache`.
*/
cacheFactory: () => AsyncMemoCache;
/**
* Provide a custom implementation of CacheKey function.
*/
cacheKeyFn?: (args: MaybeParameters<FN>) => any;
/**
* Default to `console`
*/
logger?: CommonLogger;
}
export interface AsyncMemoInstance {
/**
* Clears the cache.
*/
clear: () => Promise<void>;
getInstanceCache: () => Map<AnyObject, AsyncMemoCache>;
getCache: (instance: AnyAsyncFunction) => AsyncMemoCache | undefined;
}
/**
* Like `@_Memo`, but allowing async MemoCache implementation.
*
* Implementation is more complex than `@_Memo`, because it needs to handle "in-flight" Promises
* while waiting for cache to resolve, to prevent "async swarm" issue.
*
* @experimental consider normal `@_Memo` for most of the cases, it's stable and predictable
*/
export declare const _AsyncMemo: <FN>(opt: AsyncMemoOptions<FN>) => MethodDecorator<FN>;
/**
Call it on a method that is decorated with `@_AsyncMemo` to get access to additional functions,
e.g `clear` to clear the cache, or get its underlying data.
*/
export declare function _getAsyncMemo(method: AnyFunction): AsyncMemoInstance;