Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 3a59556

Browse files
authored
Revert #124 and #135 (#139)
This seems to be causing a lot of weirdness, presumably because there's another missing thing like in #135, but I don't know what it might be and it feels like it might take a while to find. Backing these changes out to fix develop while we sort it out. Fixes element-hq/element-web#28179
1 parent 4e5cf1b commit 3a59556

30 files changed

+78
-88
lines changed

src/MatrixClientPeg.ts

-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import { formatList } from "./utils/FormattingUtils";
4343
import SdkConfig from "./SdkConfig";
4444
import { Features } from "./settings/Settings";
4545
import { setDeviceIsolationMode } from "./settings/controllers/DeviceIsolationModeController.ts";
46-
import { ReadyWatchingStore } from "./stores/ReadyWatchingStore.ts";
4746

4847
export interface IMatrixClientCreds {
4948
homeserverUrl: string;
@@ -310,7 +309,6 @@ class MatrixClientPegClass implements IMatrixClientPeg {
310309
MatrixActionCreators.start(this.matrixClient);
311310
MatrixClientBackedSettingsHandler.matrixClient = this.matrixClient;
312311
MatrixClientBackedController.matrixClient = this.matrixClient;
313-
ReadyWatchingStore.matrixClient = this.matrixClient;
314312

315313
return opts;
316314
}

src/stores/AsyncStoreWithClient.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@ export abstract class AsyncStoreWithClient<T extends Object> extends AsyncStore<
3636
})(dispatcher);
3737
}
3838

