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: upgrade to strict-event-emitter@0.4.3 #382

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
node-version: ${{ matrix.node }}

- name: Install dependencies
run: yarn install --frozen-lockfile
run: yarn install --frozen-lockfile --ignore-engines

- name: Unit tests
run: yarn test:internal
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"node": ">=14"
},
"scripts": {
"start": "tsc --build -w",
"start": "tsc -p ./tsconfig.build.json -w",
"test": "yarn test:internal && yarn test:integration",
"test:internal": "jest",
"test:integration": "yarn test:integration:node && yarn test:integration:browser",
"test:integration:node": "jest --c test/jest.node.config.js --runInBand",
"test:integration:browser": "jest --c test/jest.browser.config.js",
"clean": "rimraf lib",
"build": "yarn clean && tsc --build",
"build": "yarn clean && tsc -p ./tsconfig.build.json",
"prepare": "yarn simple-git-hooks init",
"release": "release publish",
"prepublishOnly": "yarn build && yarn test"
Expand Down Expand Up @@ -72,7 +72,7 @@
"debug": "^4.3.3",
"headers-polyfill": "^3.1.0",
"outvariant": "^1.2.1",
"strict-event-emitter": "^0.2.4",
"strict-event-emitter": "^0.4.3",
"web-encoding": "^1.1.5"
},
"keywords": [
Expand Down
16 changes: 8 additions & 8 deletions src/BatchInterceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventMapType } from 'strict-event-emitter'
import { ExtractEventNames, Interceptor } from './Interceptor'
import { FunctionEventMap } from './utils/AsyncEventEmitter'

export interface BatchInterceptorOptions<
InterceptorList extends Interceptor<any>[]
Expand All @@ -8,10 +8,10 @@ export interface BatchInterceptorOptions<
interceptors: InterceptorList
}

export type ExtractEventMapType<InterceptorList extends Interceptor<any>[]> =
export type ExtractEventsType<InterceptorList extends Interceptor<any>[]> =
InterceptorList extends Array<infer InterceptorType>
? InterceptorType extends Interceptor<infer EventMap>
? EventMap
? InterceptorType extends Interceptor<infer Events>
? Events
: never
: never

Expand All @@ -21,8 +21,8 @@ export type ExtractEventMapType<InterceptorList extends Interceptor<any>[]> =
*/
export class BatchInterceptor<
InterceptorList extends Interceptor<any>[],
EventMap extends EventMapType = ExtractEventMapType<InterceptorList>
> extends Interceptor<EventMap> {
Events extends FunctionEventMap = ExtractEventsType<InterceptorList>
> extends Interceptor<Events> {
static symbol: Symbol

private interceptors: InterceptorList
Expand All @@ -47,9 +47,9 @@ export class BatchInterceptor<
}
}

