Skip to content

Commit cc52369

Browse files
richvdhsnowping
authored andcommitted
MatrixChat: only start session load once (element-hq#30642)
* MatrixChat: only start session load once When running in development mode, `MatrixChat.componentDidMount` is run twice, meaning it checks the session lock twice. Sometimes, this means it shows the "Element is running in another window" page. The real problem is of course that we're running all this application logic inside the `MatrixChat` component, but as a quick workaround, we can just remember that we've started the session load code, and bail out on the second pass. * Address review comments
1 parent d4b3684 commit cc52369

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

src/components/structures/MatrixChat.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
238238
private readonly stores: SdkContextClass;
239239
private loadSessionAbortController = new AbortController();
240240

241+
private sessionLoadStarted = false;
242+
241243
public constructor(props: IProps) {
242244
super(props);
243245
this.stores = SdkContextClass.instance;
@@ -470,15 +472,20 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
470472
this.fontWatcher.start();
471473

472474
initSentry(SdkConfig.get("sentry"));
475+
window.addEventListener("resize", this.onWindowResized);
473476

474-
if (!checkSessionLockFree()) {
475-
// another instance holds the lock; confirm its theft before proceeding
476-
setTimeout(() => this.setState({ view: Views.CONFIRM_LOCK_THEFT }), 0);
477-
} else {
478-
this.startInitSession();
477+
// Once we start loading the MatrixClient, we can't stop, even if MatrixChat gets unmounted (as it does
478+
// in React's Strict Mode). So, start loading the session now, but only if this MatrixChat was not previously
479+
// mounted.
480+
if (!this.sessionLoadStarted) {
481+
this.sessionLoadStarted = true;
482+
if (!checkSessionLockFree()) {
483+
// another instance holds the lock; confirm its theft before proceeding
484+
setTimeout(() => this.setState({ view: Views.CONFIRM_LOCK_THEFT }), 0);
485+
} else {
486+
this.startInitSession();
487+
}
479488
}
480-
481-
window.addEventListener("resize", this.onWindowResized);
482489
}
483490

484491
public componentDidUpdate(prevProps: IProps, prevState: IState): void {

0 commit comments

Comments
 (0)