UNPKG

@white-matrix/event-emitter

Version:

The Emitter can be used to expose an Event to the public to fire it from the insides.

59 lines (53 loc) 1.58 kB
import { LinkedList } from './linkedList'; export interface IDisposable { dispose(): void; } /** * To an event a function with one or zero parameters * can be subscribed. The event is the subscriber function itself. */ export declare type Event<T> = (listener: (e: T) => any, thisArgs?: any) => IDisposable; declare type Listener<T> = [(e: T) => void, any] | ((e: T) => void); export interface EmitterOptions { onFirstListenerAdd?: Function; onFirstListenerDidAdd?: Function; onListenerDidAdd?: Function; onLastListenerRemove?: Function; } /** * The Emitter can be used to expose an Event to the public * to fire it from the insides. * Sample: class Document { private readonly _onDidChange = new Emitter<(value:string)=>any>(); public onDidChange = this._onDidChange.event; // getter-style // get onDidChange(): Event<(value:string)=>any> { // return this._onDidChange.event; // } private _doIt() { //... this._onDidChange.fire(value); } } */ export declare class Emitter<T> { protected _listeners?: LinkedList<Listener<T>>; private readonly _options?; private _event?; private _deliveryQueue?; constructor(options?: EmitterOptions); private static readonly _noop; /** * For the public to allow to subscribe * to events from this Emitter */ get event(): Event<T>; /** * To be kept private to fire an event to * subscribers */ fire(event: T): void; dispose(): void; } export {};