123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- /**
- Emittery accepts strings and symbols as event names.
- Symbol event names can be used to avoid name collisions when your classes are extended, especially for internal events.
- */
- type EventName = string | symbol;
- /**
- Emittery also accepts an array of strings and symbols as event names.
- */
- type EventNames = EventName | readonly EventName[];
- declare class Emittery {
- /**
- In TypeScript, it returns a decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the target class.
- @example
- ```
- import Emittery = require('emittery');
- @Emittery.mixin('emittery')
- class MyClass {}
- const instance = new MyClass();
- instance.emit('event');
- ```
- */
- static mixin(emitteryPropertyName: string | symbol, methodNames?: readonly string[]): Function;
- /**
- Fires when an event listener was added.
- An object with `listener` and `eventName` (if `on` or `off` was used) is provided as event data.
- @example
- ```
- import Emittery = require('emittery');
- const emitter = new Emittery();
- emitter.on(Emittery.listenerAdded, ({listener, eventName}) => {
- console.log(listener);
- //=> data => {}
- console.log(eventName);
- //=> '๐ฆ'
- });
- emitter.on('๐ฆ', data => {
- // Handle data
- });
- ```
- */
- static readonly listenerAdded: unique symbol;
- /**
- Fires when an event listener was removed.
- An object with `listener` and `eventName` (if `on` or `off` was used) is provided as event data.
- @example
- ```
- import Emittery = require('emittery');
- const emitter = new Emittery();
- const off = emitter.on('๐ฆ', data => {
- // Handle data
- });
- emitter.on(Emittery.listenerRemoved, ({listener, eventName}) => {
- console.log(listener);
- //=> data => {}
- console.log(eventName);
- //=> '๐ฆ'
- });
- off();
- ```
- */
- static readonly listenerRemoved: unique symbol;
- /**
- Subscribe to one or more events.
- Using the same listener multiple times for the same event will result in only one method call per emitted event.
- @returns An unsubscribe method.
- @example
- ```
- import Emittery = require('emittery');
- const emitter = new Emittery();
- emitter.on('๐ฆ', data => {
- console.log(data);
- });
- emitter.on(['๐ฆ', '๐ถ'], data => {
- console.log(data);
- });
- emitter.emit('๐ฆ', '๐'); // log => '๐' x2
- emitter.emit('๐ถ', '๐'); // log => '๐'
- ```
- */
- on(eventName: typeof Emittery.listenerAdded | typeof Emittery.listenerRemoved, listener: (eventData: Emittery.ListenerChangedData) => void): Emittery.UnsubscribeFn
- on(eventName: EventNames, listener: (eventData?: unknown) => void): Emittery.UnsubscribeFn;
- /**
- Get an async iterator which buffers data each time an event is emitted.
- Call `return()` on the iterator to remove the subscription.
- @example
- ```
- import Emittery = require('emittery');
- const emitter = new Emittery();
- const iterator = emitter.events('๐ฆ');
- emitter.emit('๐ฆ', '๐1'); // Buffered
- emitter.emit('๐ฆ', '๐2'); // Buffered
- iterator
- .next()
- .then(({value, done}) => {
- // done === false
- // value === '๐1'
- return iterator.next();
- })
- .then(({value, done}) => {
- // done === false
- // value === '๐2'
- // Revoke subscription
- return iterator.return();
- })
- .then(({done}) => {
- // done === true
- });
- ```
- In practice you would usually consume the events using the [for await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) statement. In that case, to revoke the subscription simply break the loop.
- @example
- ```
- import Emittery = require('emittery');
- const emitter = new Emittery();
- const iterator = emitter.events('๐ฆ');
- emitter.emit('๐ฆ', '๐1'); // Buffered
- emitter.emit('๐ฆ', '๐2'); // Buffered
- // In an async context.
- for await (const data of iterator) {
- if (data === '๐2') {
- break; // Revoke the subscription when we see the value `๐2`.
- }
- }
- ```
- It accepts multiple event names.
- @example
- ```
- import Emittery = require('emittery');
- const emitter = new Emittery();
- const iterator = emitter.events(['๐ฆ', '๐ฆ']);
- emitter.emit('๐ฆ', '๐1'); // Buffered
- emitter.emit('๐ฆ', '๐2'); // Buffered
- iterator
- .next()
- .then(({value, done}) => {
- // done === false
- // value === '๐1'
- return iterator.next();
- })
- .then(({value, done}) => {
- // done === false
- // value === '๐2'
- // Revoke subscription
- return iterator.return();
- })
- .then(({done}) => {
- // done === true
- });
- ```
- */
- events(eventName: EventNames): AsyncIterableIterator<unknown>
- /**
- Remove one or more event subscriptions.
- @example
- ```
- import Emittery = require('emittery');
- const emitter = new Emittery();
- const listener = data => console.log(data);
- (async () => {
- emitter.on(['๐ฆ', '๐ถ', '๐ฆ'], listener);
- await emitter.emit('๐ฆ', 'a');
- await emitter.emit('๐ถ', 'b');
- await emitter.emit('๐ฆ', 'c');
- emitter.off('๐ฆ', listener);
- emitter.off(['๐ถ', '๐ฆ'], listener);
- await emitter.emit('๐ฆ', 'a'); // nothing happens
- await emitter.emit('๐ถ', 'b'); // nothing happens
- await emitter.emit('๐ฆ', 'c'); // nothing happens
- })();
- ```
- */
- off(eventName: EventNames, listener: (eventData?: unknown) => void): void;
- /**
- Subscribe to one or more events only once. It will be unsubscribed after the first
- event.
- @returns The event data when `eventName` is emitted.
- @example
- ```
- import Emittery = require('emittery');
- const emitter = new Emittery();
- emitter.once('๐ฆ').then(data => {
- console.log(data);
- //=> '๐'
- });
- emitter.once(['๐ฆ', '๐ถ']).then(data => {
- console.log(data);
- });
- emitter.emit('๐ฆ', '๐'); // Logs `๐` twice
- emitter.emit('๐ถ', '๐'); // Nothing happens
- ```
- */
- once(eventName: typeof Emittery.listenerAdded | typeof Emittery.listenerRemoved): Promise<Emittery.ListenerChangedData>
- once(eventName: EventNames): Promise<unknown>;
- /**
- Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
- @returns A promise that resolves when all the event listeners are done. *Done* meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
- */
- emit(eventName: EventName, eventData?: unknown): Promise<void>;
- /**
- Same as `emit()`, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer `emit()` whenever possible.
- If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will *not* be called.
- @returns A promise that resolves when all the event listeners are done.
- */
- emitSerial(eventName: EventName, eventData?: unknown): Promise<void>;
- /**
- Subscribe to be notified about any event.
- @returns A method to unsubscribe.
- */
- onAny(listener: (eventName: EventName, eventData?: unknown) => unknown): Emittery.UnsubscribeFn;
- /**
- Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
- Call `return()` on the iterator to remove the subscription.
- In the same way as for `events`, you can subscribe by using the `for await` statement.
- @example
- ```
- import Emittery = require('emittery');
- const emitter = new Emittery();
- const iterator = emitter.anyEvent();
- emitter.emit('๐ฆ', '๐1'); // Buffered
- emitter.emit('๐', '๐2'); // Buffered
- iterator.next()
- .then(({value, done}) => {
- // done is false
- // value is ['๐ฆ', '๐1']
- return iterator.next();
- })
- .then(({value, done}) => {
- // done is false
- // value is ['๐', '๐2']
- // revoke subscription
- return iterator.return();
- })
- .then(({done}) => {
- // done is true
- });
- ```
- */
- anyEvent(): AsyncIterableIterator<unknown>
- /**
- Remove an `onAny` subscription.
- */
- offAny(listener: (eventName: EventName, eventData?: unknown) => void): void;
- /**
- Clear all event listeners on the instance.
- If `eventName` is given, only the listeners for that event are cleared.
- */
- clearListeners(eventName?: EventNames): void;
- /**
- The number of listeners for the `eventName` or all events if not specified.
- */
- listenerCount(eventName?: EventNames): number;
- /**
- Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
- @example
- ```
- import Emittery = require('emittery');
- const object = {};
- new Emittery().bindMethods(object);
- object.emit('event');
- ```
- */
- bindMethods(target: object, methodNames?: readonly string[]): void;
- }
- declare namespace Emittery {
- /**
- Removes an event subscription.
- */
- type UnsubscribeFn = () => void;
- type EventNameFromDataMap<EventDataMap> = Extract<keyof EventDataMap, EventName>;
- /**
- Maps event names to their emitted data type.
- */
- interface Events {
- // Blocked by https://github.com/microsoft/TypeScript/issues/1863, should be
- // `[eventName: EventName]: unknown;`
- }
- /**
- The data provided as `eventData` when listening for `Emittery.listenerAdded` or `Emittery.listenerRemoved`.
- */
- interface ListenerChangedData {
- /**
- The listener that was added or removed.
- */
- listener: (eventData?: unknown) => void;
- /**
- The name of the event that was added or removed if `.on()` or `.off()` was used, or `undefined` if `.onAny()` or `.offAny()` was used.
- */
- eventName?: EventName;
- }
- /**
- Async event emitter.
- You must list supported events and the data type they emit, if any.
- @example
- ```
- import Emittery = require('emittery');
- const emitter = new Emittery.Typed<{value: string}, 'open' | 'close'>();
- emitter.emit('open');
- emitter.emit('value', 'foo\n');
- emitter.emit('value', 1); // TS compilation error
- emitter.emit('end'); // TS compilation error
- ```
- */
- class Typed<EventDataMap extends Events, EmptyEvents extends EventName = never> extends Emittery {
- on<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, listener: (eventData: EventDataMap[Name]) => void): Emittery.UnsubscribeFn;
- on<Name extends EmptyEvents>(eventName: Name, listener: () => void): Emittery.UnsubscribeFn;
- events<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name): AsyncIterableIterator<EventDataMap[Name]>;
- once<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name): Promise<EventDataMap[Name]>;
- once<Name extends EmptyEvents>(eventName: Name): Promise<void>;
- off<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, listener: (eventData: EventDataMap[Name]) => void): void;
- off<Name extends EmptyEvents>(eventName: Name, listener: () => void): void;
- onAny(listener: (eventName: EventNameFromDataMap<EventDataMap> | EmptyEvents, eventData?: EventDataMap[EventNameFromDataMap<EventDataMap>]) => void): Emittery.UnsubscribeFn;
- anyEvent(): AsyncIterableIterator<[EventNameFromDataMap<EventDataMap>, EventDataMap[EventNameFromDataMap<EventDataMap>]]>;
- offAny(listener: (eventName: EventNameFromDataMap<EventDataMap> | EmptyEvents, eventData?: EventDataMap[EventNameFromDataMap<EventDataMap>]) => void): void;
- emit<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
- emit<Name extends EmptyEvents>(eventName: Name): Promise<void>;
- emitSerial<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
- emitSerial<Name extends EmptyEvents>(eventName: Name): Promise<void>;
- }
- }
- export = Emittery;
|