thorish
Version:
This is a library of useful JS concepts and data structures for Node and the browser. It it, unashamedly, a dumping ground for code needed by [@samthor](https://twitter.com/samthor)'s projects.
56 lines (55 loc) • 1.88 kB
TypeScript
/**
* Map-like structure which allows fetching of unique {@link V} for a tuple-type {@link K}.
*
* This unique {@link V} is garbage-collected when no longer referenced.
*
* Each part of {@link K} must be identity comparable, as they are stored in intermediate {@link Map} instances.
* (There's probably a different class for where the key requires an e.g., `isEqual(...)` method or similar.)
*/
export declare class WeakIdentityCache<K extends [...any], V extends WeakKey> {
#private;
constructor(
/**
* Builds a {@link V}.
*/
build: (...k: K) => V,
/**
* Called on GC of the assoicated {@link V}.
*/
cleanup?: (...k: K) => void);
/**
* Provides a {@link V} for the given {@link K}.
* This allows you to skip the builder, however, this {@link K} will still be passed to the cleanup function when it is GC'ed.
*
* If this {@link K} already has a value, rejects the update and returns `false`.
* Otherwise, returns `true`.
*/
provide(v: V, ...k: K): boolean;
/**
* Gets this {@link K} from the cache.
*
* Builds the underlying {@link V} if it does not exist.
*/
get(...k: K): V;
/**
* Retrieves all live {@link V} instances under the given {@link K} prefix.
*/
all(...k: Partial<K>): [K, V][];
/**
* Deletes this {@link K} from the cache.
* Returns `true` if it existed and was deleted.
*
* This will prevent the given {@link K} from being passed to the cleanup function.
*/
delete(...k: K): boolean;
/**
* Gets the current size of actual values.
*
* This is useful for testing but might change as things are GC'ed.
*/
get size(): number;
}
/**
* Builds a helper which provides shared named symbols.
*/
export declare function buildScopedSymbolCache(id: string): (s: string) => Symbol;