Class WritableSignal<T>

The most basic form of a signal which is just a container for a value providing read and write access to the value and allowing to monitor the value for changes by subscribing to it.

Type Parameters

  • T = unknown

Hierarchy (View Summary)

Constructors

Methods

  • Returns the current signal value version. This version is incremented every time the signal value has really changed. The version starts by 0 and wraps to Number.MIN_SAFE_INTEGER when Number.MAX_SAFE_INTEGER is reached, allowing unique versions for eighteen quadrillion signal updates.

    Returns number

    The current signal version.

  • Checks if signal value is still valid. When signal has no dependencies then this method can always return true. If signal has dependencies then it must check if any dependency has changed or became invalid. If yes, then this method must return false and the signal will be re-validated by calling (). If all dependency are valid/unchanged then this method must return true to indicate that the signal value is still valid.

    Returns boolean

    True if signal value is valid, false if it must be re-validated.

  • Sets a new value. If the new value does not equal the old value then subscribers are informed about the change. By default values are compared with Object.is() but this behavior can be changed by specifying a custom equals method when creating the signal.

    Parameters

    • value: T

      The new value to set.

    Returns this

  • Subscribes the given observer to this object.

    Parameters

    • observer: Observer<T> | (next: T) => void

      The observer to subscribe.

    Returns Subscription

    Object which can be used to unsubscribe the observer.

  • Updates the value. This calls the given updater function with the current value and then just calls set with the value returned by the updater function.

    Parameters

    • updater: (currentValue: T) => T

      The function to call with the current value and which must return the new value.

    Returns this

  • Called when () returned false and the current value of this signal is needed. Must compute/fetch and cache the new value so the next call to () returns a valid value. When signal has no dependencies and () is implemented to always return true then this method can stay empty as it has nothing to validate/update.

    Returns void