cache-entanglement
Version:
Manage caches that are dependent on each other efficiently.
31 lines (30 loc) • 1.79 kB
TypeScript
type CacheDataCloneStrategy = 'array-shallow-copy' | 'object-shallow-copy' | 'deep-copy';
type CacheDataCopy<T> = (raw: T) => T;
export declare class CacheData<T> {
protected static readonly StructuredClone: typeof globalThis.structuredClone;
private readonly _value;
constructor(value: T);
/**
* This is cached data.
* It was generated at the time of caching, so there is a risk of modification if it's an object due to shallow copying.
* Therefore, if it's not a primitive type, please avoid using this value directly and use the `clone` method to use a copied version of the data.
*/
get raw(): T;
/**
* The method returns a copied value of the cached data.
* You can pass a function as a parameter to copy the value. This parameter function should return the copied value.
*
* If no parameter is passed, it defaults to using `structuredClone` function to copy the value.
* If you prefer shallow copying instead of deep copying,
* you can use the default options `array-shallow-copy`, `object-shallow-copy` and `deep-copy`,
* which are replaced with functions to shallow copy arrays and objects, respectively. This is a syntactic sugar.
* @param strategy The function that returns the copied value.
* If you want to perform a shallow copy, simply pass the strings `array-shallow-copy` or `object-shallow-copy` for easy use.
* The `array-shallow-copy` strategy performs a shallow copy of an array.
* The `object-shallow-copy` strategy performs a shallow copy of an object.
* The `deep-copy` strategy performs a deep copy of the value using `structuredClone`.
* The default is `deep-copy`.
*/
clone(strategy?: CacheDataCloneStrategy | CacheDataCopy<T>): T;
}
export {};