Class Realm

The realm is the actual "engine" that orchestrates any cells and signals that it touches. The realm also stores the state and the dependencies of the nodes that are referred through it.

Constructors

  • Creates a new realm.

    Parameters

    • initialValues: Record<symbol, unknown> = {}

      the initial cell values that will populate the realm. Those values will not trigger a recomputation cycle, and will overwrite the initial values specified for each cell.

    Returns Realm

Methods

  • Creates or resolves an existing cell instance in the realm. Useful as a joint point when building your own operators.

    Type Parameters

    • T

    Parameters

    • value: T

      the initial value of the cell

    • distinct: Distinct<T> = true

      true by default. Pass false to mark the signal as a non-distinct one, meaning that publishing the same value multiple times will re-trigger a recomputation cycle.

    • node: symbol = ...

      optional, a reference to a cell. If the cell has not been touched in the realm before, the realm will instantiate a reference to it. If it's registered already, the function will return the reference.

    Returns NodeRef<T>

    a reference to the cell.

  • Convenient for mutation of cells that contian non-primitive values (e.g. arrays, or objects). Specifies that the cell value should be changed when source emits, with the result of the map callback parameter. the map parameter gets called with the current value of the cell and the value published through the source.

    Type Parameters

    • T

      the type of the cell value.

    • K

      the type of the value published through the source.

    Parameters

    • cell: NodeRef<T>
    • source: NodeRef<K>
    • map: ((cellValue, signalValue) => T)
        • (cellValue, signalValue): T
        • Parameters

          • cellValue: T
          • signalValue: K

          Returns T

    Returns void

    Example

    const items$ = Cell<string[]([])
    const addItem$ = Signal<string>(false, (r) => {
    r.changeWith(items$, addItem$, (items, item) => [...items, item])
    })
    const r = new Realm()
    r.pub(addItem$, 'foo')
    r.pub(addItem$, 'bar')
    r.getValue(items$) // ['foo', 'bar']
  • A low-level utility that connects multiple nodes to a sink node with a map function. Used as a foundation for the higher-level operators. The nodes can be active (sources) or passive (pulls).

    Type Parameters

    • T extends unknown[] = unknown[]

    Parameters

    • __namedParameters: {
          map: ProjectionFunc<T>;
          pulls?: NodeRef<unknown>[];
          sink: NodeRef<unknown>;
          sources: NodeRef<unknown>[];
      }
      • map: ProjectionFunc<T>

        The projection function that will be called when any of the source nodes emits.

      • Optional pulls?: NodeRef<unknown>[]

        The nodes which values will be pulled. The values will be passed as arguments to the map function.

      • sink: NodeRef<unknown>

        The sink node that will receive the result of the map function.

      • sources: NodeRef<unknown>[]

        The source nodes that emit values to the sink node. The values will be passed as arguments to the map function.

    Returns void

  • Gets the current value of a node. The node must be stateful.

    Type Parameters

    • T

    Parameters

    • node: NodeRef<T>

      the node instance.

    Returns T

    Remark

    if possible, use withLatestFrom or combine, as getValue will not create a dependency to the passed node, which means that if you call it within a computational cycle, you may not get the correct value.

    Example

    const foo$ = Cell('foo')

    const r = new Realm()
    r.getValue(foo$) // 'foo'
    r.pub(foo$, 'bar')
    //...
    r.getValue(foo$) // 'bar'
  • Links the output of a node to the input of another node.

    Type Parameters

    • T

    Parameters

    Returns void

  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T

    Parameters

    Returns NodeRef<T>

    Example

    const signal$ = Signal<number>(true, (r) => {
    const signalPlusOne$ = r.pipe(signal$, map(i => i + 1))
    r.sub(signalPlusOne$, console.log)
    })
    const r = new Realm()
    r.pub(signal$, 1)
  • Type Parameters

    • T

    • O1

    Parameters

    Returns NodeRef<O1>

  • Type Parameters

    • T

    • O1

    • O2

    Parameters

    • s: NodeRef<T>
    • Rest ...o: [O<T, O1>, O<O1, O2>]

    Returns NodeRef<O2>

  • Type Parameters

    • T

    • O1

    • O2

    • O3

    Parameters

    • s: NodeRef<T>
    • Rest ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>]

    Returns NodeRef<O3>

  • Type Parameters

    • T

    • O1

    • O2

    • O3

    • O4

    Parameters

    • s: NodeRef<T>
    • Rest ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>]

    Returns NodeRef<O4>

  • Type Parameters

    • T

    • O1

    • O2

    • O3

    • O4

    • O5

    Parameters

    • s: NodeRef<T>
    • Rest ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>, O<O4, O5>]

    Returns NodeRef<O5>

  • Type Parameters

    • T

    • O1

    • O2

    • O3

    • O4

    • O5

    • O6

    Parameters

    • s: NodeRef<T>
    • Rest ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>, O<O4, O5>, O<O5, O6>]

    Returns NodeRef<O6>

  • Type Parameters

    • T

    • O1

    • O2

    • O3

    • O4

    • O5

    • O6

    • O7

    Parameters

    • s: NodeRef<T>
    • Rest ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>, O<O4, O5>, O<O5, O6>, O<O6, O7>]

    Returns NodeRef<O7>

  • Type Parameters

    • T

    • O1

    • O2

    • O3

    • O4

    • O5

    • O6

    • O7

    • O8

    Parameters

    • s: NodeRef<T>
    • Rest ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>, O<O4, O5>, O<O5, O6>, O<O6, O7>, O<O7, O8>]

    Returns NodeRef<O8>

  • Type Parameters

    • T

    • O1

    • O2

    • O3

    • O4

    • O5

    • O6

    • O7

    • O8

    • O9

    Parameters

    • s: NodeRef<T>
    • Rest ...o: [O<T, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>, O<O4, O5>, O<O5, O6>, O<O6, O7>, O<O7, O8>, O<O8, O9>]

    Returns NodeRef<O9>

  • Type Parameters

    • T

    Parameters

    • source: NodeRef<T>
    • Rest ...operators: O<unknown, unknown>[]

    Returns NodeRef<unknown>

  • Runs the subscrptions of this node.

    Type Parameters

    • T

    Parameters

    Returns void

    Example

    const foo$ = Action((r) => {
    r.sub(foo$, console.log)
    })

    const r = new Realm()
    r.pub(foo$)
  • Publishes the specified value into a node.

    Type Parameters

    • T

    Parameters

    Returns void

    Example

    const foo$ = Cell('foo')
    const r = new Realm()
    r.pub(foo$, 'bar')
  • Publishes into multiple nodes simultaneously, triggering a single re-computation cycle.

    Parameters

    • values: Record<symbol, unknown>

      a record of node references and their values.

    Returns void

    Example

    const foo$ = Cell('foo')
    const bar$ = Cell('bar')

    const r = new Realm()
    r.pubIn({[foo$]: 'foo1', [bar$]: 'bar1'})
  • Explicitly includes the specified cell/signal reference in the realm. Most of the time you don't need to do that, since any interaction with the node through a realm will register it. The only exception of that rule should be when the interaction is conditional, and the node definition includes an init function that needs to be eagerly evaluated.

    Parameters

    Returns NodeRef<any>

  • Clears all exclusive subscriptions.

    Returns void

  • Creates or resolves an existing signal instance in the realm. Useful as a joint point when building your own operators.

    Type Parameters

    • T

    Parameters

    • distinct: Distinct<T> = true

      true by default. Pass false to mark the signal as a non-distinct one, meaning that publishing the same value multiple times will re-trigger a recomputation cycle.

    • node: symbol = ...

      optional, a reference to a signal. If the signal has not been touched in the realm before, the realm will instantiate a reference to it. If it's registered already, the function will return the reference.

    Returns NodeRef<T>

    a reference to the signal.

  • Subscribes exclusively to values in the referred node. Calling this multiple times on a single node will remove the previous subscription created through singletonSub. Subscriptions created through sub are not affected.

    Type Parameters

    • T

    Parameters

    Returns UnsubscribeHandle

    a function that, when called, will cancel the subscription.

    Example

    const signal$ = Signal<number>()
    const r = new Realm()
    // console.log will run only once.
    r.singletonSub(signal$, console.log)
    r.singletonSub(signal$, console.log)
    r.singletonSub(signal$, console.log)
    r.pub(signal$, 2)
  • Subscribes to the values published in the referred node.

    Type Parameters

    • T

    Parameters

    • node: NodeRef<T>

      the cell/signal to subscribe to.

    • subscription: Subscription<T>

      the callback to execute when the node receives a new value.

    Returns UnsubscribeHandle

    a function that, when called, will cancel the subscription.

    Example

    const signal$ = Signal<number>()
    const r = new Realm()
    const unsub = r.sub(signal$, console.log)
    r.pub(signal$, 2)
    unsub()
    r.pub(signal$, 3)
  • Works as a reverse pipe. Constructs a function, that, when passed a certain node (sink), will create a node that will work as a publisher through the specified pipes into the sink.

    Type Parameters

    • In

    Parameters

    • Rest ...o: []

    Returns ((s) => NodeRef<In>)

      • (s): NodeRef<In>
      • Works as a reverse pipe. Constructs a function, that, when passed a certain node (sink), will create a node that will work as a publisher through the specified pipes into the sink.

        Parameters

        Returns NodeRef<In>

        Example

        const foo$ = Cell('foo')
        const bar$ = Cell('bar')
        const entry$ = Signal<number>(true, (r) => {
        const transform = r.transformer(map(x: number => `num${x}`))
        const transformFoo$ = transform(foo$)
        const transformBar$ = transform(bar$)
        r.link(entry$, transformFoo$)
        r.link(entry$, transformBar$)
        })

        const r = new Realm()
        r.pub(entry$, 1) // Both foo$ and bar$ now contain `num1`

    Example

    const foo$ = Cell('foo')
    const bar$ = Cell('bar')
    const entry$ = Signal<number>(true, (r) => {
    const transform = r.transformer(map(x: number => `num${x}`))
    const transformFoo$ = transform(foo$)
    const transformBar$ = transform(bar$)
    r.link(entry$, transformFoo$)
    r.link(entry$, transformBar$)
    })

    const r = new Realm()
    r.pub(entry$, 1) // Both foo$ and bar$ now contain `num1`
  • Type Parameters

    • In

    • Out

    Parameters

    • Rest ...o: [O<In, Out>]

    Returns ((s) => NodeRef<In>)

  • Type Parameters

    • In

    • Out

    • O1

    Parameters

    • Rest ...o: [O<In, O1>, O<O1, Out>]

    Returns ((s) => NodeRef<In>)

  • Type Parameters

    • In

    • Out

    • O1

    • O2

    Parameters

    • Rest ...o: [O<In, O1>, O<O1, O2>, O<O2, Out>]

    Returns ((s) => NodeRef<In>)

  • Type Parameters

    • In

    • Out

    • O1

    • O2

    • O3

    Parameters

    • Rest ...o: [O<In, O1>, O<O1, O2>, O<O2, O3>, O<O3, Out>]

    Returns ((s) => NodeRef<In>)

  • Type Parameters

    • In

    • Out

    • O1

    • O2

    • O3

    • O4

    Parameters

    • Rest ...o: [O<In, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>, O<O4, Out>]

    Returns ((s) => NodeRef<In>)

  • Type Parameters

    • In

    • Out

    • O1

    • O2

    • O3

    • O4

    • O5

    Parameters

    • Rest ...o: [O<In, O1>, O<O1, O2>, O<O2, O3>, O<O3, O4>, O<O4, O5>, O<O5, Out>]

    Returns ((s) => NodeRef<In>)

  • Type Parameters

    • In

    • Out

    Parameters

    • Rest ...operators: O<unknown, unknown>[]

    Returns ((s) => NodeRef<In>)

Generated using TypeDoc