A function, or class constructor producing a value that will be cached by
$() and service() accessors for the life of the root job, or until
overridden or unset via $cache.
The function or constructor will be run in the then-current root job, and its
value cached. If the function is a native generator function, it is
automatically wrapped with fork(), so that its job will not end when
the generator ends, and the result can be waited on by multiple callers. (If
your factory is not a native generator function but still returns a
generator to produce an async result, you should wrap that generator with
fork before returning it.)
Note: if an original or replacement factory (set via
$cache.replace()) directly or indirectly requests its own value (even
via a service() accessor), an error is thrown to prevent infinite
recursion. An error is also thrown if a reactive value is directly or
indirectly accessed during the factory's execution, to prevent your cached
singleton from unintentionally retaining a stale value. (Rules and
fx() are unaffected, as they will update their
state as needed.)
A function, or class constructor producing a value that will be cached by
$()andservice()accessors for the life of the root job, or until overridden or unset via$cache.The function or constructor will be run in the then-current root job, and its value cached. If the function is a native generator function, it is automatically wrapped with fork(), so that its job will not end when the generator ends, and the result can be waited on by multiple callers. (If your factory is not a native generator function but still returns a generator to produce an async result, you should wrap that generator with fork before returning it.)
Note: if an original or replacement factory (set via $cache.replace()) directly or indirectly requests its own value (even via a service() accessor), an error is thrown to prevent infinite recursion. An error is also thrown if a reactive value is directly or indirectly accessed during the factory's execution, to prevent your cached singleton from unintentionally retaining a stale value. (Rules and fx() are unaffected, as they will update their state as needed.)