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

Typing event emitters #764

Merged
merged 11 commits into from
Feb 22, 2022
Merged

Typing event emitters #764

merged 11 commits into from
Feb 22, 2022

Conversation

unao
Copy link
Contributor

@unao unao commented Feb 7, 2022

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 is Producer according to code { producer: Producer }.

Copy link
Member

@jmillan jmillan left a 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

const logger = new Logger('DataProducer');

export class DataProducer extends EnhancedEventEmitter
export class DataProducer extends EnhancedEventEmitter<{ transportclose: [] }>
Copy link
Member

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?

Copy link
Contributor Author

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.

const logger = new Logger('Router');

export class Router extends EnhancedEventEmitter
export class Router extends EnhancedEventEmitter<{ workerclose: [] }>
Copy link
Member

Choose a reason for hiding this comment

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

Events type for consistency?

@@ -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]}>
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

trace: [ConsumerTraceEventData];
}

type ConsumerEvents = Pick<ObserverEvents, 'score' | 'layerschange' | 'trace'>
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@unao unao requested a review from ibc February 9, 2022 13:25
Copy link
Member

@ibc ibc left a 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.

Comment on lines 22 to 29
type ObserverEvents = RtpObserverEvents & {
dominantspeaker: [{ producer: Producer }];
}

type Events = {
routerclose: [];
dominantspeaker: [{ producer: Producer }];
}
Copy link
Member

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:

  1. Can we please define first class events and then observer events?.
  2. routerclose is not just an event in ActiveSpeakerObserver class. It's an event that all RtpObserver inherited classes.
  3. The name of RtpObserver class in unfortunate since it also contains an observer. So events of the observer of RtpObserver should be named RtpObserverObserverEvents.
  4. And events of the RtpObserver parent class should be named RtpObserverEvents.
  5. Some classes (such as Transport and RtpObserver export they XxxxxxEvents and XxxxxObserverEvents. However, in child classes this PR defines type Events and type ObserverEvents. For consistency we should export these as well and prefix their name with the class name.
  6. 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:

Suggested change
type ObserverEvents = RtpObserverEvents & {
dominantspeaker: [{ producer: Producer }];
}
type Events = {
routerclose: [];
dominantspeaker: [{ producer: Producer }];
}
type ActiveSpeakerEvents = RtpObserverEvents &
{
dominantspeaker: [{ producer: Producer }];
}
type ActiveSpeakerObserverEvents = RtpObserverObserverEvents &
{
dominantspeaker: [{ producer: Producer }];
}

Comment on lines 7 to 17
export type RtpObserverEvents = {
close: [];
pause: [];
resume: [];
addproducer: [Producer];
removeproducer: [Producer];
}

type Events = {
routerclose: [];
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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];
}

Comment on lines 86 to 94
type ObserverEvents = {
close: [];
newproducer: [Producer];
newconsumer: [Consumer];
newdataproducer: [DataProducer];
newdataconsumer: [DataConsumer];
trace: [TransportTraceEventData];

}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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];
}

@@ -81,9 +81,23 @@ export interface TransportTraceEventData

export type SctpState = 'new' | 'connecting' | 'connected' | 'failed' | 'closed';

export type TransportBaseEvents = { routerclose: []; trace: [TransportTraceEventData] }
Copy link
Member

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:

Suggested change
export type TransportBaseEvents = { routerclose: []; trace: [TransportTraceEventData] }
export type TransportEvents = { routerclose: []; trace: [TransportTraceEventData] }

@unao
Copy link
Contributor Author

unao commented Feb 10, 2022

@ibc
Updated the PR accordingly. I introduced bit more verbosity for the sake of decoupling.

Not sure why ESLint doesn't complain but we mandate opening brackets to be in the next line.

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 type; only for (...) enum, interface, namespace and module declarations.

@unao unao requested a review from ibc February 10, 2022 17:09
@unao
Copy link
Contributor Author

unao commented Feb 10, 2022

@ibc
Copy link
Member

ibc commented Feb 10, 2022

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!

Copy link
Member

@ibc ibc left a comment

Choose a reason for hiding this comment

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

Fantastic. Merging, THANKS

@ibc ibc merged commit fe5b3ab into versatica:v3 Feb 22, 2022
@ibc
Copy link
Member

ibc commented Feb 22, 2022

Released in 3.9.7

@unao unao deleted the typing-event-emitters branch February 22, 2022 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants