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

FCE-205 Refactor into smaller files #7

Merged
merged 52 commits into from
Jul 10, 2024
Merged

FCE-205 Refactor into smaller files #7

merged 52 commits into from
Jul 10, 2024

Conversation

kamil-stasiak
Copy link
Contributor

Description

Basic refactor:

  • extract code to separate files

Motivation and Context

src/webrtc/webRTCEndpoint.ts is too big and has too many responsibilities. It's hard to work with it.

Types of changes

Refactor (non-breaking change)

…efactor-v2

# Conflicts:
#	e2e-tests/react-client/app/package.json
#	packages/ts-client/src/webrtc/webRTCEndpoint.ts
#	yarn.lock
…efactor-v2

# Conflicts:
#	e2e-tests/ts-client/app/package.json
e2e-tests/ts-client/app/package.json Outdated Show resolved Hide resolved
e2e-tests/ts-client/app/playwright.config.ts Outdated Show resolved Hide resolved
packages/ts-client/src/webrtc/CommandsQueue.ts Outdated Show resolved Hide resolved
(this.stateManager.connection.signalingState !== 'stable' ||
this.stateManager.connection.connectionState !== 'connected' ||
this.stateManager.connection.iceConnectionState !== 'connected')
)

Choose a reason for hiding this comment

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

please extract this big boolean to a variable and use the variable in the condition, same can apply to the 95th line, sth like

const areThereOngoingOperations = ...
const isStateInvalid = ...
if(areThereOngoingOperations || isStateInvalid) return

Copy link
Contributor

Choose a reason for hiding this comment

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

Another approach could be to create separate function and call here as:

if(this.isConnectionActvite()) { ...

(maybe with better name?)

Copy link
Contributor Author

@kamil-stasiak kamil-stasiak Jul 9, 2024

Choose a reason for hiding this comment

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

I came up with something like this:

private isConnectionUnstable() {
    const connection = this.stateManager.connection;
    if (connection === undefined) return false;

    const isSignalingUnstable = connection.signalingState !== 'stable';
    const isConnectionNotConnected = connection.connectionState !== 'connected';
    const isIceNotConnected = connection.iceConnectionState !== 'connected';

    return isSignalingUnstable && isConnectionNotConnected && isIceNotConnected;
  }

  private isNegotiationInProgress() {
    return (
      this.negotiationManager.ongoingRenegotiation ||
      this.stateManager.ongoingTrackReplacement
    );
  }

  public processNextCommand() {
    if (this.isNegotiationInProgress()) return;
    if (this.isConnectionUnstable()) return;

    this.resolvePreviousCommand();

    const command = this.commandsQueue.shift();

    if (!command) return;

    this.commandResolutionNotifier = command.resolutionNotifier;
    this.handleCommand(command);
  }

if (this.commandResolutionNotifier) {
this.commandResolutionNotifier.resolve();
this.commandResolutionNotifier = null;
}

Choose a reason for hiding this comment

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

little to not important nit:
would you prefer early returns in general?

if (!this.CRN) return 
this.commandResolutionNotifier.resolve();
this.commandResolutionNotifier = null;

Copy link
Contributor

Choose a reason for hiding this comment

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

:50_50:
I usually prefer to avoid early returns, as they make code less readable. But this is subjective opinion.

packages/ts-client/src/webrtc/transciever.ts Show resolved Hide resolved
packages/ts-client/src/webrtc/transciever.ts Outdated Show resolved Hide resolved
if (trackContext.simulcastConfig!.enabled) {
transceiverConfig = simulcastTransceiverConfig;
const trackActiveEncodings = trackContext.simulcastConfig!.activeEncodings;
const disabledTrackEncodings: TrackEncoding[] = [];

Choose a reason for hiding this comment

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

this fn could be broken down to smaller functions to increase readibility

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. I will refactor it in the future PRs

disabledTrackEncodings.push(encoding.rid! as TrackEncoding);
}
});
disabledTrackEncodingsMap.set(trackContext.trackId, disabledTrackEncodings);

Choose a reason for hiding this comment

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

we mutate a map passed in as an argument and return a completely other value.
please create another function to handle updating the disabled encodings

i can see its being drilled through at least 3 functions, i still think it should get refactored though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will do it in future PRs

packages/ts-client/src/webrtc/transciever.ts Outdated Show resolved Hide resolved
@kamil-stasiak kamil-stasiak changed the title Refactor v2 FCE-205 Refactor into smaller files Jul 9, 2024
Copy link

linear bot commented Jul 9, 2024

packages/ts-client/src/webrtc/CommandsQueue.ts Outdated Show resolved Hide resolved
(this.stateManager.connection.signalingState !== 'stable' ||
this.stateManager.connection.connectionState !== 'connected' ||
this.stateManager.connection.iceConnectionState !== 'connected')
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Another approach could be to create separate function and call here as:

if(this.isConnectionActvite()) { ...

(maybe with better name?)

Comment on lines 95 to 99
if (
this.negotiationManager.ongoingRenegotiation ||
this.stateManager.ongoingTrackReplacement
)
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is something for separate PR. But I would vote to add a prettier rule to always use brackets. This will make expressions like this more readable.

Suggested change
if (
this.negotiationManager.ongoingRenegotiation ||
this.stateManager.ongoingTrackReplacement
)
return;
if (
this.negotiationManager.ongoingRenegotiation ||
this.stateManager.ongoingTrackReplacement
) {
return;
}

if (this.commandResolutionNotifier) {
this.commandResolutionNotifier.resolve();
this.commandResolutionNotifier = null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

:50_50:
I usually prefer to avoid early returns, as they make code less readable. But this is subjective opinion.

* Indicates if an ongoing renegotiation is active.
* During renegotiation, both parties are expected to actively exchange events: renegotiateTracks, offerData, sdpOffer, sdpAnswer.
*/
public ongoingRenegotiation: boolean = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

WDYT about having two functions renegotationStarted() and renegotationCompleted() with private variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. That's why I created this class. I will do it in future PRs

packages/ts-client/src/webrtc/transciever.ts Outdated Show resolved Hide resolved
Comment on lines 181 to 185
.filter((transceiver) => transceiver.sender.track?.id && transceiver.mid)
.reduce(
(acc, transceiver) => {
const localTrackId = transceiver.sender.track!.id;
const mid = transceiver!.mid!;
Copy link
Contributor

Choose a reason for hiding this comment

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

It would require a little more work, but in future refactor it would be nice to create filter functions that returns proper TS type, so no ! will be required.

packages/ts-client/src/webrtc/transciever.ts Outdated Show resolved Hide resolved
packages/ts-client/src/webrtc/turn.ts Outdated Show resolved Hide resolved
packages/ts-client/src/webrtc/turn.ts Show resolved Hide resolved
@kamil-stasiak
Copy link
Contributor Author

I will address the remaining comments (on the old code that I moved to other files) in future PRs. For now, I don't want to interfere too much with the logic. I would like to dedicate the next PR to cleaning up the state, which is currently spread across many fields is StateManager

Copy link
Contributor

@mironiasty mironiasty left a comment

Choose a reason for hiding this comment

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

TL;DR;

@kamil-stasiak kamil-stasiak merged commit b54803b into main Jul 10, 2024
2 checks passed
@kamil-stasiak kamil-stasiak deleted the refactor-v2 branch July 10, 2024 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants