UNPKG

@zedux/atoms

Version:

A Molecular State Engine for React

34 lines (33 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.injectAtomGetters = void 0; const EvaluationStack_1 = require("../classes/EvaluationStack"); /** * injectAtomGetters * * An injector that returns an AtomGetters object. The returned AtomGetters * dynamically register dependencies for the current atom instance when called * synchronously during atom evaluation. * * ```ts * const exampleAtom = atom('example', () => { * const { ecosystem, get, getInstance } = injectAtomGetters() * const dynamicVal = get(myAtom) // registers a dynamic graph edge * const instance = getInstance(myAtom) // registers a static graph edge * const fromEcosystem = ecosystem.get(myAtom) // doesn't register anything * * injectEffect(() => { * const dynamicVal2 = get(myAtom) // doesn't register anything * const instance2 = getInstance(myAtom) // doesn't register anything * const fromEcosystem2 = ecosystem.get(myAtom) // doesn't register anything * }, []) // no need to pass AtomGetters as deps; they're stable references * }) * ``` * * @see AtomGetters */ const injectAtomGetters = () => { const instance = (0, EvaluationStack_1.readInstance)(); return instance.ecosystem._evaluationStack.atomGetters; }; exports.injectAtomGetters = injectAtomGetters;