Skip to content

Commit

Permalink
fix: replace StrictEventEmitter with Emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguttandin committed Jan 17, 2023
1 parent 3b97268 commit a7cfc73
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
22 changes: 11 additions & 11 deletions src/SetupApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { invariant } from 'outvariant'
import { EventMapType, StrictEventEmitter } from 'strict-event-emitter'
import { EventMap, Emitter } from 'strict-event-emitter'
import {
DefaultBodyType,
RequestHandler,
Expand All @@ -14,11 +14,11 @@ import { MockedRequest } from './utils/request/MockedRequest'
/**
* Generic class for the mock API setup.
*/
export abstract class SetupApi<EventsMap extends EventMapType> {
export abstract class SetupApi<EventsMap extends EventMap> {
protected initialHandlers: ReadonlyArray<RequestHandler>
protected currentHandlers: Array<RequestHandler>
protected readonly emitter: StrictEventEmitter<EventsMap>
protected readonly publicEmitter: StrictEventEmitter<EventsMap>
protected readonly emitter: Emitter<EventsMap>
protected readonly publicEmitter: Emitter<EventsMap>

public readonly events: LifeCycleEventEmitter<EventsMap>

Expand All @@ -28,8 +28,8 @@ export abstract class SetupApi<EventsMap extends EventMapType> {
this.initialHandlers = toReadonlyArray(initialHandlers)
this.currentHandlers = [...initialHandlers]

this.emitter = new StrictEventEmitter<EventsMap>()
this.publicEmitter = new StrictEventEmitter<EventsMap>()
this.emitter = new Emitter<EventsMap>()
this.publicEmitter = new Emitter<EventsMap>()
pipeEvents(this.emitter, this.publicEmitter)

this.events = this.createLifeCycleEvents()
Expand Down Expand Up @@ -81,13 +81,13 @@ export abstract class SetupApi<EventsMap extends EventMapType> {

private createLifeCycleEvents(): LifeCycleEventEmitter<EventsMap> {
return {
on: (...args) => {
return this.publicEmitter.on(...args)
on: (...args: any[]) => {
return (this.publicEmitter.on as any)(...args)
},
removeListener: (...args) => {
return this.publicEmitter.removeListener(...args)
removeListener: (...args: any[]) => {
return (this.publicEmitter.removeListener as any)(...args)
},
removeAllListeners: (...args: any) => {
removeAllListeners: (...args: any[]) => {
return this.publicEmitter.removeAllListeners(...args)
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/setupWorker/glossary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FlatHeadersObject } from 'headers-polyfill'
import { StrictEventEmitter } from 'strict-event-emitter'
import { Emitter } from 'strict-event-emitter'
import {
LifeCycleEventEmitter,
LifeCycleEventsMap,
Expand Down Expand Up @@ -103,7 +103,7 @@ export interface SetupWorkerInternalContext {
worker: ServiceWorker | null
registration: ServiceWorkerRegistration | null
requestHandlers: RequestHandler[]
emitter: StrictEventEmitter<WorkerLifecycleEventsMap>
emitter: Emitter<WorkerLifecycleEventsMap>
keepAliveInterval?: number
workerChannel: {
/**
Expand Down
22 changes: 10 additions & 12 deletions src/sharedOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StrictEventEmitter } from 'strict-event-emitter'
import { Emitter } from 'strict-event-emitter'
import { MockedRequest } from './utils/request/MockedRequest'
import { UnhandledRequestStrategy } from './utils/request/onUnhandledRequest'

Expand All @@ -15,18 +15,16 @@ export interface SharedOptions {
}

export interface LifeCycleEventsMap<ResponseType> {
'request:start': (request: MockedRequest) => void
'request:match': (request: MockedRequest) => void
'request:unhandled': (request: MockedRequest) => void
'request:end': (request: MockedRequest) => void
'response:mocked': (response: ResponseType, requestId: string) => void
'response:bypass': (response: ResponseType, requestId: string) => void
unhandledException: (error: Error, request: MockedRequest) => void
'request:start': [MockedRequest]
'request:match': [MockedRequest]
'request:unhandled': [MockedRequest]
'request:end': [MockedRequest]
'response:mocked': [response: ResponseType, requestId: string]
'response:bypass': [response: ResponseType, requestId: string]
unhandledException: [error: Error, request: MockedRequest]
[key: string]: Array<unknown>
}

export type LifeCycleEventEmitter<
ResponseType extends Record<string | symbol, any>,
> = Pick<
StrictEventEmitter<ResponseType>,
'on' | 'removeListener' | 'removeAllListeners'
>
> = Pick<Emitter<ResponseType>, 'on' | 'removeListener' | 'removeAllListeners'>
4 changes: 2 additions & 2 deletions src/utils/handleRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Headers } from 'headers-polyfill'
import { StrictEventEmitter } from 'strict-event-emitter'
import { Emitter } from 'strict-event-emitter'
import { ServerLifecycleEventsMap } from '../node/glossary'
import { SharedOptions } from '../sharedOptions'
import { RequestHandler } from '../handlers/RequestHandler'
Expand All @@ -18,7 +18,7 @@ const callbacks: Partial<Record<keyof HandleRequestOptions<any>, any>> = {
}

function setup() {
const emitter = new StrictEventEmitter<ServerLifecycleEventsMap>()
const emitter = new Emitter<ServerLifecycleEventsMap>()
const listener = jest.fn()

const createMockListener = (name: string) => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/handleRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { until } from '@open-draft/until'
import { StrictEventEmitter } from 'strict-event-emitter'
import { Emitter } from 'strict-event-emitter'
import { RequestHandler } from '../handlers/RequestHandler'
import { ServerLifecycleEventsMap } from '../node/glossary'
import { MockedResponse } from '../response'
Expand Down Expand Up @@ -45,7 +45,7 @@ export async function handleRequest<
request: MockedRequest,
handlers: RequestHandler[],
options: RequiredDeep<SharedOptions>,
emitter: StrictEventEmitter<ServerLifecycleEventsMap>,
emitter: Emitter<ServerLifecycleEventsMap>,
handleRequestOptions?: HandleRequestOptions<ResponseType>,
): Promise<ResponseType | undefined> {
emitter.emit('request:start', request)
Expand Down
8 changes: 4 additions & 4 deletions src/utils/internal/pipeEvents.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { EventEmitter } from 'stream'
import { Emitter, EventMap } from 'strict-event-emitter'

/**
* Pipes all emitted events from one emitter to another.
*/
export function pipeEvents(
source: EventEmitter,
destination: EventEmitter,
export function pipeEvents<Events extends EventMap>(
source: Emitter<Events>,
destination: Emitter<Events>,
): void {
const rawEmit = source.emit

Expand Down

0 comments on commit a7cfc73

Please sign in to comment.