Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for typed subscriptions for multiple event names for .on() #84

Merged
merged 8 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,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 @@ -264,7 +264,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 @@ -292,7 +292,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 @@ -383,12 +383,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 @@ -93,6 +94,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 @@ -103,13 +105,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 @@ -195,12 +201,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 @@ -212,25 +223,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