Function Subscription

Creates a Subscription object that allows subscribing, unsubscribing, notifying and resetting subscribers.

initialSubscribers is copied, so the array you pass is never mutated and later changes to it have no effect. notify calls the subscribers registered when it started, each once, even if one unsubscribes itself or another during the call; subscribers added during the call wait for the next notify.

const subscription = Subscription();
const subscriber = (data: string) => console.log(data);
subscription.subscribe(subscriber);
subscription.notify('Hello, world!'); // 'Hello, world!'
subscription.unsubscribe(subscriber);
subscription.notify('Hello, world!'); // (nothing)
  • Type Parameters

    • V

    Parameters

    • initialSubscribers: Subscriber<V>[] = []

    Returns {
        notify: (data: V) => void;
        reset: () => void;
        subscribe: (subscriber: Subscriber<V>) => void;
        unsubscribe: (subscriber: Subscriber<V>) => void;
    }