import type { CacheData } from './CacheData'; import { LRUMap } from './utils/LRUMap'; type Deferred = T | Promise; type ValueRecord = { [key: string]: T; }; export type CacheGetter> = (key: string, cache: R, ...initialParameter: any) => any; export type CacheGetterParams> = C extends (key: string, cache: any, ...parameter: infer P) => any ? P : never; export type DependencyMap = ValueRecord>>; export type DependencyCacheData)> = { [K in keyof T]: T[K] extends CacheEntanglement ? CacheData>> : CacheData; }; export type BeforeUpdateHookSync>> = (key: string, dependencyKey: string, ...initialParameter: CacheGetterParams) => void; export type BeforeUpdateHookAsync>> = (key: string, dependencyKey: string, ...initialParameter: CacheGetterParams) => Promise; export type BeforeUpdateHook>> = BeforeUpdateHookSync | BeforeUpdateHookAsync; export interface CacheEntanglementConstructorOption>> { /** * The dependencies of the cache value. * The key of the object is the name of the dependency, and the value is the CacheEntanglement instance. * The dependency cache value is passed to the creation function as the second parameter. */ dependencies?: D; /** * A hook that is called before the cache value is updated. * This hook is called before the creation function is called. * You can use this hook to update the dependency cache values before the creation function is called. * @param key The key of the cache value to be updated. * @param dependencyKey The key of the dependency cache value to be updated. * @param initialParameter The parameter of the cache creation function passed when creating the instance. */ beforeUpdateHook?: BeforeUpdateHook; /** * The capacity of the cache. * If the number of cache values exceeds this value, the least recently used cache values will be removed. */ capacity?: number; } export declare abstract class CacheEntanglement>> { protected readonly creation: G; protected readonly beforeUpdateHook: BeforeUpdateHook; protected readonly capacity: number; protected readonly dependencies: D; protected readonly caches: LRUMap>>>; protected readonly parameters: Map>; protected readonly assignments: CacheEntanglement[]; protected readonly dependencyProperties: (keyof D)[]; protected readonly updateRequirements: Set; constructor(creation: G, option?: CacheEntanglementConstructorOption); protected abstract recache(key: string): Deferred>> | undefined>; protected abstract resolve(key: string, ...parameter: CacheGetterParams): Deferred>>>; protected bubbleUpdateSignal(key: string): void; protected dependencyKey(key: string): string; /** * Returns all keys stored in the instance. */ keys(): IterableIterator; /** * Deletes all cache values stored in the instance. */ clear(): void; /** * Checks if there is a cache value stored in the key within the instance. * @param key The key to search. */ exists(key: string): boolean; /** * Checks if there is a cache value stored in the key within the instance. * This method is an alias for `exists`. * @param key The key to search. */ has(key: string): boolean; /** * Deletes the cache value stored in the key within the instance. * @param key The key to delete. */ delete(key: string): void; /** * Returns the cache value stored in the key within the instance. If the cached value is not present, an error is thrown. * @param key The key to search. */ abstract get(key: string): Deferred>>>; /** * Returns the cache value of the key assigned to the instance. * If the cached value is not present, the creation function is called, and the returned value is cached. * And this value is returned. * @param key The key value of the cache value. This value must be unique within the instance. * @param parameter The parameter of the cache creation function passed when creating the instance. */ abstract cache(key: string, ...parameter: CacheGetterParams): Deferred>>>; /** * Re-calls the creation function passed when creating the instance and stores the returned value in the cache again. * And this value is returned. This is used to forcefully update the value. * @param key The key value of the cache value. This value must be unique within the instance. * @param parameter The parameter of the cache creation function passed when creating the instance. */ abstract update(key: string, ...parameter: CacheGetterParams): Deferred>>>; } export {};