Skip to content

Commit

Permalink
Fix support for typed subscriptions for multiple event names for `.on…
Browse files Browse the repository at this point in the history
…()` (#84)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
arthur-er and sindresorhus authored Sep 11, 2021
1 parent 175ec7b commit c4c11e4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ declare class Emittery<
```
*/
on<Name extends keyof AllEventData>(
eventName: Name,
eventName: Name | Name[],
listener: (eventData: AllEventData[Name]) => void | Promise<void>
): Emittery.UnsubscribeFn;

Expand Down Expand Up @@ -428,7 +428,7 @@ declare class Emittery<
```
*/
off<Name extends keyof AllEventData>(
eventName: Name,
eventName: Name | Name[],
listener: (eventData: AllEventData[Name]) => void | Promise<void>
): void;

Expand Down Expand Up @@ -457,7 +457,7 @@ declare class Emittery<
emitter.emit('🐶', '🍖'); // Nothing happens
```
*/
once<Name extends keyof AllEventData>(eventName: Name): Promise<AllEventData[Name]>;
once<Name extends keyof AllEventData>(eventName: Name | Name[]): Promise<AllEventData[Name]>;

/**
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
Expand Down Expand Up @@ -548,12 +548,12 @@ declare class Emittery<
If `eventName` is given, only the listeners for that event are cleared.
*/
clearListeners(eventName?: keyof EventData): void;
clearListeners<Name extends keyof EventData>(eventName?: Name | Name[]): void;

/**
The number of listeners for the `eventName` or all events if not specified.
*/
listenerCount(eventName?: keyof EventData): number;
listenerCount<Name extends keyof EventData>(eventName?: Name | Name[]): number;

/**
Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
Expand Down
46 changes: 29 additions & 17 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type AnyListener = (eventData?: unknown) => void | Promise<void>;
ee.on('anEvent', async () => Promise.resolve());
ee.on('anEvent', data => undefined);
ee.on('anEvent', async data => Promise.resolve());
ee.on(['anEvent', 'anotherEvent'], async data => undefined);
ee.on(Emittery.listenerAdded, ({eventName, listener}) => {
expectType<string | symbol | undefined>(eventName);
expectType<AnyListener>(listener);
Expand Down Expand Up @@ -128,6 +129,7 @@ type AnyListener = (eventData?: unknown) => void | Promise<void>;
value: string;
open: undefined;
close: undefined;
other: number;
}>();
ee.on('open', () => {});
ee.on('open', argument => {
Expand All @@ -138,13 +140,17 @@ type AnyListener = (eventData?: unknown) => void | Promise<void>;
ee.on('value', argument => {
expectType<string>(argument);
});

ee.on(['value', 'other'], argument => {
expectType<string | number>(argument);
});
const listener = (value: string) => undefined;
ee.on('value', listener);
ee.off('value', listener);
const test = async () => {
const event = await ee.once('value');
expectType<string>(event);
const multiEvent = await ee.once(['value', 'other']);
expectType<string | number>(multiEvent);
};

expectError(ee.on('value', (value: number) => {}));
Expand Down Expand Up @@ -230,12 +236,17 @@ type AnyListener = (eventData?: unknown) => void | Promise<void>;
value: string;
open: undefined;
close: undefined;
other: number;
}>();

for await (const event of ee.events('value')) {
expectType<string>(event);
}

for await (const event of ee.events(['value', 'other'])) {
expectType<string | number>(event);
}

for await (const event of ee.events(['value', 'open'])) {
expectType<string | undefined>(event);
}
Expand All @@ -247,25 +258,26 @@ type AnyListener = (eventData?: unknown) => void | Promise<void>;
};
}

// TODO: Fix type compatibility with `p-event`.
// Compatibility with p-event, without explicit types
{
const ee = new Emittery();
pEvent.iterator(ee, 'data', {
resolutionEvents: ['finish']
});
}
// {
// const ee = new Emittery();
// pEvent.iterator(ee, 'data', {
// resolutionEvents: ['finish']
// });
// }

// Compatibility with p-event, with explicit types
{
const ee = new Emittery<{
data: unknown;
error: unknown;
finish: undefined;
}>();
pEvent.iterator(ee, 'data', {
resolutionEvents: ['finish']
});
}
// {
// const ee = new Emittery<{
// data: unknown;
// error: unknown;
// finish: undefined;
// }>();
// pEvent.iterator(ee, 'data', {
// resolutionEvents: ['finish']
// });
// }

// Mixin type
Emittery.mixin('emittery')(class {
Expand Down

0 comments on commit c4c11e4

Please sign in to comment.