Function Subscription

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

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>) => Subscriber<V>[]);
    }

    • notify: ((data: V) => void)
        • (data): void
        • Parameters

          • data: V

          Returns void

    • reset: (() => void)
        • (): void
        • Returns void

    • subscribe: ((subscriber: Subscriber<V>) => void)
        • (subscriber): void
        • Parameters

          • subscriber: Subscriber<V>

          Returns void

    • unsubscribe: ((subscriber: Subscriber<V>) => Subscriber<V>[])
        • (subscriber): Subscriber<V>[]
        • Parameters

          • subscriber: Subscriber<V>

          Returns Subscriber<V>[]