Uneventful
    Preparing search index...

    Return a singleton instance for the given factory, or create a per-signal lazy constant (a bit like React's useMemo, but without the deps or ordering constraints).

    Expression Returns Behavior
    $(() => T | new () => T) T Get or make a singleton instance
    $``(() => T | new () => T) T Return a per-signal lazy constant

    In complex programs and frameworks, it's often beneficial to both 1) have a single access point for some functionality, and 2) not to need a specific point where that access is explicitly initialized. The $() function lets you unobtrusively request a singleton instance to be instantiated on demand, then shared with all other access points in the program, and it does so without requiring any change to the target class or classes. (You can even override the target instance or replace its factory, as one might with a dependency-injection container.)

    Within functions, there's often a need to have some state that carries across multiple calls to the function. (Like what other languages do with function-static variables.)

    You can do something like this with a closure, of course, but it often increases code complexity, especially when writing signal functions. So the lazy-constant operator ($``()) lets you write expressions like const myMap = $``(WeakMap<...>) instead of needing to initialize (or at least define) myMap outside the signal function body.

    • Experimental

      Return a singleton instance for the given factory

      Every call to $() with a given factory will return the same result. (Unless overridden using $cache.set, $cache.unset or $cache.replace.) On first use, the factory is called (or constructed, if it's a class) and the result (if not an error) is cached for future calls.

      Type Parameters

      • T

      Parameters

      • factory: (() => T) | (new () => T)

      Returns T

    • Experimental

      Create a per-signal lazy constant, via $``()

      When you call $``(factory) inside a given signal function for the first time, factory() will be called (or constructed, if it's a class) and returned, and the result cached for future calls at the same location in that specific signal. An error results if called outside a signal function.

      The primary difference between this and the singleton operator (plain $()), is that lazy constants are singletons per call-site, per signal. A specific invocation of $``() in a specific signal will always return the same value.

      Parameters

      • callSite: TemplateStringsArray

      Returns <T>(factory: (() => T) | (new () => T)) => T

      Lazy constants are somewhat similar in concept to a React useMemo(), but also very different. React requires hooks to always be invoked in the same order to match them up with their targets, but lazy constants do not need this: they're tied to the line of code where they're called, and you can branch, loop, skip, or call them out of order with no consequence. (They'll just always return the same value each time if running in the same signal - even in a loop or called in another function with different arguments, and they don't support dependencies because you can use signals instead.)

      (Also unlike React hooks, they can also be used in nested functions, as long as those functions are only invoked from within a relevant signal. Conversely, because they're keyed to a specific code location, you can't just call a wrapping function more than once in a signal, and expect to get different results: a lazy constant is a per-signal constant, not a React hook!)