-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): Use type-safe event emitters (no-changelog) (#10234)
- Loading branch information
Showing
12 changed files
with
124 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { EventEmitter } from 'node:events'; | ||
import debounce from 'lodash/debounce'; | ||
|
||
type Payloads<ListenerMap> = { | ||
[E in keyof ListenerMap]: unknown; | ||
}; | ||
|
||
type Listener<Payload> = (payload: Payload) => void; | ||
|
||
export class TypedEmitter<ListenerMap extends Payloads<ListenerMap>> extends EventEmitter { | ||
private debounceWait = 300; // milliseconds | ||
|
||
override on<EventName extends keyof ListenerMap & string>( | ||
eventName: EventName, | ||
listener: Listener<ListenerMap[EventName]>, | ||
) { | ||
return super.on(eventName, listener); | ||
} | ||
|
||
override once<EventName extends keyof ListenerMap & string>( | ||
eventName: EventName, | ||
listener: Listener<ListenerMap[EventName]>, | ||
) { | ||
return super.once(eventName, listener); | ||
} | ||
|
||
override off<EventName extends keyof ListenerMap & string>( | ||
eventName: EventName, | ||
listener: Listener<ListenerMap[EventName]>, | ||
) { | ||
return super.off(eventName, listener); | ||
} | ||
|
||
override emit<EventName extends keyof ListenerMap & string>( | ||
eventName: EventName, | ||
payload?: ListenerMap[EventName], | ||
): boolean { | ||
return super.emit(eventName, payload); | ||
} | ||
|
||
protected debouncedEmit = debounce( | ||
<EventName extends keyof ListenerMap & string>( | ||
eventName: EventName, | ||
payload?: ListenerMap[EventName], | ||
) => super.emit(eventName, payload), | ||
this.debounceWait, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,6 @@ | ||
import { EventEmitter } from 'node:events'; | ||
import { Service } from 'typedi'; | ||
import { TypedEmitter } from '@/TypedEmitter'; | ||
import type { Event } from './event.types'; | ||
|
||
@Service() | ||
export class EventService extends EventEmitter { | ||
emit<K extends keyof Event>(eventName: K, arg?: Event[K]) { | ||
super.emit(eventName, arg); | ||
return true; | ||
} | ||
|
||
on<K extends keyof Event>(eventName: K, handler: (arg: Event[K]) => void) { | ||
super.on(eventName, handler); | ||
return this; | ||
} | ||
} | ||
export class EventService extends TypedEmitter<Event> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
packages/cli/src/services/orchestration/main/MultiMainSetup.ee.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.