-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Typing event emitters #764
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Added some comments
node/src/DataProducer.ts
Outdated
const logger = new Logger('DataProducer'); | ||
|
||
export class DataProducer extends EnhancedEventEmitter | ||
export class DataProducer extends EnhancedEventEmitter<{ transportclose: [] }> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No Events
type constructed and used here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree: consistency is better than succinctness.
node/src/Router.ts
Outdated
const logger = new Logger('Router'); | ||
|
||
export class Router extends EnhancedEventEmitter | ||
export class Router extends EnhancedEventEmitter<{ workerclose: [] }> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Events
type for consistency?
node/src/Worker.ts
Outdated
@@ -176,7 +178,7 @@ const workerBin = process.env.MEDIASOUP_WORKER_BIN | |||
const logger = new Logger('Worker'); | |||
const workerLogger = new Logger('Worker'); | |||
|
|||
export class Worker extends EnhancedEventEmitter | |||
export class Worker extends EnhancedEventEmitter<{ died: [Error]}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, defining Events
makes it more consistent and allows for seamlessly adding new events in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
node/src/Consumer.ts
Outdated
trace: [ConsumerTraceEventData]; | ||
} | ||
|
||
type ConsumerEvents = Pick<ObserverEvents, 'score' | 'layerschange' | 'trace'> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if Consumer and Consumer.observer events sometimes match, can we repeat their definition instead of picking some of them from the other?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great PR. I've requested some changes to get more consistency.
node/src/ActiveSpeakerObserver.ts
Outdated
type ObserverEvents = RtpObserverEvents & { | ||
dominantspeaker: [{ producer: Producer }]; | ||
} | ||
|
||
type Events = { | ||
routerclose: []; | ||
dominantspeaker: [{ producer: Producer }]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some concerns here that affects all files and not just this one:
- Can we please define first class events and then observer events?.
routerclose
is not just an event inActiveSpeakerObserver
class. It's an event that allRtpObserver
inherited classes.- The name of
RtpObserver
class in unfortunate since it also contains anobserver
. So events of theobserver
ofRtpObserver
should be namedRtpObserverObserverEvents
. - And events of the
RtpObserver
parent class should be namedRtpObserverEvents
. - Some classes (such as
Transport
andRtpObserver
export theyXxxxxxEvents
andXxxxxObserverEvents
. However, in child classes this PR definestype Events
andtype ObserverEvents
. For consistency we should export these as well and prefix their name with the class name. - Not sure why ESLint doesn't complain but we mandate opening brackets to be in the next line.
So IMHO this should be as follows:
type ObserverEvents = RtpObserverEvents & { | |
dominantspeaker: [{ producer: Producer }]; | |
} | |
type Events = { | |
routerclose: []; | |
dominantspeaker: [{ producer: Producer }]; | |
} | |
type ActiveSpeakerEvents = RtpObserverEvents & | |
{ | |
dominantspeaker: [{ producer: Producer }]; | |
} | |
type ActiveSpeakerObserverEvents = RtpObserverObserverEvents & | |
{ | |
dominantspeaker: [{ producer: Producer }]; | |
} |
node/src/RtpObserver.ts
Outdated
export type RtpObserverEvents = { | ||
close: []; | ||
pause: []; | ||
resume: []; | ||
addproducer: [Producer]; | ||
removeproducer: [Producer]; | ||
} | ||
|
||
type Events = { | ||
routerclose: []; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export type RtpObserverEvents = { | |
close: []; | |
pause: []; | |
resume: []; | |
addproducer: [Producer]; | |
removeproducer: [Producer]; | |
} | |
type Events = { | |
routerclose: []; | |
} | |
export type RtpObserverEvents = | |
{ | |
routerclose: []; | |
} | |
export type RtpObserverObserverEvents = | |
{ | |
close: []; | |
pause: []; | |
resume: []; | |
addproducer: [Producer]; | |
removeproducer: [Producer]; | |
} |
node/src/Transport.ts
Outdated
type ObserverEvents = { | ||
close: []; | ||
newproducer: [Producer]; | ||
newconsumer: [Consumer]; | ||
newdataproducer: [DataProducer]; | ||
newdataconsumer: [DataConsumer]; | ||
trace: [TransportTraceEventData]; | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type ObserverEvents = { | |
close: []; | |
newproducer: [Producer]; | |
newconsumer: [Consumer]; | |
newdataproducer: [DataProducer]; | |
newdataconsumer: [DataConsumer]; | |
trace: [TransportTraceEventData]; | |
} | |
export type TransportObserverEvents = | |
{ | |
close: []; | |
newproducer: [Producer]; | |
newconsumer: [Consumer]; | |
newdataproducer: [DataProducer]; | |
newdataconsumer: [DataConsumer]; | |
trace: [TransportTraceEventData]; | |
} |
node/src/Transport.ts
Outdated
@@ -81,9 +81,23 @@ export interface TransportTraceEventData | |||
|
|||
export type SctpState = 'new' | 'connecting' | 'connected' | 'failed' | 'closed'; | |||
|
|||
export type TransportBaseEvents = { routerclose: []; trace: [TransportTraceEventData] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for "base" in the name:
export type TransportBaseEvents = { routerclose: []; trace: [TransportTraceEventData] } | |
export type TransportEvents = { routerclose: []; trace: [TransportTraceEventData] } |
@ibc
According to their docs; you should have following rule to enable it for typescript: {
// note you must disable the base rule as it can report incorrect errors
"brace-style": "off",
"@typescript-eslint/brace-style": ["error"]
} Unfortunately (which I checked) it does not add support for |
I'm running CI checks on this PR. It looks good. I'll do my final review tomorrow and merge if no more changes are requested. Thanks for this! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic. Merging, THANKS
Released in 3.9.7 |
This PR adds proper typing to event emitters for all classes documented in the official docs: https://mediasoup.org/documentation/v3/mediasoup/api/.
Listening to (and emitting) events is fully type checked.
The PR is compatible with current way of registering internal events (names starting with
@
).Initially I wanted only to augment types, but finally very minor changes to
EnhancedEventEmitter
were introduced.One mismatch between code and docs discovered is argument to
ActiveSpeakerObserver.on('dominantspeaker')
. According to docs it isProducer
according to code{ producer: Producer }
.