UNPKG

@synet/patterns

Version:

Robust, battle-tested collection of stable patterns used in Synet packages

38 lines (37 loc) 1.05 kB
/** * Interface for events that can be emitted * This interface can be extended to create specific event types * with additional properties as needed. * @see /docs/event-emitter.md */ export interface Event { type: string; } /** * Interface for observer objects that want to be notified of specific events */ export interface EventObserver<T extends Event> { update(event: T): void; } /** * EventEmitter that maintains a list of observers and notifies them of events */ export declare class EventEmitter<T extends Event> { private observers; /** * Subscribe to events of a specific type */ subscribe(eventType: string, observer: EventObserver<T>): void; /** * Unsubscribe from events of a specific type */ unsubscribe(eventType: string, observer: EventObserver<T>): void; /** * Emit an event to all subscribed observers */ emit(event: T): void; /** * Check if there are any subscribers for a specific event type */ hasObservers(eventType: string): boolean; }