interface EventsMap { [event: string]: any; } interface DefaultEvents extends EventsMap { [event: string]: (...args: any) => void; } export interface Unsubscribe { (): void; } export interface Emitter { /** * Calls each of the listeners registered for a given event. * * ```js * ee.emit('tick', tickType, tickDuration) * ``` * * @param event The event name. * @param args The arguments for listeners. */ emit( this: this, event: K, ...args: Parameters ): void; /** * Event names in keys and arrays with listeners in values. * * ```js * emitter1.events = emitter2.events * emitter2.events = { } * ``` */ events: Partial<{[E in keyof Events]: Events[E][]}>; /** * Add a listener for a given event. * * ```js * const unbind = ee.on('tick', (tickType, tickDuration) => { * count += 1 * }) * * disable () { * unbind() * } * ``` * * @param event The event name. * @param cb The listener function. * @returns Unbind listener from event. */ on(this: this, event: K, cb: Events[K]): Unsubscribe; } /** * Create event emitter. * * ```js * import { createNanoEvents } from 'nanoevents' * * class Ticker { * constructor() { * this.emitter = createNanoEvents() * } * on(...args) { * return this.emitter.on(...args) * } * tick() { * this.emitter.emit('tick') * } * } * ``` */ export function createNanoEvents< Events extends EventsMap = DefaultEvents, >(): Emitter; /** * An interface for mixins that expose the `on` function (without the emitter * bound to `this`) * * ```js * import { createNanoEvents } from 'nanoevents' * * class Ticker implements EmitterMixin { * constructor() { * this.emitter = createNanoEvents() * } * on(...args) { * return this.emitter.on(...args) * } * tick() { * this.emitter.emit('tick') * } * } * ``` */ export interface EmitterMixin { on(event: K, cb: Events[K]): Unsubscribe; }