Options
All
  • Public
  • Public/Protected
  • All
Menu

A Observable is a value that can be observed for changes.

This observation can happen in two forms: Watchers and Observers.

Watchers

Watchers are simple functions that get called when the observable value changes.

They receives the new and old value of the observable as arguments.

const observable = new Observable(20);

observable.watch((value, oldValue) => {
 console.log(value, oldValue);
});

observable.update(15); // output: 15 20

Observers

Observers are ComputedObservables.

When the observable changes then the observers are updated.

const observable = new Observable(20);
const computedObservable = new ComputedObservable(() => observable.value * 2);
computedObservable.update(computedObservable.evaluate());

console.log(computedObservable.value); // output: 40

observable.observe(computedObservable);

// Since the computed observable is now 'observed' it will get updated when the observable changes.
observable.update(15);

console.log(computedObservable.value); // output: 30

Type parameters

  • T

    Any valid javascript value.

Hierarchy

Implements

Index

Constructors

constructor

Properties

Protected _observers

_observers: ComputedObservable<any>[] = []

List of ComputedObservables that need to be updated when value changes.

Protected _watchers

_watchers: WatcherFunction<T>[] = []

List of WatcherFunctions that get run when the observable value changes.

Accessors

value

  • get value(): T

Methods

Protected _invokeWatchers

  • _invokeWatchers(value: T | undefined, oldValue: T | undefined): void
  • Safely invoke the watchers registered on this observable.

    Parameters

    • value: T | undefined

      New value of the observer.

    • oldValue: T | undefined

      Old value of the observer.

    Returns void

Protected _updateObservers

  • _updateObservers(): void

observe

unobserve

unwatch

update

  • update(value: T): void

watch

Generated using TypeDoc