UNPKG

fluidstate-alien

Version:

Alien Signals-based reactive layer for fluidstate

60 lines (59 loc) 3.05 kB
/** * Note: Much of this file is a copy of alien signals' higher-level abstractions. * The biggest differences from it are: * - Removal of the effect scopes abstraction - it is not needed for `fluidstate` * - Replacement of the signals abstraction with the atom abstraction (i.e. a signal * without value) * - Adding the ability to schedule effects * - Adding the ability to have "onBecomeObservedListener" and * "onBecomeUnobservedListener" listeners on atoms and computeds * - Adding the ability to customize `equals` check for computeds * and replacing the `===` default equals check with `Object.is` */ import { createReactiveSystem, ReactiveNode, ReactiveFlags, Link } from "alien-signals"; import { AtomOptions, ComputedAtomOptions, ReactionOptions } from "fluidstate"; export { AtomOptions, ComputedAtomOptions, ReactionOptions, createReactiveSystem, ReactiveNode, ReactiveFlags, Link, }; export declare enum AtomType { Atom = 0, Computed = 1, Effect = 2 } export interface Effect extends ReactiveNode { isInitialized: boolean; isStopped: boolean; atomType: AtomType.Effect; fn(): void; computed: Computed<void>; options?: ReactionOptions; } export interface Computed<T = any> extends ReactiveNode { atomType: AtomType.Computed; value: T | undefined; getter: (previousValue?: T) => T; watchCount: number; options?: ComputedAtomOptions; } export interface Atom extends ReactiveNode { atomType: AtomType.Atom; watchCount: number; options?: AtomOptions; } export declare const isComputed: (node: ReactiveNode) => node is Computed<unknown>; export declare const isEffect: (node: ReactiveNode) => node is Effect; export declare const isAtom: (node: ReactiveNode) => node is Atom; export declare const link: (dep: ReactiveNode, sub: ReactiveNode) => void, unlink: (link: Link, sub?: ReactiveNode) => Link | undefined, propagate: (link: Link) => void, checkDirty: (link: Link, sub: ReactiveNode) => boolean, endTracking: (sub: ReactiveNode) => void, startTracking: (sub: ReactiveNode) => void, shallowPropagate: (link: Link) => void; export declare let batchDepth: number; export declare function getCurrentSub(): ReactiveNode | undefined; export declare function setCurrentSub(sub: ReactiveNode | undefined): ReactiveNode | undefined; export declare function startBatch(): void; export declare function endBatch(): void; export declare function atom(options?: AtomOptions): Atom; export declare const computedCore: <T>(getter: (previousValue?: T) => T, options?: ComputedAtomOptions) => { computed: Computed<T>; get: () => T; }; export declare function computed<T>(getter: (previousValue?: T) => T, options?: ComputedAtomOptions): () => T; export declare function effect(fn: () => void, options?: ReactionOptions): () => void; export declare function runInitialEffect(e: Effect): void; export declare function reportChanged(this: Atom): void; export declare function reportObserved(this: Atom): boolean;