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 1 commit
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
16 changes: 12 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ type DatalessEventNames<EventData> = {

declare const listenerAdded: unique symbol;
declare const listenerRemoved: unique symbol;
type OmnipresentEventData = {[listenerAdded]: Emittery.ListenerChangedData; [listenerRemoved]: Emittery.ListenerChangedData};
type OmnipresentEventData = {
[listenerAdded]: Emittery.ListenerChangedData;
[listenerRemoved]: Emittery.ListenerChangedData;
};

/**
Emittery is a strictly typed, fully async EventEmitter implementation. Event listeners can be registered with `on` or `once`, and events can be emitted with `emit`.
Expand Down Expand Up @@ -149,7 +152,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 @@ -292,7 +295,9 @@ declare class Emittery<
emitter.emit('🐶', '🍖'); // Nothing happens
```
*/
once<Name extends keyof AllEventData>(eventName: Name): Promise<AllEventData[Name]>;
once<Name extends keyof AllEventData>(
eventName: Name
): Promise<AllEventData[Name]>;
arthur-er marked this conversation as resolved.
Show resolved Hide resolved

/**
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
Expand Down Expand Up @@ -404,7 +409,10 @@ declare class Emittery<
object.emit('event');
```
*/
bindMethods(target: Record<string, unknown>, methodNames?: readonly string[]): void;
bindMethods(
target: Record<string, unknown>,
methodNames?: readonly string[]
): void;
}

declare namespace Emittery {
Expand Down
1 change: 1 addition & 0 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