Eventful.d.ts 1.5 KB

1234567891011121314151617181920212223
  1. import { Dictionary, WithThisType } from './types';
  2. export declare type EventCallbackSingleParam<EvtParam = any> = EvtParam extends any ? (params: EvtParam) => boolean | void : never;
  3. export declare type EventCallback<EvtParams = any[]> = EvtParams extends any[] ? (...args: EvtParams) => boolean | void : never;
  4. export declare type EventQuery = string | Object;
  5. declare type CbThis<Ctx, Impl> = unknown extends Ctx ? Impl : Ctx;
  6. declare type DefaultEventDefinition = Dictionary<EventCallback<any[]>>;
  7. export interface EventProcessor<EvtDef = DefaultEventDefinition> {
  8. normalizeQuery?: (query: EventQuery) => EventQuery;
  9. filter?: (eventType: keyof EvtDef, query: EventQuery) => boolean;
  10. afterTrigger?: (eventType: keyof EvtDef) => void;
  11. }
  12. export default class Eventful<EvtDef extends DefaultEventDefinition = DefaultEventDefinition> {
  13. private _$handlers;
  14. protected _$eventProcessor: EventProcessor<EvtDef>;
  15. constructor(eventProcessors?: EventProcessor<EvtDef>);
  16. on<Ctx, EvtNm extends keyof EvtDef>(event: EvtNm, handler: WithThisType<EvtDef[EvtNm], CbThis<Ctx, this>>, context?: Ctx): this;
  17. on<Ctx, EvtNm extends keyof EvtDef>(event: EvtNm, query: EventQuery, handler: WithThisType<EvtDef[EvtNm], CbThis<Ctx, this>>, context?: Ctx): this;
  18. isSilent(eventName: keyof EvtDef): boolean;
  19. off(eventType?: keyof EvtDef, handler?: Function): this;
  20. trigger<EvtNm extends keyof EvtDef>(eventType: EvtNm, ...args: Parameters<EvtDef[EvtNm]>): this;
  21. triggerWithContext(type: keyof EvtDef, ...args: any[]): this;
  22. }
  23. export {};