@zedux/atoms
Version:
A Molecular State Engine for React
120 lines (119 loc) • 5.52 kB
TypeScript
import { AtomSelectorOrConfig, Cleanup, DependentCallback, DependentEdge, EvaluationReason, Selectable } from '../types/index.js';
import { Ecosystem } from './Ecosystem.js';
export declare class SelectorCache<T = any, Args extends any[] = any[]> {
id: string;
selectorRef: AtomSelectorOrConfig<T, Args>;
args?: Args | undefined;
static $$typeof: symbol;
isDestroyed?: boolean;
isMaterialized?: boolean;
nextReasons: EvaluationReason[];
prevReasons?: EvaluationReason[];
result?: T;
task?: () => void;
_lastEdge?: WeakRef<DependentEdge>;
_prevCache?: WeakRef<SelectorCache>;
constructor(id: string, selectorRef: AtomSelectorOrConfig<T, Args>, args?: Args | undefined);
}
/**
* Since AtomSelectors are meant to feel lightweight, they don't have to be
* instances of a class - they'll often be standalone or even inline
* functions. This class handles all the logic that AtomSelectors would handle
* themselves if they were classes - creation, cache management, and
* destruction.
*/
export declare class Selectors {
private readonly ecosystem;
/**
* Map selectorKey + params id strings to the SelectorCache for the selector
*/
_items: Record<string, SelectorCache<any, any>>;
/**
* A workaround for React StrictMode
*/
_lastCache?: WeakRef<SelectorCache<any, any>>;
/**
* Map selectors (or selector config objects) to a base selectorKey that can
* be used to predictably create selectorKey+params ids to look up the cache
* in `this._items`
*/
_refBaseKeys: WeakMap<AtomSelectorOrConfig<any, any>, string>;
constructor(ecosystem: Ecosystem);
addDependent(cacheItem: SelectorCache<any, any>, { callback, operation, }?: {
callback?: DependentCallback;
operation?: string;
}): Cleanup;
/**
* Get an object mapping all ids in this selectorCache to their current
* values.
*
* Pass a selector to only return caches of that selector.
*
* Pass a partial SelectorCache id string to only return caches whose id
* contains the passed key (case-insensitive).
*
* IMPORTANT: Don't use this for SSR. SelectorCaches are not designed to be
* shared across environments. Selectors should be simple derivations that
* will be predictably recreated from rehydrated atom instances.
*
* In other words, `ecosystem.dehydrate()` is all you need for SSR. Don't
* worry about selectors. This method is solely an inspection/debugging util.
*/
dehydrate(selectableOrName?: Selectable<any, any> | string): Record<string, SelectorCache<any, any[]>>;
destroyCache<T = any, Args extends [] = []>(selectable: Selectable<T, Args>): void;
destroyCache<T = any, Args extends any[] = []>(selectable: Selectable<T, Args>, args: Args, force?: boolean): void;
/**
* Get the cache for the given selector. Return undefined if it doesn't exist
* yet - don't create it.
*/
find<T = any, Args extends [] = []>(selectable: Selectable<T, Args>): SelectorCache<T, Args> | undefined;
find<T = any, Args extends any[] = []>(selectable: Selectable<T, Args>, args: Args): SelectorCache<T, Args> | undefined;
find<T = any, Args extends any[] = any[]>(selectable: string): SelectorCache<T, Args> | undefined;
/**
* Get an object of all currently-cached AtomSelectors.
*
* Pass a selector reference or string to filter by caches whose id
* weakly matches the passed selector name.
*/
findAll(selectableOrName?: Selectable<any, any> | string): Record<string, SelectorCache<any, any[]>>;
getCache<T = any, Args extends [] = []>(selectable: Selectable<T, Args>): SelectorCache<T, Args>;
getCache<T = any, Args extends any[] = []>(selectable: Selectable<T, Args>, args: Args): SelectorCache<T, Args>;
getCacheId<T = any, Args extends [] = []>(selectorOrConfig: AtomSelectorOrConfig<T, Args>): string;
getCacheId<T = any, Args extends any[] = []>(selectorOrConfig: AtomSelectorOrConfig<T, Args>, args: Args): string;
getCacheId<T = any, Args extends any[] = []>(selectorOrConfig: AtomSelectorOrConfig<T, Args>, args: Args, weak: true): string | undefined;
/**
* Should only be used internally. Removes the selector from the cache and
* the graph
*/
_destroySelector(id: string): void;
/**
* Get the string key we would ideally use as the id of the given
* AtomSelector function or AtomSelectorConfig object - doesn't necessarily
* mean we end up caching using this key.
*/
_getIdealCacheId(selectorOrConfig: AtomSelectorOrConfig<any, any>): string | undefined;
/**
* Should only be used internally
*/
_scheduleEvaluation(id: string, reason: EvaluationReason, shouldSetTimeout?: boolean): void;
/**
* Should only be used internally
*/
_swapRefs<T, Args extends any[]>(oldCache: SelectorCache<T, Args>, newRef: AtomSelectorOrConfig<T, Args>, args?: any[]): void;
/**
* Destroy all cached selectors. Should probably only be used internally.
* Prefer `ecosystem.reset()`.
*/
_wipe(): void;
/**
* Get a base key that can be used to generate consistent ids for the given
* selector
*/
private getBaseKey;
/**
* Run an AtomSelector and, depending on the selector's resultsComparator,
* update its cached result. Updates the graph efficiently (using
* `.bufferUpdates()`)
*/
private runSelector;
}