Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Observable<T>

Observable class. Usefull to make a variable calling asynchronous listener when changed.

    const observable = new Observable(1); // create the observable on a number. Inital value set to 1.

const unsub = observable.listen((value: number) => { // add a listener to be called when observed value is changed (observable.set() is called).

// value is equel to the new value of the observable. Here value === 2.
// code

});

// code

observable.set(2); // set the value to 2. All listeners will be called.

// code

unsub(); // unsubscribe the function. The listener wont be called anymore
author

Valentin Vivier

Type parameters

  • T

    Type holded by the observer.

Hierarchy

  • Observable

Index

Constructors

  • Constructor for observable.

    Type parameters

    • T

      Type holded by the observer.

    Parameters

    • value: T

      Initial value to observe.

    Returns Observable<T>

Properties

didUpdate: (a: T, b: T) => boolean = ...

Type declaration

    • (a: T, b: T): boolean
    • Overloadable by extending Observable. Called after change is made.

      Parameters

      • a: T
      • b: T

      Returns boolean

shouldCallListener: (a: T, b: T) => boolean = ...

Type declaration

    • (a: T, b: T): boolean
    • Overloadable by extending Observable. Called after change is made.

      Parameters

      • a: T
      • b: T

      Returns boolean

      If false is returned, listeners will not be called.

willUpdate: (a: T, b: T) => boolean = ...

Type declaration

    • (a: T, b: T): boolean
    • Overloadable by extending Observable. Called before any change is made, can cancel update process.

      Parameters

      • a: T
      • b: T

      Returns boolean

      If false is returned, change is cancel and listeners wont be called.

Methods

  • change(timeout?: number, enforce?: boolean): Promise<T>
  • Async function that resolve as soon as the observed value is changed.

    Parameters

    • timeout: number = 0

      Time before timeout is triggered. If 0, the function will not timeout. Default to 0.

    • enforce: boolean = false

      If true, timeout will throw with Error("Observable: Timeout on change"). If false, timeout will resolve with null. Default to false.

    Returns Promise<T>

    The promise reolving on value change.

  • get(): T
  • listen(callback: (a: T) => void): () => void
  • Listen for nay change in observed value. Callback is called each time value is changed.

    Parameters

    • callback: (a: T) => void

      Callback function called when the observed value change. Called with the new value as only parameter.

        • (a: T): void
        • Parameters

          • a: T

          Returns void

    Returns () => void

    A function to cancel this listener.

      • (): void
      • Returns void

  • set(newValue: T): this
  • Change currently hold value.

    Parameters

    • newValue: T

      Value to set in the observer.

    Returns this

Generated using TypeDoc