public on<Event extends ExtractEventNames<EventMap>>(
public on<Event extends ExtractEventNames<Events>>(
event: Event,
listener: EventMap[Event]
listener: Events[Event]
) {
// Instead of adding a listener to the batch interceptor,
// propagate the listener to each of the individual interceptors.
Expand Down
50 changes: 29 additions & 21 deletions src/utils/AsyncEventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { Debugger, debug } from 'debug'
import { StrictEventEmitter, EventMapType } from 'strict-event-emitter'
import { Emitter } from 'strict-event-emitter'
import { nextTick } from './nextTick'

export type FunctionEventMap = Record<string, (...args: Array<any>) => any>
export type FunctionEventMapToStrictEventEmitterMap<
Functions extends FunctionEventMap
> = {
[K in keyof Functions]: Parameters<Functions[K]>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EventMapType and EventMap types are different across strict-event-emitter 0.2 and 0.5. Adding this transformer type to account for that difference since the events contract in Interceptors uses Record<string, Function> index signature to allow specifying the expected return type of the events (since those can be awaited, etc).

}

export interface QueueItem<Args extends any[]> {
args: Args
done: Promise<void>
Expand All @@ -12,15 +19,15 @@ export enum AsyncEventEmitterReadyState {
DEACTIVATED = 'DEACTIVATED',
}

export class AsyncEventEmitter<
EventMap extends EventMapType
> extends StrictEventEmitter<EventMap> {
export class AsyncEventEmitter<Events extends FunctionEventMap> extends Emitter<
FunctionEventMapToStrictEventEmitterMap<Events>
> {
public readyState: AsyncEventEmitterReadyState

private log: Debugger
protected queue: Map<
keyof EventMap,
QueueItem<Parameters<EventMap[keyof EventMap]>>[]
keyof Events,
QueueItem<Parameters<Events[keyof Events]>>[]
>

constructor() {
Expand All @@ -32,10 +39,7 @@ export class AsyncEventEmitter<
this.readyState = AsyncEventEmitterReadyState.ACTIVE
}

public on<Event extends keyof EventMap>(
event: Event,
listener: EventMap[Event]
) {
public on<Event extends keyof Events>(event: Event, listener: Events[Event]) {
const log = this.log.extend('on')

log('adding "%s" listener...', event)
Expand All @@ -45,7 +49,7 @@ export class AsyncEventEmitter<
return this
}

return super.on(event, (async (...args: Parameters<EventMap[Event]>) => {
return super.on(event, (async (...args: Parameters<Events[Event]>) => {
// Event queue is always established when calling ".emit()".
const queue = this.openListenerQueue(event)

Expand All @@ -69,13 +73,18 @@ export class AsyncEventEmitter<
}
}),
})
}) as EventMap[Event])
}) as Events[Event])
}

public emit<Event extends keyof EventMap>(
public emit<Event extends keyof Events>(
event: Event,
...args: Parameters<EventMap[Event]>
...args: Parameters<Events[Event]>
): boolean {
// Ignore internal emitter events.
if (['newListener', 'removeListener'].includes(event as string)) {
return super.emit(event, ...args)
}

const log = this.log.extend('emit')

log('emitting "%s" event...', event)
Expand All @@ -99,7 +108,7 @@ export class AsyncEventEmitter<
this.queue.delete(event)
log('cleaned up "%s" listeners queue!', event)
})
}) as EventMap[Event])
}) as Events[Event])

return super.emit(event, ...args)
}
Expand All @@ -109,10 +118,9 @@ export class AsyncEventEmitter<
* has been called. Awaits asynchronous listeners.
* If the event has no listeners, resolves immediately.
*/
public async untilIdle<Event extends keyof EventMap>(
public async untilIdle<Event extends keyof Events>(
event: Event,
filter: (item: QueueItem<Parameters<EventMap[Event]>>) => boolean = () =>
true
filter: (item: QueueItem<Parameters<Events[Event]>>) => boolean = () => true
): Promise<void> {
const listenersQueue = this.queue.get(event) || []

Expand All @@ -125,9 +133,9 @@ export class AsyncEventEmitter<
})
}

private openListenerQueue<Event extends keyof EventMap>(
private openListenerQueue<Event extends keyof Events>(
event: Event
): QueueItem<Parameters<EventMap[Event]>>[] {
): QueueItem<Parameters<Events[Event]>>[] {
const log = this.log.extend('openListenerQueue')

log('opening "%s" listeners queue...', event)
Expand All @@ -145,7 +153,7 @@ export class AsyncEventEmitter<
return queue
}

public removeAllListeners<Event extends keyof EventMap>(event?: Event) {
public removeAllListeners<Event extends keyof Events>(event?: Event) {
const log = this.log.extend('removeAllListeners')
log('event:', event)

Expand Down
15 changes: 15 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"strict": true,
"target": "ES5",
"sourceMap": true,
"outDir": "lib",
"declaration": true,
"moduleResolution": "node",
"removeComments": false,
"esModuleInterop": true,
"downlevelIteration": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/*.test.*"]
}
14 changes: 3 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"strict": true,
"target": "ES5",
"sourceMap": true,
"outDir": "lib",
"declaration": true,
"moduleResolution": "node",
"removeComments": false,
"esModuleInterop": true,
"downlevelIteration": true
"target": "es6"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/*.test.*"]
"include": ["./src"]
}