Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Apply strictNullChecks to src/LegacyCallHandler.tsx #10690

Merged
merged 9 commits into from
May 5, 2023

Conversation

artcodespace
Copy link
Contributor

@artcodespace artcodespace commented Apr 21, 2023

Closes element-hq/element-web#25087

Checklist

  • Tests written for new code (and old code if feasible)
  • Linter and other CI checks pass
  • Sign-off given on the changes (see CONTRIBUTING.md)

This change is marked as an internal change (Task), so will not be included in the changelog.

@artcodespace artcodespace added the T-Task Refactoring, enabling or disabling functionality, other engineering tasks label Apr 21, 2023
@artcodespace artcodespace marked this pull request as ready for review April 21, 2023 12:55
@artcodespace artcodespace requested a review from a team as a code owner April 21, 2023 12:55
Comment on lines 150 to 153
function isNonNull<T>(thing: T | null): thing is T {
return thing !== null;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will probably need to move somewhere else - going to ask about whether we have a place for typeguards

Copy link
Contributor

@florianduros florianduros left a comment

Choose a reason for hiding this comment

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

Imo instead of using isNotNull, we can use if(myVar). It's simpler and easy to maintain

@@ -581,7 +583,9 @@ export default class LegacyCallHandler extends EventEmitter {
call.on(CallEvent.Hangup, () => {
if (!this.matchesCallForThisRoom(call)) return;

this.removeCallForRoom(mappedRoomId);
if (isNotNull(mappedRoomId)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just do ?

if (mappedRoomId) {
   this.removeCallForRoom(mappedRoomId)
}

@@ -597,8 +601,10 @@ export default class LegacyCallHandler extends EventEmitter {
this.pause(AudioID.Ringback);
}

this.removeCallForRoom(mappedRoomId);
this.addCallForRoom(mappedRoomId, newCall);
if (isNotNull(mappedRoomId)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if (mappedRoomId) ?

@@ -691,7 +700,10 @@ export default class LegacyCallHandler extends EventEmitter {
}
case CallState.Ended: {
const hangupReason = call.hangupReason;
this.removeCallForRoom(mappedRoomId);
if (isNotNull(mappedRoomId)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same ^^ if (mappedRoomId)

@@ -723,7 +735,9 @@ export default class LegacyCallHandler extends EventEmitter {
this.play(AudioID.CallEnd);
}

this.logCallStats(call, mappedRoomId);
if (isNotNull(mappedRoomId)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if (mappedRoomId)

room_id: roomId,
metricsTrigger: "WebDialPad",
});
if (isNotNull(roomId)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if (roomId)

joining: false,
metricsTrigger: undefined, // other
});
if (isNotNull(dmRoomId)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if (dmRoomId)

@@ -1175,15 +1192,18 @@ export default class LegacyCallHandler extends EventEmitter {
const widget = WidgetStore.instance.getApps(roomId).find((app) => WidgetType.JITSI.matches(app.type));
if (widget) {
// If there already is a Jitsi widget, pin it
WidgetLayoutStore.instance.moveToContainer(client.getRoom(roomId), widget, Container.Top);
const room = client.getRoom(roomId);
if (isNotNull(room)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if (room)

@artcodespace
Copy link
Contributor Author

Imo instead of using isNotNull, we can use if(myVar). It's simpler and easy to maintain

It is simpler, but we recently discussed not using implicit coercion to Booleans. Let me see if I can find the meeting notes where we discussed it and what the outcome was.

Notes were from 6 Apr (in the Element Web Weekly notes). My reading of the meeting notes is we should prefer explicit comparison over coercion, which I think is what I have done here. In fact @justjanne some of the comments in the notes were attributed to you, interested to hear what you think too.

@justjanne
Copy link
Contributor

@alunturner It's important to be careful as falsy isn't always what we'd want it to be. In fact, I actually saw another case of a falsy value (first enum member) being treated as if it was false when it shouldn't have been just this week: matrix-org/matrix-js-sdk#3248 (comment)

So I'd keep the isNotNull helper.

@artcodespace
Copy link
Contributor Author

@florianduros 👋 I'm back after a few days away and trying to get some stalled work moving. What do you think is the way ahead here? I think we should stick with the isNotNull typeguard and Janne seems to agree too.

@florianduros
Copy link
Contributor

@alunturner okay, seems fine to me !

@artcodespace artcodespace enabled auto-merge May 5, 2023 12:39
@t3chguy t3chguy merged commit de16d34 into develop May 5, 2023
@t3chguy t3chguy deleted the alunturner/strict-null-checks branch May 5, 2023 16:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
T-Task Refactoring, enabling or disabling functionality, other engineering tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Apply strictNullChecks to src/LegacyCallHandler.tsx
4 participants