Skip to content

Commit

Permalink
fix: static .on returning wrong iterator types
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfrangu committed Aug 21, 2024
1 parent 154c197 commit 77ad774
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable @typescript-eslint/dot-notation */
function validateListener(input: unknown): asserts input is (...args: unknown[]) => void {
if (typeof input !== 'function') {
Expand Down Expand Up @@ -136,10 +137,7 @@ export type AsyncEventEmitterListenerForEvent<

const brandSymbol = Symbol.for('async-event-emitter.ts-brand');

export class AsyncEventEmitter<
// eslint-disable-next-line @typescript-eslint/ban-types
Events extends {} = {},
> {
export class AsyncEventEmitter<Events extends {} = {}> {
/**
* This field doesn't actually exist, it's just a way to make TS properly infer the events from classes that extend AsyncEventEmitter
*/
Expand Down Expand Up @@ -685,10 +683,9 @@ export class AsyncEventEmitter<
}

public static listenerCount<
Emitter extends AsyncEventEmitter<any>,
EventNames = Emitter extends AsyncEventEmitter<infer Events> ? Events : never,
EventName extends PropertyKey = EventNames extends never ? string | symbol : keyof EventNames,
>(emitter: Emitter, eventName: EventName | keyof AsyncEventEmitterPredefinedEvents): number;
EventMap extends {},
EventName extends PropertyKey = keyof EventMap | keyof AsyncEventEmitterPredefinedEvents,
>(emitter: AsyncEventEmitter<EventMap>, eventName: EventName | keyof AsyncEventEmitterPredefinedEvents): number;

public static listenerCount(emitter: AsyncEventEmitter<any>, eventName: string | symbol): number;

Expand All @@ -697,15 +694,13 @@ export class AsyncEventEmitter<
}

public static async once<
Emitter extends AsyncEventEmitter<any>,
EventNames extends Record<PropertyKey, unknown[]> = Emitter extends AsyncEventEmitter<infer Events> ? Events
: Record<PropertyKey, unknown[]>,
EventName extends PropertyKey = keyof EventNames | keyof AsyncEventEmitterPredefinedEvents,
EventMap extends {},
EventName extends PropertyKey = keyof EventMap | keyof AsyncEventEmitterPredefinedEvents,
>(
emitter: Emitter,
emitter: AsyncEventEmitter<EventMap>,
eventName: EventName,
options?: AbortableMethods,
): Promise<GetAsyncEventEmitterEventParameters<Emitter, EventName>>;
): Promise<GetAsyncEventEmitterEventParameters<AsyncEventEmitter<EventMap>, EventName>>;

public static async once(
emitter: AsyncEventEmitter<any>,
Expand Down Expand Up @@ -764,15 +759,13 @@ export class AsyncEventEmitter<
}

public static on<
Emitter extends AsyncEventEmitter<any>,
EventNames extends Record<PropertyKey, unknown[]> = Emitter extends AsyncEventEmitter<infer Events> ? Events
: Record<PropertyKey, unknown[]>,
EventName extends PropertyKey = keyof EventNames | keyof AsyncEventEmitterPredefinedEvents,
EventMap extends {},
EventName extends PropertyKey = keyof EventMap | keyof AsyncEventEmitterPredefinedEvents,
>(
emitter: Emitter,
emitter: AsyncEventEmitter<EventMap>,
eventName: EventName,
options?: AbortableMethods,
): AsyncGenerator<GetAsyncEventEmitterEventParameters<Emitter, EventName>, void>;
): AsyncGenerator<GetAsyncEventEmitterEventParameters<AsyncEventEmitter<EventMap>, EventName>, void>;

public static on(
emitter: AsyncEventEmitter<any>,
Expand Down
8 changes: 8 additions & 0 deletions tests/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ class Extension extends AsyncEventEmitter<Events2> {

this.emit(EventsEnum.Test1, 123);
}

public async selfIterate() {
const iterator = AsyncEventEmitter.on(this, EventsEnum.Test1);

for await (const [bar] of iterator) {
console.log(bar);
}
}
}

declare const extended: Extension;
Expand Down

0 comments on commit 77ad774

Please sign in to comment.