39-
protected async start(matrixClient: MatrixClient | null): Promise<void> {
40-
await this.readyStore.start(matrixClient);
41-
}
42-
43-
// XXX: This method is intended only for use in tests.
44-
public async useUnitTestClient(cli: MatrixClient): Promise<void> {
45-
await this.readyStore.useUnitTestClient(cli);
39+
public async start(): Promise<void> {
40+
await this.readyStore.start();
4641
}
4742

4843
public get matrixClient(): MatrixClient | null {

src/stores/AutoRageshakeStore.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ interface IState {
4646
*/
4747
export default class AutoRageshakeStore extends AsyncStoreWithClient<IState> {
4848
private static readonly internalInstance = (() => {
49-
return new AutoRageshakeStore();
49+
const instance = new AutoRageshakeStore();
50+
instance.start();
51+
return instance;
5052
})();
5153

5254
private constructor() {

src/stores/BreadcrumbsStore.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ interface IState {
3030

3131
export class BreadcrumbsStore extends AsyncStoreWithClient<IState> {
3232
private static readonly internalInstance = (() => {
33-
return new BreadcrumbsStore();
33+
const instance = new BreadcrumbsStore();
34+
instance.start();
35+
return instance;
3436
})();
3537

3638
private waitingRooms: { roomId: string; addedTs: number }[] = [];

src/stores/CallStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class CallStore extends AsyncStoreWithClient<{}> {
3131
public static get instance(): CallStore {
3232
if (!this._instance) {
3333
this._instance = new CallStore();
34+
this._instance.start();
3435
}
3536
return this._instance;
3637
}

src/stores/ModalWidgetStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface IState {
2424
export class ModalWidgetStore extends AsyncStoreWithClient<IState> {
2525
private static readonly internalInstance = (() => {
2626
const instance = new ModalWidgetStore();
27+
instance.start();
2728
return instance;
2829
})();
2930
private modalInstance: IHandle<typeof ModalWidgetDialog> | null = null;

src/stores/OwnBeaconStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const getLocallyCreatedBeaconEventIds = (): string[] => {
8787
export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
8888
private static readonly internalInstance = (() => {
8989
const instance = new OwnBeaconStore();
90+
instance.start();
9091
return instance;
9192
})();
9293
// users beacons, keyed by event type

src/stores/OwnProfileStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const KEY_AVATAR_URL = "mx_profile_avatar_url";
2828
export class OwnProfileStore extends AsyncStoreWithClient<IState> {
2929
private static readonly internalInstance = (() => {
3030
const instance = new OwnProfileStore();
31+
instance.start();
3132
return instance;
3233
})();
3334

src/stores/ReadyWatchingStore.ts

+11-28
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,27 @@
99
import { MatrixClient, SyncState } from "matrix-js-sdk/src/matrix";
1010
import { EventEmitter } from "events";
1111

12+
import { MatrixClientPeg } from "../MatrixClientPeg";
1213
import { ActionPayload } from "../dispatcher/payloads";
1314
import { IDestroyable } from "../utils/IDestroyable";
1415
import { Action } from "../dispatcher/actions";
1516
import { MatrixDispatcher } from "../dispatcher/dispatcher";
1617

1718
export abstract class ReadyWatchingStore extends EventEmitter implements IDestroyable {
18-
private static instances: ReadyWatchingStore[] = [];
19-
protected _matrixClient: MatrixClient | null = null;
19+
protected matrixClient: MatrixClient | null = null;
2020
private dispatcherRef: string | null = null;
2121

22-
public static set matrixClient(client: MatrixClient) {
23-
for (const instance of ReadyWatchingStore.instances) {
24-
instance.start(client);
25-
}
26-
}
27-
2822
public constructor(protected readonly dispatcher: MatrixDispatcher) {
2923
super();
30-
31-
this.dispatcherRef = this.dispatcher.register(this.onAction);
32-
33-
ReadyWatchingStore.instances.push(this);
34-
}
35-
36-
public get matrixClient(): MatrixClient | null {
37-
return this._matrixClient;
3824
}
3925

40-
public async start(matrixClient: MatrixClient | null): Promise<void> {
41-
const oldClient = this._matrixClient;
42-
this._matrixClient = matrixClient;
26+
public async start(): Promise<void> {
27+
this.dispatcherRef = this.dispatcher.register(this.onAction);
4328

44-
if (oldClient !== matrixClient) {
45-
await this.onNotReady();
46-
}
29+
// MatrixClientPeg can be undefined in tests because of circular dependencies with other stores
30+
const matrixClient = MatrixClientPeg?.get();
4731
if (matrixClient) {
32+
this.matrixClient = matrixClient;
4833
await this.onReady();
4934
}
5035
}
@@ -53,10 +38,8 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
5338
return this.matrixClient; // for external readonly access
5439
}
5540

56-
// XXX: This method is intended only for use in tests.
57-
public async useUnitTestClient(cli: MatrixClient): Promise<void> {
58-
this._matrixClient = cli;
59-
await this.onReady();
41+
public useUnitTestClient(cli: MatrixClient): void {
42+
this.matrixClient = cli;
6043
}
6144

6245
public destroy(): void {
@@ -91,13 +74,13 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
9174
if (this.matrixClient) {
9275
await this.onNotReady();
9376
}
94-
this._matrixClient = payload.matrixClient;
77+
this.matrixClient = payload.matrixClient;
9578
await this.onReady();
9679
}
9780
} else if (payload.action === "on_client_not_viable" || payload.action === Action.OnLoggedOut) {
9881
if (this.matrixClient) {
9982
await this.onNotReady();
100-
this._matrixClient = null;
83+
this.matrixClient = null;
10184
}
10285
}
10386
};

src/stores/VoiceRecordingStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class VoiceRecordingStore extends AsyncStoreWithClient<IState> {
3030
public static get instance(): VoiceRecordingStore {
3131
if (!this.internalInstance) {
3232
this.internalInstance = new VoiceRecordingStore();
33+
this.internalInstance.start();
3334
}
3435
return this.internalInstance;
3536
}

src/stores/WidgetStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ interface IRoomWidgets {
4545
export default class WidgetStore extends AsyncStoreWithClient<IState> {
4646
private static readonly internalInstance = (() => {
4747
const instance = new WidgetStore();
48+
instance.start();
4849
return instance;
4950
})();
5051

src/stores/local-echo/EchoStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class EchoStore extends AsyncStoreWithClient<IState> {
3838
public static get instance(): EchoStore {
3939
if (!this._instance) {
4040
this._instance = new EchoStore();
41+
this._instance.start();
4142
}
4243
return this._instance;
4344
}

src/stores/notifications/RoomNotificationStateStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const UPDATE_STATUS_INDICATOR = Symbol("update-status-indicator");
2626
export class RoomNotificationStateStore extends AsyncStoreWithClient<IState> {
2727
private static readonly internalInstance = (() => {
2828
const instance = new RoomNotificationStateStore();
29+
instance.start();
2930
return instance;
3031
})();
3132
private roomMap = new Map<Room, RoomNotificationState>();

src/stores/right-panel/RightPanelStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ export default class RightPanelStore extends ReadyWatchingStore {
403403
public static get instance(): RightPanelStore {
404404
if (!this.internalInstance) {
405405
this.internalInstance = new RightPanelStore();
406+
this.internalInstance.start();
406407
}
407408
return this.internalInstance;
408409
}

src/stores/room-list/MessagePreviewStore.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ const mkMessagePreview = (text: string, event: MatrixEvent): MessagePreview => {
124124
};
125125

126126
export class MessagePreviewStore extends AsyncStoreWithClient<IState> {
127-
private static readonly internalInstance = (() => new MessagePreviewStore())();
127+
private static readonly internalInstance = (() => {
128+
const instance = new MessagePreviewStore();
129+
instance.start();
130+
return instance;
131+
})();
128132

129133
/**
130134
* @internal Public for test only

src/stores/room-list/RoomListLayoutStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default class RoomListLayoutStore extends AsyncStoreWithClient<IState> {
2828
public static get instance(): RoomListLayoutStore {
2929
if (!this.internalInstance) {
3030
this.internalInstance = new RoomListLayoutStore();
31+
this.internalInstance.start();
3132
}
3233
return RoomListLayoutStore.internalInstance;
3334
}

src/stores/room-list/RoomListStore.ts

+2
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,11 @@ export default class RoomListStore {
643643
if (SettingsStore.getValue("feature_sliding_sync")) {
644644
logger.info("using SlidingRoomListStoreClass");
645645
const instance = new SlidingRoomListStoreClass(defaultDispatcher, SdkContextClass.instance);
646+
instance.start();
646647
RoomListStore.internalInstance = instance;
647648
} else {
648649
const instance = new RoomListStoreClass(defaultDispatcher);
650+
instance.start();
649651
RoomListStore.internalInstance = instance;
650652
}
651653
}

src/stores/spaces/SpaceStore.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
MatrixEvent,
1818
ClientEvent,
1919
ISendEventResponse,
20-
MatrixClient,
2120
} from "matrix-js-sdk/src/matrix";
2221
import { KnownMembership } from "matrix-js-sdk/src/types";
2322
import { logger } from "matrix-js-sdk/src/logger";
@@ -1398,6 +1397,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
13981397
export default class SpaceStore {
13991398
private static readonly internalInstance = (() => {
14001399
const instance = new SpaceStoreClass();
1400+
instance.start();
14011401
return instance;
14021402
})();
14031403

@@ -1408,9 +1408,9 @@ export default class SpaceStore {
14081408
/**
14091409
* @internal for test only
14101410
*/
1411-
public static testInstance(client: MatrixClient): SpaceStoreClass {
1411+
public static testInstance(): SpaceStoreClass {
14121412
const store = new SpaceStoreClass();
1413-
store.useUnitTestClient(client);
1413+
store.start();
14141414
return store;
14151415
}
14161416
}

src/stores/widgets/WidgetLayoutStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
6060
public static get instance(): WidgetLayoutStore {
6161
if (!this.internalInstance) {
6262
this.internalInstance = new WidgetLayoutStore();
63+
this.internalInstance.start();
6364
}
6465
return this.internalInstance;
6566
}

src/stores/widgets/WidgetMessagingStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export enum WidgetMessagingStoreEvent {
2727
export class WidgetMessagingStore extends AsyncStoreWithClient<{}> {
2828
private static readonly internalInstance = (() => {
2929
const instance = new WidgetMessagingStore();
30+
instance.start();
3031
return instance;
3132
})();
3233

test/MatrixClientPeg-test.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
66
Please see LICENSE files in the repository root for full details.
77
*/
88

9-
import * as MatrixJs from "matrix-js-sdk/src/matrix";
109
import { logger } from "matrix-js-sdk/src/logger";
10+
import fetchMockJest from "fetch-mock-jest";
1111

1212
import { advanceDateAndTime, stubClient } from "./test-utils";
1313
import { IMatrixClientPeg, MatrixClientPeg as peg } from "../src/MatrixClientPeg";
@@ -19,14 +19,9 @@ jest.useFakeTimers();
1919
const PegClass = Object.getPrototypeOf(peg).constructor;
2020

2121
describe("MatrixClientPeg", () => {
22-
let mockClient: MatrixJs.MatrixClient;
23-
2422
beforeEach(() => {
2523
// stub out Logger.log which gets called a lot and clutters up the test output
2624
jest.spyOn(logger, "log").mockImplementation(() => {});
27-
28-
mockClient = stubClient();
29-
jest.spyOn(MatrixJs, "createClient").mockReturnValue(mockClient);
3025
});
3126

3227
afterEach(() => {
@@ -38,6 +33,7 @@ describe("MatrixClientPeg", () => {
3833
});
3934

4035
it("setJustRegisteredUserId", () => {
36+
stubClient();
4137
(peg as any).matrixClient = peg.get();
4238
peg.setJustRegisteredUserId("@userId:matrix.org");
4339
expect(peg.safeGet().credentials.userId).toBe("@userId:matrix.org");
@@ -56,6 +52,7 @@ describe("MatrixClientPeg", () => {
5652
});
5753

5854
it("setJustRegisteredUserId(null)", () => {
55+
stubClient();
5956
(peg as any).matrixClient = peg.get();
6057
peg.setJustRegisteredUserId(null);
6158
expect(peg.currentUserIsJustRegistered()).toBe(false);
@@ -74,6 +71,7 @@ describe("MatrixClientPeg", () => {
7471
beforeEach(() => {
7572
// instantiate a MatrixClientPegClass instance, with a new MatrixClient
7673
testPeg = new PegClass();
74+
fetchMockJest.get("http://example.com/_matrix/client/versions", {});
7775
testPeg.replaceUsingCreds({
7876
accessToken: "SEKRET",
7977
homeserverUrl: "http://example.com",
@@ -85,20 +83,24 @@ describe("MatrixClientPeg", () => {
8583
it("should initialise the rust crypto library by default", async () => {
8684
const mockSetValue = jest.spyOn(SettingsStore, "setValue").mockResolvedValue(undefined);
8785

86+
const mockInitCrypto = jest.spyOn(testPeg.safeGet(), "initCrypto").mockResolvedValue(undefined);
87+
const mockInitRustCrypto = jest.spyOn(testPeg.safeGet(), "initRustCrypto").mockResolvedValue(undefined);
88+
8889
const cryptoStoreKey = new Uint8Array([1, 2, 3, 4]);
8990
await testPeg.start({ rustCryptoStoreKey: cryptoStoreKey });
90-
expect(mockClient.initCrypto).not.toHaveBeenCalled();
91-
expect(mockClient.initRustCrypto).toHaveBeenCalledWith({ storageKey: cryptoStoreKey });
91+
expect(mockInitCrypto).not.toHaveBeenCalled();
92+
expect(mockInitRustCrypto).toHaveBeenCalledWith({ storageKey: cryptoStoreKey });
9293

9394
// we should have stashed the setting in the settings store
9495
expect(mockSetValue).toHaveBeenCalledWith("feature_rust_crypto", null, SettingLevel.DEVICE, true);
9596
});
9697

9798
it("Should migrate existing login", async () => {
9899
const mockSetValue = jest.spyOn(SettingsStore, "setValue").mockResolvedValue(undefined);
100+
const mockInitRustCrypto = jest.spyOn(testPeg.safeGet(), "initRustCrypto").mockResolvedValue(undefined);
99101

100102
await testPeg.start();
101-
expect(mockClient.initRustCrypto).toHaveBeenCalledTimes(1);
103+
expect(mockInitRustCrypto).toHaveBeenCalledTimes(1);
102104

103105
// we should have stashed the setting in the settings store
104106
expect(mockSetValue).toHaveBeenCalledWith("feature_rust_crypto", null, SettingLevel.DEVICE, true);

test/stores/AutoRageshakeStore-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe("AutoRageshakeStore", () => {
4747

4848
// @ts-ignore bypass private ctor for tests
4949
autoRageshakeStore = new AutoRageshakeStore();
50-
autoRageshakeStore.useUnitTestClient(client);
50+
autoRageshakeStore.start();
5151

5252
utdEvent = mkEvent({
5353
event: true,

test/stores/OwnProfileStore-test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("OwnProfileStore", () => {
4040
displayname: "Display Name",
4141
avatar_url: "mxc://example.com/abc123",
4242
});
43-
await ownProfileStore.useUnitTestClient(client);
43+
await ownProfileStore.start();
4444

4545
expect(onUpdate).toHaveBeenCalled();
4646
expect(ownProfileStore.displayName).toBe("Display Name");
@@ -54,7 +54,7 @@ describe("OwnProfileStore", () => {
5454
errcode: "M_NOT_FOUND",
5555
}),
5656
);
57-
await ownProfileStore.useUnitTestClient(client);
57+
await ownProfileStore.start();
5858

5959
expect(onUpdate).toHaveBeenCalled();
6060
expect(ownProfileStore.displayName).toBe(client.getSafeUserId());
@@ -69,7 +69,7 @@ describe("OwnProfileStore", () => {
6969
}),
7070
);
7171
try {
72-
await ownProfileStore.useUnitTestClient(client);
72+
await ownProfileStore.start();
7373
} catch (ignore) {}
7474

7575
expect(onUpdate).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)