sub-events
Version:
Lightweight, strongly-typed events, with monitored subscriptions.
64 lines (63 loc) • 1.76 kB
TypeScript
import { IEmitOptions, IEventOptions, ISubscriber, SubEvent } from './event';
/**
* Represents a change in the number of subscriptions, as used with {@link SubEventCount.onCount} event.
*/
export interface ISubCountChange {
/**
* New number of subscriptions.
*/
newCount: number;
/**
* Previous number of subscriptions.
*/
prevCount: number;
}
/**
* Constructor options for {@link SubEventCount} class.
*/
export interface ICountOptions<T> extends IEventOptions<T> {
/**
* Emit options for event {@link SubEventCount.onCount}.
*/
emitOptions?: IEmitOptions;
}
/**
* Extends {@link SubEvent} with event {@link onCount}, to observe the number of subscriptions.
*/
export declare class SubEventCount<T = unknown> extends SubEvent<T> {
/**
* @hidden
*/
protected _notify: (data: ISubCountChange) => SubEvent<ISubCountChange>;
/**
* Triggered on any change in the number of subscriptions.
* @event onCount
*/
readonly onCount: SubEvent<ISubCountChange>;
/**
* Event constructor.
*
* @param options
* Configuration Options.
*/
constructor(options?: ICountOptions<T>);
/**
* Cancels all existing subscriptions for the event.
*
* It overrides the base implementation, to trigger event {@link onCount}
* when there was at least one subscription.
*
* @returns
* Number of subscriptions cancelled.
*
* @see {@link Subscription.cancel}
*/
cancelAll(): number;
/**
* Overrides base implementation, to trigger event {@link onCount} during
* `subscribe` and `cancel` calls.
*
* @hidden
*/
protected _createCancel(sub: ISubscriber<T>): () => void;
}