BetaCreate 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.)
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.)
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.
BetaCreate a parameterized reactive effect that tracks separate dependencies, jobs, and caching state for each object it's called on.
The type of object the effect will be applied to
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.
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.
Create or invoke a reactive effect.
fx(() => {})() => voidfx``(() => {})() => voidfx(ob => () => {})ob => voidfx``(ob => () => {})ob => void@fx method(): voidvoidReactive 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:
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).
fx``(...)is shorthand for$``(() => fx(...))[3]. That is, it acts just like any other call tofx, 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. ↩︎Both TC39 decorators and "legacy" TypeScript/Babel decorators are supported. ↩︎
$``()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. ↩︎