Skip to main content

SelectorCache

Every time an ecosystem's .selectors manager caches an atom selector, the "cached selector" is really just an instance of this class. Thus, an instance of this class is what's returned from various Selectors class methods like .getCache() and .findAll().

This class has no special functionality. It just stores data. It has to be a class (not any old object) so that Zedux can detect instances of it for certain APIs.

Example

Live Sandbox
1234567
const ecosystem = createEcosystem({ id: 'selector-cache-item-example' })
const numbersAtom = atom('numbers', () => [1, 2, 3, 4, 5, 6, 7])

const getOddNumbers = ({ get }: AtomGetters) =>
get(numbersAtom).filter(num => num % 2)

const cacheItem = ecosystem.selectors.getCache(getOddNumbers)

Properties

args

An array or undefined if this selector wasn't given args. This array contains references to the actual args passed, in the order passed.

id

A string. The id Zedux uses to refer to this SelectorCache in the Selectors class and the ecosystem graph.

Contains a deterministic hash of all the selector's args and a name generated from the selector's function name if possible or the AtomSelectorConfig's .name property if given.

isDestroyed

A boolean or undefined. Cached atom selectors don't have a real lifecycle like atom instances. This is all they get.

This will be set to true when the selector's ref count goes from 1 to 0 and Zedux will discard all references to this object, allowing it to be garbage collected. If you're ever using an instance of this class where isDestroyed is true, you're doing something wrong. Consider using manual graphing to prevent automatic destruction and handle force destruction of SelectorCaches you're using outside React/atoms.

nextReasons

An array that tracks the batched EvaluationReasons that will lead to the selector reevaluating.

If the selector is currently evaluating, this is the list of reasons explaining why and is what ecosystem.why() will return.

This will be empty if no reevaluation is currently scheduled or if this is the first evaluation of the selector.

prevReasons

An array that tracks the batched EvaluationReasons describing why the selector last evaluated. This will be empty if the last evaluation was the first.

result

The cached return value of the atom selector. Selectors don't use stores, this mutable value just sits right here.

selectorRef

A reference to the actual selector function or AtomSelectorConfig object used to generate this selector cache item

See Also