Options
All
  • Public
  • Public/Protected
  • All
Menu

Class View<T>

This class implements readonly access to a deeply nested value with in an Atom/Cursor. An optional transformer function can be supplied at creation time to produce a derived/materialized view of the actual value held in the atom.

remarks

Views can be created directly or via the {@link IViewable.addView} method of the parent state. Views can be {@link @thi.ng/api#IDeref.deref}'d like atoms and polled for value changes using IView.changed. The transformer is only applied once per value change and its result cached until the next change.

If the optional lazy is true (default), the transformer will only be executed with the first {@link @thi.ng/api#IDeref.deref} after each value change. If lazy is false, the transformer function will be executed immediately after a value change occurred and so can be used like a watch which only triggers if there was an actual value change (in contrast to normal watches, which execute with each update, regardless of value change).

Related, the actual value change predicate can be customized. If not given, the default equiv will be used.

example
a = defAtom({ a: { b: 1 } });
v = defView(a, ["a", "b"], (x) => x * 10);

v.deref()
// 10

// update atom state
a.resetIn(["a", "b"], 2);
// { a: { b: 2 } }

v.changed()
// true
v.deref()
// 20

// remove view from parent state
v.release()

Type parameters

  • T

Hierarchy

  • View

Implements

Index

Constructors

constructor

  • new View<T>(parent: ReadonlyAtom<any>, path: Path, tx?: Fn<any, T>, lazy?: boolean, equiv?: (a: any, b: any) => boolean): View<T>
  • Type parameters

    • T

    Parameters

    • parent: ReadonlyAtom<any>
    • path: Path
    • Optional tx: Fn<any, T>
    • lazy: boolean = true
    • equiv: (a: any, b: any) => boolean = ...
        • (a: any, b: any): boolean
        • Parameters

          • a: any
          • b: any

          Returns boolean

    Returns View<T>

Properties

Readonly id

id: string

Protected isDirty

isDirty: boolean

Protected isLazy

isLazy: boolean

Readonly parent

parent: ReadonlyAtom<any>

Readonly path

path: Path

Protected state

state: undefined | T

Protected tx

tx: Fn<any, T>

Protected unprocessed

unprocessed: any

Accessors

value

  • get value(): undefined | T

Methods

changed

  • changed(): boolean
  • Returns true, if the view's value has changed since last {@link @thi.ng/api#IDeref.deref}.

    Returns boolean

deref

  • deref(): undefined | T
  • Returns view's value. If the view has a transformer, the transformed value is returned. The transformer is only run once per value change.

    remarks

    See class comments about difference between lazy/eager behaviors.

    Returns undefined | T

release

  • release(): boolean
  • Disconnects this view from parent state, marks itself dirty/changed and sets its unprocessed raw value to undefined.

    Returns boolean

view

  • view(): undefined | T
  • Like {@link @thi.ng/api#IDeref.deref}, but doesn't update view's cached state and dirty flag if value has changed.

    remarks

    If there's an unprocessed value change, returns result of this sub's transformer or else the cached value.

    Important: Use this function only if the view has none or or a stateless transformer. Else might cause undefined/inconsistent behavior when calling view or {@link @thi.ng/api#IDeref.deref} subsequently.

    Returns undefined | T

Generated using TypeDoc