Uneventful
    Preparing search index...

    Create or invoke a reactive effect.

    Expression Returns Behavior
    fx(() => {}) () => void Create a reactive effect
    fx``(() => {}) () => void Same, but with call site caching[1]
    fx(ob => () => {}) ob => void Create a function that applies a per-object reactive effect
    fx``(ob => () => {}) ob => void Same, but with call site caching[1]
    @fx method(): void void Decorate[2] a method to act as a per-object reactive effect

    Reactive effects are functions that re-run whenever there are changes to the values of the signals they used during their last run.

    Each effect runs in its own job that is restarted when the effect is re-run or is no longer in use. (Effects can thus register cleanup functions with must(), and use any other job-dependent APIs.)

    Effects are similar to rules, but they run immediately the first time they're called (assuming they're observed), and rather than stopping manually (or with their creating job), effects are reference-counted and only stop when they're no longer in use.

    An effect is "in use" if it is:

    1. Called from a rule (or a non-signal job) that has not yet ended, OR
    2. Invoked from another signal or effect that is in use

    When no active rules or jobs or in-use signals are referencing an effect, the effect stops and runs any registered cleanups, closes stream connections, etc.

    Unlike other signals, an effect also does not run at all if it is not "in use". So if you have signals that are being queried but not observed, and they invoke effects, the effects will not actually do anything until or unless the calling signal is observed (e.g. via a rule or another in-use effect).


    1. fx``(...) is shorthand for $``(() => fx(...)) [3]. That is, it acts just like any other call to fx, except that it only runs at most once per code location in a given signal function. The result is then cached and returned for every subsequent call from that specific location in the same signal function. ↩︎

    2. Both TC39 decorators and "legacy" TypeScript/Babel decorators are supported. ↩︎

    3. $``() is the lazy constant operator. It allows for an expression to only be evaluated once during the life of an enclosing signal, by caching the result on its first invocation. ↩︎

    • Beta

      Create a reactive effect (void signal) that runs when observed by a rule (directly or via other reactive functions). (Can also be used as a method decorator via @fx, with either TC39 or "Legacy" decorators.)

      Parameters

      • effect: () => void

        The function that will be called to start (or restart) the effect, if the reactive values it used during its last run have changed since then.

        The effect function is run in a job that will restart if its dependencies change, and when the effect is no longer in use. (So you can use e.g. must() to define rollback actions, and any jobs, subscriptions, etc. you start inside the effect function will likewise be terminated when the effect becomes unobserved or its dependencies change.)

      Returns () => void

      a zero-argument reactive function that can be called from any rule, job, or other observed reactive functions, to start or continue the effect.

      Multiple calls from the same or different observers do not restart the effect; only dependency changes will restart it. If it loses all observers (i.e. fails to be called by any of them, or all the calling jobs end), the effect will stop until it's in use again.

      Note that reactive effects do not actually execute unless they are "in use", i.e. either directly invoked from an active job or called indirectly from a rule or effect that is itself in use.

    • Beta

      Create a parameterized reactive effect that tracks separate dependencies, jobs, and caching state for each object it's called on.

      Type Parameters

      • Instance extends object

        The type of object the effect will be applied to

      Parameters

      • factory: (obj: Instance) => () => void

        A function that will be called with each new instance passed to the returned effect function. It must return a zero-argument effect function, customized to apply the effect to the instance it was given.

      Returns (ob: Instance) => void

      a one-argument reactive effect that is shorthand for looking up and calling a cached, zero-argument reactive effect customized for the given argument.

      That is, given rx = fx(a => () => a.bar()), calling rx(foo) is equivalent to calling fx(() => foo.bar())(), except that each foo gets its own fx(() => foo.bar()) instance cached, so that dependencies can be properly tracked per instance.