Documentation - v7.17.0
    Preparing search index...

    Class Observable<T>

    Minimal synchronous publish/subscribe primitive.

    Observers are called synchronously, in subscription order, each time Observable.notify is invoked. An optional onFirstSubscribe callback lets the observable lazily set up its underlying source (e.g. a DOM listener) when the first observer subscribes; the function it returns, if any, is called to tear that source down when the last observer unsubscribes.

    const resize = new Observable<UIEvent>((observable) => {
    const { stop } = addEventListener(window, DOM_EVENT.RESIZE, (event) => observable.notify(event))
    return stop
    })
    const subscription = resize.subscribe((event) => console.log(event))
    subscription.unsubscribe() // last observer: the listener is removed

    Type Parameters

    • T

    Hierarchy (View Summary)

    Index
    • Creates an observable with no observers.

      Type Parameters

      • T

      Parameters

      • OptionalonFirstSubscribe: (observable: Observable<T>) => void | (() => void)

        Called when the observer count goes from 0 to 1. May return a teardown function, called when the observer count goes back to 0.

      Returns Observable<T>

    observers: Observer<T>[] = []

    Observers currently subscribed, in subscription order.

    • Adds observer to the observer list, triggering onFirstSubscribe if it is the first one.

      Parameters

      • observer: Observer<T>

        The observer to add.

      Returns void

    • Removes observer from the observer list, calling the teardown returned by onFirstSubscribe if it was the last one.

      Parameters

      • observer: Observer<T>

        The observer to remove.

      Returns void