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

    Returns void

    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.

      • Optionalpulls?: 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

    Returns T

    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.

    const foo$ = Cell('foo')

    const r = new Realm()
    r.getValue(foo$) // 'foo'
    r.pub(foo$, 'bar')
    //...
    r.getValue(foo$) // 'bar'
  • Type Parameters

    • T

    Parameters

    • fn: () => T

    Returns T

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

    Type Parameters

    • T

    Parameters

    Returns NodeRef<T>

    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
  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T
    • O1

    Parameters

    Returns NodeRef<O1>

    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
  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T
    • O1
    • O2

    Parameters

    Returns NodeRef<O2>

    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
  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T
    • O1
    • O2
    • O3

    Parameters

    Returns NodeRef<O3>

    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
  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T
    • O1
    • O2
    • O3
    • O4

    Parameters

    Returns NodeRef<O4>

    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
  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T
    • O1
    • O2
    • O3
    • O4
    • O5

    Parameters

    Returns NodeRef<O5>

    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
  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T
    • O1
    • O2
    • O3
    • O4
    • O5
    • O6

    Parameters

    Returns NodeRef<O6>

    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
  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T
    • O1
    • O2
    • O3
    • O4
    • O5
    • O6
    • O7

    Parameters

    Returns NodeRef<O7>

    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
  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T
    • O1
    • O2
    • O3
    • O4
    • O5
    • O6
    • O7
    • O8

    Parameters

    Returns NodeRef<O8>

    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
  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T
    • O1
    • O2
    • O3
    • O4
    • O5
    • O6
    • O7
    • O8
    • O9

    Parameters

    Returns NodeRef<O9>

    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
  • Creates a new node that emits the values of the source node transformed through the specified operators.

    Type Parameters

    • T

    Parameters

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

    Returns NodeRef<unknown>

    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
  • Runs the subscrptions of this node.

    Type Parameters

    • T

    Parameters

    Returns void

    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

    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

    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.

    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.

    const signal$ = Signal<number>()
    const r = new Realm()
    const unsub = r.sub(signal$, console.log)
    r.pub(signal$, 2)
    unsub()
    r.pub(signal$, 3)
  • Subscribes to multiple nodes at once. If any of the nodes emits a value, the subscription will be called with an array of the latest values from each node. If the nodes change within a single execution cycle, the subscription will be called only once with the final node values.

    Type Parameters

    • T1

    Parameters

    Returns UnsubscribeHandle

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

    const trigger$ = Signal<number>(true, (r) => {
    r.link(r.pipe(trigger$, map(i => `foo${i}`)), foo$)
    r.link(r.pipe(trigger$, map(i => `bar${i}`)), bar$)
    })

    const r = new Realm()
    r.subMultiple([foo$, bar$], ([foo, bar]) => console.log(foo, bar))
    r.pub(trigger$, 2)
  • Subscribes to multiple nodes at once. If any of the nodes emits a value, the subscription will be called with an array of the latest values from each node. If the nodes change within a single execution cycle, the subscription will be called only once with the final node values.

    Type Parameters

    • T1
    • T2

    Parameters

    Returns UnsubscribeHandle

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

    const trigger$ = Signal<number>(true, (r) => {
    r.link(r.pipe(trigger$, map(i => `foo${i}`)), foo$)
    r.link(r.pipe(trigger$, map(i => `bar${i}`)), bar$)
    })

    const r = new Realm()
    r.subMultiple([foo$, bar$], ([foo, bar]) => console.log(foo, bar))
    r.pub(trigger$, 2)
  • Subscribes to multiple nodes at once. If any of the nodes emits a value, the subscription will be called with an array of the latest values from each node. If the nodes change within a single execution cycle, the subscription will be called only once with the final node values.

    Type Parameters

    • T1
    • T2
    • T3

    Parameters

    Returns UnsubscribeHandle

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

    const trigger$ = Signal<number>(true, (r) => {
    r.link(r.pipe(trigger$, map(i => `foo${i}`)), foo$)
    r.link(r.pipe(trigger$, map(i => `bar${i}`)), bar$)
    })

    const r = new Realm()
    r.subMultiple([foo$, bar$], ([foo, bar]) => console.log(foo, bar))
    r.pub(trigger$, 2)
  • Subscribes to multiple nodes at once. If any of the nodes emits a value, the subscription will be called with an array of the latest values from each node. If the nodes change within a single execution cycle, the subscription will be called only once with the final node values.

    Type Parameters

    • T1
    • T2
    • T3
    • T4

    Parameters

    Returns UnsubscribeHandle

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

    const trigger$ = Signal<number>(true, (r) => {
    r.link(r.pipe(trigger$, map(i => `foo${i}`)), foo$)
    r.link(r.pipe(trigger$, map(i => `bar${i}`)), bar$)
    })

    const r = new Realm()
    r.subMultiple([foo$, bar$], ([foo, bar]) => console.log(foo, bar))
    r.pub(trigger$, 2)
  • Subscribes to multiple nodes at once. If any of the nodes emits a value, the subscription will be called with an array of the latest values from each node. If the nodes change within a single execution cycle, the subscription will be called only once with the final node values.

    Type Parameters

    • T1
    • T2
    • T3
    • T4
    • T5

    Parameters

    Returns UnsubscribeHandle

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

    const trigger$ = Signal<number>(true, (r) => {
    r.link(r.pipe(trigger$, map(i => `foo${i}`)), foo$)
    r.link(r.pipe(trigger$, map(i => `bar${i}`)), bar$)
    })

    const r = new Realm()
    r.subMultiple([foo$, bar$], ([foo, bar]) => console.log(foo, bar))
    r.pub(trigger$, 2)
  • Subscribes to multiple nodes at once. If any of the nodes emits a value, the subscription will be called with an array of the latest values from each node. If the nodes change within a single execution cycle, the subscription will be called only once with the final node values.

    Type Parameters

    • T1
    • T2
    • T3
    • T4
    • T5
    • T6

    Parameters

    Returns UnsubscribeHandle

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

    const trigger$ = Signal<number>(true, (r) => {
    r.link(r.pipe(trigger$, map(i => `foo${i}`)), foo$)
    r.link(r.pipe(trigger$, map(i => `bar${i}`)), bar$)
    })

    const r = new Realm()
    r.subMultiple([foo$, bar$], ([foo, bar]) => console.log(foo, bar))
    r.pub(trigger$, 2)
  • Subscribes to multiple nodes at once. If any of the nodes emits a value, the subscription will be called with an array of the latest values from each node. If the nodes change within a single execution cycle, the subscription will be called only once with the final node values.

    Type Parameters

    • T1
    • T2
    • T3
    • T4
    • T5
    • T6
    • T7

    Parameters

    Returns UnsubscribeHandle

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

    const trigger$ = Signal<number>(true, (r) => {
    r.link(r.pipe(trigger$, map(i => `foo${i}`)), foo$)
    r.link(r.pipe(trigger$, map(i => `bar${i}`)), bar$)
    })

    const r = new Realm()
    r.subMultiple([foo$, bar$], ([foo, bar]) => console.log(foo, bar))
    r.pub(trigger$, 2)
  • Subscribes to multiple nodes at once. If any of the nodes emits a value, the subscription will be called with an array of the latest values from each node. If the nodes change within a single execution cycle, the subscription will be called only once with the final node values.

    Type Parameters

    • T1
    • T2
    • T3
    • T4
    • T5
    • T6
    • T7
    • T8

    Parameters

    Returns UnsubscribeHandle

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

    const trigger$ = Signal<number>(true, (r) => {
    r.link(r.pipe(trigger$, map(i => `foo${i}`)), foo$)
    r.link(r.pipe(trigger$, map(i => `bar${i}`)), bar$)
    })

    const r = new Realm()
    r.subMultiple([foo$, bar$], ([foo, bar]) => console.log(foo, bar))
    r.pub(trigger$, 2)
  • Subscribes to multiple nodes at once. If any of the nodes emits a value, the subscription will be called with an array of the latest values from each node. If the nodes change within a single execution cycle, the subscription will be called only once with the final node values.

    Parameters

    Returns UnsubscribeHandle

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

    const trigger$ = Signal<number>(true, (r) => {
    r.link(r.pipe(trigger$, map(i => `foo${i}`)), foo$)
    r.link(r.pipe(trigger$, map(i => `bar${i}`)), bar$)
    })

    const r = new Realm()
    r.subMultiple([foo$, bar$], ([foo, bar]) => console.log(foo, bar))
    r.pub(trigger$, 2)
  • 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

    • ...o: []

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

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

    Parameters

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

    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`
  • 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
    • Out
    • O1

    Parameters

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

    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`
  • 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
    • Out
    • O1
    • O2

    Parameters

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

    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`
  • 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
    • Out
    • O1
    • O2
    • O3

    Parameters

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

    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`
  • 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
    • Out
    • O1
    • O2
    • O3
    • O4

    Parameters

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

    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`
  • 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
    • Out
    • O1
    • O2
    • O3
    • O4
    • O5

    Parameters

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

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

    Parameters

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

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

    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`