Function Cell

  • Defines a new stateful node and returns a reference to it. Once a realm instance publishes or subscribes to the node, an instance of that node it will be registered in the realm.

    Type Parameters

    • T

    Parameters

    • value: T

      the initial value of the node. Stateful nodes always have a value.

    • init: ((r) => void) = noop

      an optional function that will be called when the node is registered in a realm. Can be used to create subscriptions and define relationships to other nodes. Any referred nodes will be registered in the realm automatically.

        • (r): void
        • Parameters

          Returns void

    • distinct: Distinct<T> = true

      if true, the node will only emit values that are different from the previous value. Optionally, a custom distinct function can be provided if the node values are non-primitive.

    Returns NodeRef<T>

    Example

    const foo$ = Cell('foo', true, (r) => {
    r.sub(foo$, console.log)
    })
    const r = new Realm()
    r.pub(foo$, 'bar') // the subscription will log 'bar'

    Remarks

    Unlike the RxJS BehaviorSubject, a stateful node does not immediately invoke its subscriptions when subscribed to. It only emits values when you publish something in it, either directly or through its relationships. If you need to get the current value of a stateful node, use Realm.getValue.

Generated using TypeDoc