UNPKG

@etsoo/shared

Version:

TypeScript shared utilities and functions

96 lines (95 loc) 2.37 kB
/** * Abstract event base class * T for type * D for data */ export declare abstract class EventBase<T, D> { readonly target: {}; readonly type: T; readonly data: D; private _propagationStopped; /** * stopImmediatePropagation called */ get propagationStopped(): boolean; private _timeStamp; /** * Time stamp */ get timeStamp(): number; /** * Constructor * @param type Type */ constructor(target: {}, type: T, data: D); /** * Prevent all other listeners from being called */ stopImmediatePropagation(): void; } /** * Event options */ interface EventOptions { /** * A boolean value indicating that events of this type will be dispatched first */ capture?: boolean; /** * A boolean value indicating that the listener should be invoked at most once after being added */ once?: boolean; } type EventClassDef = { [key: string]: object; }; /** * Event class * T for type * D for data */ export declare abstract class EventClass<D extends EventClassDef> { private readonly listeners; /** * Has specific type events * @param type Type */ hasEvents<T extends keyof D>(type: T): boolean; /** * Has specific type and callback events * @param type Type * @param callback Callback */ hasEvents<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void): boolean; /** * Remove all specific type events * @param type Type */ off<T extends keyof D>(type: T): void; /** * Remove specific type and callback event * @param type Type * @param callback Callback */ off<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void): void; /** * Add event listeners * @param collection Collection of events */ on(collection: { [type in keyof D]: (event: EventBase<type, D[type]>) => void; }): void; /** * Add event listener * @param type Type * @param callback Callback * @param options Options */ on<T extends keyof D>(type: T, callback: (event: EventBase<T, D[T]>) => void, options?: EventOptions): void; /** * Trigger event * @param event Event */ trigger<T extends keyof D>(event: EventBase<T, D[T]>): void; } export {};