Skip to content

Commit

Permalink
Move state update listeners from constructor to componentDidMount (#2…
Browse files Browse the repository at this point in the history
…8341)

* Move state update listeners from constructor to componentDidMount

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]>
  • Loading branch information
t3chguy authored Nov 1, 2024
1 parent 2d9982f commit 0899165
Show file tree
Hide file tree
Showing 72 changed files with 377 additions and 309 deletions.
1 change: 1 addition & 0 deletions src/AsyncWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default class AsyncWrapper extends React.Component<IProps, IState> {
public state: IState = {};

public componentDidMount(): void {
this.unmounted = false;
this.props.prom
.then((result) => {
if (this.unmounted) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ export default class CreateSecretStorageDialog extends React.PureComponent<IProp
// signing key upload as well. This avoids hitting the server to
// test auth flows, which may be slow under high load.
canUploadKeysWithPasswordOnly = true;
} else {
this.queryKeyUploadAuth();
}

const keyFromCustomisations = ModuleRunner.instance.extensions.cryptoSetup.createSecretStorageKey();
Expand All @@ -140,8 +138,15 @@ export default class CreateSecretStorageDialog extends React.PureComponent<IProp
passPhraseKeySelected,
accountPassword,
};
}

public componentDidMount(): void {
const keyFromCustomisations = ModuleRunner.instance.extensions.cryptoSetup.createSecretStorageKey();
if (keyFromCustomisations) this.initExtension(keyFromCustomisations);

if (this.state.canUploadKeysWithPasswordOnly === null) {
this.queryKeyUploadAuth();
}
}

private initExtension(keyFromCustomisations: Uint8Array): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export default class ExportE2eKeysDialog extends React.Component<IProps, IState>
};
}

public componentDidMount(): void {
this.unmounted = false;
}

public componentWillUnmount(): void {
this.unmounted = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export default class ImportE2eKeysDialog extends React.Component<IProps, IState>
};
}

public componentDidMount(): void {
this.unmounted = false;
}

public componentWillUnmount(): void {
this.unmounted = true;
}
Expand Down
8 changes: 5 additions & 3 deletions src/components/structures/InteractiveAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ interface IState {

export default class InteractiveAuthComponent<T> extends React.Component<InteractiveAuthProps<T>, IState> {
private readonly authLogic: InteractiveAuth<T>;
private readonly intervalId: number | null = null;
private readonly stageComponent = createRef<IStageComponent>();
private intervalId: number | null = null;

private unmounted = false;

Expand Down Expand Up @@ -126,15 +126,17 @@ export default class InteractiveAuthComponent<T> extends React.Component<Interac
AuthType.SsoUnstable,
],
});
}

public componentDidMount(): void {
this.unmounted = false;

if (this.props.poll) {
this.intervalId = window.setInterval(() => {
this.authLogic.poll();
}, 2000);
}
}

