Skip to content

Commit 2d9982f

Browse files
authored
Remove boilerplate around dispatcher and settings watchers (#28338)
* Remove boilerplate around dispatcher and settings watchers Signed-off-by: Michael Telatynski <[email protected]> * Iterate Signed-off-by: Michael Telatynski <[email protected]> * Iterate Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent b8fd98a commit 2d9982f

36 files changed

+81
-111
lines changed

src/DeviceListener.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,9 @@ export default class DeviceListener {
113113
this.client.removeListener(ClientEvent.Sync, this.onSync);
114114
this.client.removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
115115
}
116-
if (this.deviceClientInformationSettingWatcherRef) {
117-
SettingsStore.unwatchSetting(this.deviceClientInformationSettingWatcherRef);
118-
}
119-
if (this.dispatcherRef) {
120-
dis.unregister(this.dispatcherRef);
121-
this.dispatcherRef = undefined;
122-
}
116+
SettingsStore.unwatchSetting(this.deviceClientInformationSettingWatcherRef);
117+
dis.unregister(this.dispatcherRef);
118+
this.dispatcherRef = undefined;
123119
this.dismissed.clear();
124120
this.dismissedThisDeviceToast = false;
125121
this.keyBackupInfo = null;

src/PosthogAnalytics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export class PosthogAnalytics {
326326
if (this.enabled) {
327327
this.posthog.reset();
328328
}
329-
if (this.watchSettingRef) SettingsStore.unwatchSetting(this.watchSettingRef);
329+
SettingsStore.unwatchSetting(this.watchSettingRef);
330330
this.setAnonymity(Anonymity.Disabled);
331331
}
332332

src/Presence.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import { ActionPayload } from "./dispatcher/payloads";
2020
const UNAVAILABLE_TIME_MS = 3 * 60 * 1000; // 3 mins
2121

2222
class Presence {
23-
private unavailableTimer: Timer | null = null;
24-
private dispatcherRef: string | null = null;
25-
private state: SetPresence | null = null;
23+
private unavailableTimer?: Timer;
24+
private dispatcherRef?: string;
25+
private state?: SetPresence;
2626

2727
/**
2828
* Start listening the user activity to evaluate his presence state.
@@ -46,22 +46,18 @@ class Presence {
4646
* Stop tracking user activity
4747
*/
4848
public stop(): void {
49-
if (this.dispatcherRef) {
50-
dis.unregister(this.dispatcherRef);
51-
this.dispatcherRef = null;
52-
}
53-
if (this.unavailableTimer) {
54-
this.unavailableTimer.abort();
55-
this.unavailableTimer = null;
56-
}
49+
dis.unregister(this.dispatcherRef);
50+
this.dispatcherRef = undefined;
51+
this.unavailableTimer?.abort();
52+
this.unavailableTimer = undefined;
5753
}
5854

5955
/**
6056
* Get the current presence state.
6157
* @returns {string} the presence state (see PRESENCE enum)
6258
*/
6359
public getState(): SetPresence | null {
64-
return this.state;
60+
return this.state ?? null;
6561
}
6662

6763
private onAction = (payload: ActionPayload): void => {

src/components/structures/EmbeddedPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default class EmbeddedPage extends React.PureComponent<IProps, IState> {
3838
public static contextType = MatrixClientContext;
3939
public declare context: React.ContextType<typeof MatrixClientContext>;
4040
private unmounted = false;
41-
private dispatcherRef: string | null = null;
41+
private dispatcherRef?: string;
4242

4343
public constructor(props: IProps, context: React.ContextType<typeof MatrixClientContext>) {
4444
super(props, context);
@@ -100,7 +100,7 @@ export default class EmbeddedPage extends React.PureComponent<IProps, IState> {
100100

101101
public componentWillUnmount(): void {
102102
this.unmounted = true;
103-
if (this.dispatcherRef !== null) dis.unregister(this.dispatcherRef);
103+
dis.unregister(this.dispatcherRef);
104104
}
105105

106106
private onAction = (payload: ActionPayload): void => {

src/components/structures/LoggedInView.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ class LoggedInView extends React.Component<IProps, IState> {
228228
this._matrixClient.removeListener(ClientEvent.Sync, this.onSync);
229229
this._matrixClient.removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
230230
OwnProfileStore.instance.off(UPDATE_EVENT, this.refreshBackgroundImage);
231-
if (this.layoutWatcherRef) SettingsStore.unwatchSetting(this.layoutWatcherRef);
232-
if (this.compactLayoutWatcherRef) SettingsStore.unwatchSetting(this.compactLayoutWatcherRef);
233-
if (this.backgroundImageWatcherRef) SettingsStore.unwatchSetting(this.backgroundImageWatcherRef);
231+
SettingsStore.unwatchSetting(this.layoutWatcherRef);
232+
SettingsStore.unwatchSetting(this.compactLayoutWatcherRef);
233+
SettingsStore.unwatchSetting(this.backgroundImageWatcherRef);
234234
this.timezoneProfileUpdateRef?.forEach((s) => SettingsStore.unwatchSetting(s));
235235
this.resizer?.detach();
236236
}

src/components/structures/ThreadView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default class ThreadView extends React.Component<IProps, IState> {
7777
public static contextType = RoomContext;
7878
public declare context: React.ContextType<typeof RoomContext>;
7979

80-
private dispatcherRef: string | null = null;
80+
private dispatcherRef?: string;
8181
private readonly layoutWatcherRef: string;
8282
private timelinePanel = createRef<TimelinePanel>();
8383
private card = createRef<HTMLDivElement>();
@@ -118,7 +118,7 @@ export default class ThreadView extends React.Component<IProps, IState> {
118118
}
119119

120120
public componentWillUnmount(): void {
121-
if (this.dispatcherRef) dis.unregister(this.dispatcherRef);
121+
dis.unregister(this.dispatcherRef);
122122
const roomId = this.props.mxEvent.getRoomId();
123123
SettingsStore.unwatchSetting(this.layoutWatcherRef);
124124

src/components/structures/UserMenu.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ export default class UserMenu extends React.Component<IProps, IState> {
121121
}
122122

123123
public componentWillUnmount(): void {
124-
if (this.themeWatcherRef) SettingsStore.unwatchSetting(this.themeWatcherRef);
125-
if (this.dndWatcherRef) SettingsStore.unwatchSetting(this.dndWatcherRef);
126-
if (this.dispatcherRef) defaultDispatcher.unregister(this.dispatcherRef);
124+
SettingsStore.unwatchSetting(this.themeWatcherRef);
125+
SettingsStore.unwatchSetting(this.dndWatcherRef);
126+
defaultDispatcher.unregister(this.dispatcherRef);
127127
OwnProfileStore.instance.off(UPDATE_EVENT, this.onProfileUpdate);
128128
SpaceStore.instance.off(UPDATE_SELECTED_SPACE, this.onSelectedSpaceUpdate);
129129
this.context.voiceBroadcastRecordingsStore.off(

src/components/views/dialogs/RoomSettingsDialog.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ class RoomSettingsDialog extends React.Component<IProps, IState> {
8080
}
8181

8282
public componentWillUnmount(): void {
83-
if (this.dispatcherRef) {
84-
dis.unregister(this.dispatcherRef);
85-
}
83+
dis.unregister(this.dispatcherRef);
8684

8785
MatrixClientPeg.get()?.removeListener(RoomEvent.Name, this.onRoomName);
8886
MatrixClientPeg.get()?.removeListener(RoomStateEvent.Events, this.onStateEvent);

src/components/views/elements/AppTile.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,13 @@ export default class AppTile extends React.Component<IProps, IState> {
340340
}
341341

342342
// Widget action listeners
343-
if (this.dispatcherRef) dis.unregister(this.dispatcherRef);
343+
dis.unregister(this.dispatcherRef);
344344

345345
if (this.props.room) {
346346
this.context.off(RoomEvent.MyMembership, this.onMyMembership);
347347
}
348348

349-
if (this.allowedWidgetsWatchRef) SettingsStore.unwatchSetting(this.allowedWidgetsWatchRef);
349+
SettingsStore.unwatchSetting(this.allowedWidgetsWatchRef);
350350
OwnProfileStore.instance.removeListener(UPDATE_EVENT, this.onUserReady);
351351
}
352352

src/components/views/messages/DateSeparator.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default class DateSeparator extends React.Component<IProps, IState> {
7171
}
7272

7373
public componentWillUnmount(): void {
74-
if (this.settingWatcherRef) SettingsStore.unwatchSetting(this.settingWatcherRef);
74+
SettingsStore.unwatchSetting(this.settingWatcherRef);
7575
}
7676

7777
private onContextMenuOpenClick = (e: ButtonEvent): void => {

0 commit comments

Comments
 (0)