Function DerivedCell

  • Defines a new stateful node, links it to an existing node transform 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.

    • linkFn: (r: Realm, cell: NodeRef<T>) => NodeRef<T>

      an function that will be called when the node is registered in a realm. Should return a node reference to link to.

    • 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>

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