public componentDidMount(): void {
this.authLogic
.attemptAuth()
.then(async (result) => {
Expand Down
8 changes: 4 additions & 4 deletions src/components/structures/LeftPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ export default class LeftPanel extends React.Component<IProps, IState> {
activeSpace: SpaceStore.instance.activeSpace,
showBreadcrumbs: LeftPanel.breadcrumbsMode,
};

BreadcrumbsStore.instance.on(UPDATE_EVENT, this.onBreadcrumbsUpdate);
RoomListStore.instance.on(LISTS_UPDATE_EVENT, this.onBreadcrumbsUpdate);
SpaceStore.instance.on(UPDATE_SELECTED_SPACE, this.updateActiveSpace);
}

private static get breadcrumbsMode(): BreadcrumbsMode {
return !BreadcrumbsStore.instance.visible ? BreadcrumbsMode.Disabled : BreadcrumbsMode.Legacy;
}

public componentDidMount(): void {
BreadcrumbsStore.instance.on(UPDATE_EVENT, this.onBreadcrumbsUpdate);
RoomListStore.instance.on(LISTS_UPDATE_EVENT, this.onBreadcrumbsUpdate);
SpaceStore.instance.on(UPDATE_SELECTED_SPACE, this.updateActiveSpace);

if (this.listContainerRef.current) {
UIStore.instance.trackElementDimensions("ListContainer", this.listContainerRef.current);
// Using the passive option to not block the main thread
Expand Down
71 changes: 35 additions & 36 deletions src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
private prevWindowWidth: number;
private voiceBroadcastResumer?: VoiceBroadcastResumer;

private readonly loggedInView: React.RefObject<LoggedInViewType>;
private readonly dispatcherRef: string;
private readonly themeWatcher: ThemeWatcher;
private readonly fontWatcher: FontWatcher;
private readonly loggedInView = createRef<LoggedInViewType>();
private dispatcherRef?: string;
private themeWatcher?: ThemeWatcher;
private fontWatcher?: FontWatcher;
private readonly stores: SdkContextClass;

public constructor(props: IProps) {
Expand All @@ -256,8 +256,6 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
ready: false,
};

this.loggedInView = createRef();

SdkConfig.put(this.props.config);

// Used by _viewRoom before getting state from sync
Expand All @@ -282,32 +280,10 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
}

this.prevWindowWidth = UIStore.instance.windowWidth || 1000;
UIStore.instance.on(UI_EVENTS.Resize, this.handleResize);

// For PersistentElement
this.state.resizeNotifier.on("middlePanelResized", this.dispatchTimelineResize);

RoomNotificationStateStore.instance.on(UPDATE_STATUS_INDICATOR, this.onUpdateStatusIndicator);

this.dispatcherRef = dis.register(this.onAction);

this.themeWatcher = new ThemeWatcher();
this.fontWatcher = new FontWatcher();
this.themeWatcher.start();
this.fontWatcher.start();

// object field used for tracking the status info appended to the title tag.
// we don't do it as react state as i'm scared about triggering needless react refreshes.
this.subTitleStatus = "";

initSentry(SdkConfig.get("sentry"));

if (!checkSessionLockFree()) {
// another instance holds the lock; confirm its theft before proceeding
setTimeout(() => this.setState({ view: Views.CONFIRM_LOCK_THEFT }), 0);
} else {
this.startInitSession();
}
}

/**
Expand Down Expand Up @@ -476,6 +452,29 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
}

public componentDidMount(): void {
UIStore.instance.on(UI_EVENTS.Resize, this.handleResize);

// For PersistentElement
this.state.resizeNotifier.on("middlePanelResized", this.dispatchTimelineResize);

RoomNotificationStateStore.instance.on(UPDATE_STATUS_INDICATOR, this.onUpdateStatusIndicator);

this.dispatcherRef = dis.register(this.onAction);

this.themeWatcher = new ThemeWatcher();
this.fontWatcher = new FontWatcher();
this.themeWatcher.start();
this.fontWatcher.start();

initSentry(SdkConfig.get("sentry"));

if (!checkSessionLockFree()) {
// another instance holds the lock; confirm its theft before proceeding
setTimeout(() => this.setState({ view: Views.CONFIRM_LOCK_THEFT }), 0);
} else {
this.startInitSession();
}

window.addEventListener("resize", this.onWindowResized);
}

Expand All @@ -497,8 +496,8 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
public componentWillUnmount(): void {
Lifecycle.stopMatrixClient();
dis.unregister(this.dispatcherRef);
this.themeWatcher.stop();
this.fontWatcher.stop();
this.themeWatcher?.stop();
this.fontWatcher?.stop();
UIStore.destroy();
this.state.resizeNotifier.removeListener("middlePanelResized", this.dispatchTimelineResize);
window.removeEventListener("resize", this.onWindowResized);
Expand Down Expand Up @@ -1011,7 +1010,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {

this.setStateForNewView(newState);
ThemeController.isLogin = true;
this.themeWatcher.recheck();
this.themeWatcher?.recheck();
this.notifyNewScreen(isMobileRegistration ? "mobile_register" : "register");
}

Expand Down Expand Up @@ -1088,7 +1087,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
},
() => {
ThemeController.isLogin = false;
this.themeWatcher.recheck();
this.themeWatcher?.recheck();
this.notifyNewScreen("room/" + presentedId, replaceLast);
},
);
Expand All @@ -1113,7 +1112,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
});
this.notifyNewScreen("welcome");
ThemeController.isLogin = true;
this.themeWatcher.recheck();
this.themeWatcher?.recheck();
}

private viewLogin(otherState?: any): void {
Expand All @@ -1123,7 +1122,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
});
this.notifyNewScreen("login");
ThemeController.isLogin = true;
this.themeWatcher.recheck();
this.themeWatcher?.recheck();
}

private viewHome(justRegistered = false): void {
Expand All @@ -1136,7 +1135,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
this.setPage(PageType.HomePage);
this.notifyNewScreen("home");
ThemeController.isLogin = false;
this.themeWatcher.recheck();
this.themeWatcher?.recheck();
}

private viewUser(userId: string, subAction: string): void {
Expand Down Expand Up @@ -1357,7 +1356,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
*/
private async onLoggedIn(): Promise<void> {
ThemeController.isLogin = false;
this.themeWatcher.recheck();
this.themeWatcher?.recheck();
StorageManager.tryPersistStorage();

await this.onShowPostLoginScreen();
Expand Down
15 changes: 7 additions & 8 deletions src/components/structures/MessagePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ export default class MessagePanel extends React.Component<IProps, IState> {
private readReceiptsByUserId: Map<string, IReadReceiptForUser> = new Map();

private readonly _showHiddenEvents: boolean;
private isMounted = false;
private unmounted = false;

private readMarkerNode = createRef<HTMLLIElement>();
private whoIsTyping = createRef<WhoIsTypingTile>();
public scrollPanel = createRef<ScrollPanel>();

private readonly showTypingNotificationsWatcherRef: string;
private showTypingNotificationsWatcherRef?: string;
private eventTiles: Record<string, UnwrappedEventTile> = {};

// A map to allow groupers to maintain consistent keys even if their first event is uprooted due to back-pagination.
Expand All @@ -267,22 +267,21 @@ export default class MessagePanel extends React.Component<IProps, IState> {
// and we check this in a hot code path. This is also cached in our
// RoomContext, however we still need a fallback for roomless MessagePanels.
this._showHiddenEvents = SettingsStore.getValue("showHiddenEventsInTimeline");
}

public componentDidMount(): void {
this.unmounted = false;
this.showTypingNotificationsWatcherRef = SettingsStore.watchSetting(
"showTypingNotifications",
null,
this.onShowTypingNotificationsChange,
);
}

public componentDidMount(): void {
this.calculateRoomMembersCount();
this.props.room?.currentState.on(RoomStateEvent.Update, this.calculateRoomMembersCount);
this.isMounted = true;
}

public componentWillUnmount(): void {
this.isMounted = false;
this.unmounted = true;
this.props.room?.currentState.off(RoomStateEvent.Update, this.calculateRoomMembersCount);
SettingsStore.unwatchSetting(this.showTypingNotificationsWatcherRef);
this.readReceiptMap = {};
Expand Down Expand Up @@ -441,7 +440,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
}

private isUnmounting = (): boolean => {
return !this.isMounted;
return this.unmounted;
};

public get showHiddenEvents(): boolean {
Expand Down
2 changes: 2 additions & 0 deletions src/components/structures/NonUrgentToastContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default class NonUrgentToastContainer extends React.PureComponent<IProps,
this.state = {
toasts: NonUrgentToastStore.instance.components,
};
}

public componentDidMount(): void {
NonUrgentToastStore.instance.on(UPDATE_EVENT, this.onUpdateToasts);
}

Expand Down
6 changes: 2 additions & 4 deletions src/components/structures/RoomSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ interface IProps {
}

export default class RoomSearch extends React.PureComponent<IProps> {
private readonly dispatcherRef: string;

public constructor(props: IProps) {
super(props);
private dispatcherRef?: string;

public componentDidMount(): void {
this.dispatcherRef = defaultDispatcher.register(this.onAction);
}

Expand Down
2 changes: 2 additions & 0 deletions src/components/structures/RoomStatusBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export default class RoomStatusBar extends React.PureComponent<IProps, IState> {
}

public componentDidMount(): void {
this.unmounted = false;

const client = this.context;
client.on(ClientEvent.Sync, this.onSyncStateChange);
client.on(RoomEvent.LocalEchoUpdated, this.onRoomLocalEchoUpdated);
Expand Down
Loading

0 comments on commit 0899165

Please sign in to comment.