• Creates a store whose value is computed by the provided function.

    Type Parameters

    • T

    Parameters

    • fn: (() => T)

      computation function that returns the value of the store

        • (): T
        • Returns T

    • options: Omit<StoreOptions<T>, "onUse"> = {}

      store option. Allows to define the equal function, if needed

    Returns ReadableSignal<T>

    store containing the value returned by the computation function

    Remarks

    The computation function is first called the first time the store is used.

    It can use the value of other stores or observables and the computation function is called again if the value of those dependencies changed, as long as the store is still used.

    Dependencies are detected automatically as the computation function gets their value either by calling the stores as a function (as it is possible with stores implementing ReadableSignal), or by calling the get function (with a store or any observable). If some calls made by the function should not be tracked as dependencies, it is possible to wrap them in a call to untrack.

    Note that dependencies can change between calls of the computation function. Internally, tansu will subscribe to new dependencies when they are used and unsubscribe from dependencies that are no longer used after the call of the computation function.

Generated using TypeDoc