UNPKG

fluidstate

Version:

Library for fine-grained reactivity state management

92 lines (86 loc) 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.untrackRemotes = exports.untrack = exports.setTrackingDerivation = exports.isTrackingDerivation = exports.isTracking = exports.getTrackingDerivation = void 0; var _reactiveLayer = require("./reactive-layer"); var _reactiveRemotesData = require("./reactive-remotes-data"); var _recursiveUtils = require("./recursive-utils"); let trackingDerivation = null; /** * Sets the current derivation (reaction or computed atom) that is being tracked. * * @internal * @param newTrackingDerivation The new derivation to track, or `null` to stop tracking. */ const setTrackingDerivation = newTrackingDerivation => { trackingDerivation = newTrackingDerivation; }; /** * Gets the current derivation (reaction or computed atom) that is being tracked. * * @internal * @returns The currently tracked derivation, or `null` if none. */ exports.setTrackingDerivation = setTrackingDerivation; const getTrackingDerivation = () => { return trackingDerivation; }; /** * Checks if fluidstate is currently tracking dependencies within a local derivation. * * This is determined by `fluidstate`s internal tracking of the current * `trackingDerivation` (i.e., whether we are inside a reaction or computed * atom's execution). * * @internal * @returns `true` if a local derivation is being tracked, `false` otherwise. */ exports.getTrackingDerivation = getTrackingDerivation; const isTrackingDerivation = () => { return !!trackingDerivation; }; /** * Checks if any reactive instance (local or remote) is currently tracking dependencies. * * This function determines if a reactive context is active by checking both the * local `fluidstate` instance (via `isTrackingDerivation`) and any connected * reactive remotes. * * Dependency tracking occurs during the execution of derivations like computed * atoms or reactions. * * @returns `true` if any reactive instance is tracking, `false` otherwise. */ exports.isTrackingDerivation = isTrackingDerivation; const isTracking = () => isTrackingDerivation() || [..._reactiveRemotesData.reactiveRemotes.values()].some(reactiveRemote => reactiveRemote.isTracking()); exports.isTracking = isTracking; const untrackRemotes = action => { return (0, _recursiveUtils.applyRecursive)([..._reactiveRemotesData.reactiveRemotes.values()].map(remote => remote.untrack), action); }; /** * Executes the provided `action` function without tracking any reactive * dependencies within its scope. This applies to `fluidstate`'s own tracking, * the underlying reactive layer, and all registered reactive remotes. * * This is useful when you need to read reactive values inside a reactive * context (like a computed atom or reaction) without establishing a * dependency on those values. * * @param action - The function to execute without dependency tracking. * @returns The result of the `action` function. */ exports.untrackRemotes = untrackRemotes; const untrack = action => { const previousDerivation = getTrackingDerivation(); setTrackingDerivation(null); try { return (0, _reactiveLayer.getReactiveLayer)().untrack(() => { return untrackRemotes(action); }); } finally { setTrackingDerivation(previousDerivation); } }; exports.untrack = untrack; //# sourceMappingURL=reactive-tracking.js.map