Interface Val<TValue>

A Val contains a writable value property and a set method.

interface Val<TValue = any> {
    $version: unknown;
    brand: symbol;
    get: () => TValue;
    set: (value: TValue) => void;
    value: TValue;
    dispose(): void;
    reaction(subscriber: ValSubscriber<TValue>, eager?: boolean): ValDisposer;
    ref(): ReadonlyVal<TValue>;
    ref(writable: true): Val<TValue>;
    ref(writable?: boolean): Val<TValue> | ReadonlyVal<TValue>;
    ref(writable?: false): ReadonlyVal<TValue>;
    subscribe(subscriber: ValSubscriber<TValue>, eager?: boolean): ValDisposer;
    unsubscribe(subscriber?: (...args: any[]) => any): void;
}

Type Parameters

  • TValue = any

Hierarchy (View Summary)

Properties

$version: unknown

A version representation of the value. If two versions of a val is not equal(Object.is), it means the value has changed (event if the value is equal).

brand: symbol
get: () => TValue

Get current value of the val.

set: (value: TValue) => void

Set new value

value: TValue

Current value of the val

Methods

  • Create a new ReadonlyVal referencing the value of the current ReadonlyVal as source. (It is just like derive a val without transform. It is simpler hence more efficient.) All ref ReadonlyVals share the same value from the source ReadonlyVal.

    With this pattern you can pass a ref ReadonlyVal to downstream. The ref ReadonlyVals can be safely disposed without affecting the source ReadonlyVal and other ref ReadonlyVals.

    Returns ReadonlyVal<TValue>

  • Create a new Val referencing the value of the current Val as source. All ref Vals share the same value from the source Val. The act of setting a value on the ref Val is essentially setting the value on the source Val.

    With this pattern you can pass a ref Val as a writable Val to downstream. The ref Vals can be safely disposed without affecting the source Val and other ref Vals.

    Parameters

    • writable: true

    Returns Val<TValue>

  • Parameters

    • Optionalwritable: boolean

      If true, creates a new Ref Val referencing the value of the current Val as source. If false, creates a new Ref ReadonlyVal referencing the value of the current Val as source.

    Returns Val<TValue> | ReadonlyVal<TValue>

  • Create a new ReadonlyVal referencing the value of the current ReadonlyVal as source. (It is just like derive a val without transform. It is simpler hence more efficient.) All ref ReadonlyVals share the same value from the source ReadonlyVal.

    With this pattern you can pass a ref ReadonlyVal to downstream. The ref ReadonlyVals can be safely disposed without affecting the source ReadonlyVal and other ref ReadonlyVals.

    Parameters

    • Optionalwritable: false

    Returns ReadonlyVal<TValue>

  • Remove the given subscriber. Remove all if no subscriber provided.

    Parameters

    • Optionalsubscriber: (...args: any[]) => any

    Returns void