Skip to content

Commit b2973b6

Browse files
committed
fix: Check for PlatformPeg instance in session lock.
1 parent da51e59 commit b2973b6

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/components/structures/MatrixChat.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
313313
private async initSession(): Promise<void> {
314314
// The Rust Crypto SDK will break if two Element instances try to use the same datastore at once, so
315315
// make sure we are the only Element instance in town (on this browser/domain).
316-
if (!(await PlatformPeg.get()?.getSessionLock(() => this.onSessionLockStolen()))) {
316+
const platform = PlatformPeg.get();
317+
if (platform && !(await platform.getSessionLock(() => this.onSessionLockStolen()))) {
317318
// we failed to get the lock. onSessionLockStolen should already have been called, so nothing left to do.
318319
return;
319320
}
@@ -478,7 +479,8 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
478479
// mounted.
479480
if (!this.sessionLoadStarted) {
480481
this.sessionLoadStarted = true;
481-
if (!PlatformPeg.get()?.checkSessionLockFree()) {
482+
const platform = PlatformPeg.get();
483+
if (platform && !platform.checkSessionLockFree()) {
482484
// another instance holds the lock; confirm its theft before proceeding
483485
setTimeout(() => this.setState({ view: Views.CONFIRM_LOCK_THEFT }), 0);
484486
} else {

test/test-utils/platform.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { type MethodLikeKeys, mocked, type MockedObject } from "jest-mock";
1010

1111
import BasePlatform from "../../src/BasePlatform";
1212
import PlatformPeg from "../../src/PlatformPeg";
13+
import * as SessionLock from "../../src/utils/SessionLock";
1314

1415
// doesn't implement abstract
1516
// @ts-ignore
@@ -18,6 +19,14 @@ class MockPlatform extends BasePlatform {
1819
super();
1920
Object.assign(this, platformMocks);
2021
}
22+
23+
public async checkSessionLockFree(): Promise<boolean> {
24+
return SessionLock.checkSessionLockFree();
25+
}
26+
27+
public async getSessionLock(onNewInstance: () => Promise<void>): Promise<boolean> {
28+
return SessionLock.getSessionLock(onNewInstance);
29+
}
2130
}
2231
/**
2332
* Mock Platform Peg

test/unit-tests/components/structures/MatrixChat-test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,10 @@ describe("<MatrixChat />", () => {
16321632
});
16331633

16341634
describe("Multi-tab lockout", () => {
1635+
beforeEach(() => {
1636+
mockPlatformPeg();
1637+
});
1638+
16351639
afterEach(() => {
16361640
Lifecycle.setSessionLockNotStolen();
16371641
});
@@ -1677,6 +1681,8 @@ describe("<MatrixChat />", () => {
16771681
beforeEach(() => {
16781682
// make sure we start from a clean DOM for each of these tests
16791683
document.body.replaceChildren();
1684+
// use the MockPlatform
1685+
mockPlatformPeg();
16801686
});
16811687

16821688
function simulateSessionLockClaim() {

0 commit comments

Comments
 (0)