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

Commit 197168b

Browse files
authored
Refactor stores and their relationship to the MatrixClientPeg (#124)
* Refactor stores and their relationship to the MatrixClientPeg to avoid import cycles and webpack weirdness Signed-off-by: Michael Telatynski <[email protected]> * Iterate Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 31bd10e commit 197168b

28 files changed

+71
-66
lines changed

src/MatrixClientPeg.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ 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";
4647

4748
export interface IMatrixClientCreds {
4849
homeserverUrl: string;
@@ -309,6 +310,7 @@ class MatrixClientPegClass implements IMatrixClientPeg {
309310
MatrixActionCreators.start(this.matrixClient);
310311
MatrixClientBackedSettingsHandler.matrixClient = this.matrixClient;
311312
MatrixClientBackedController.matrixClient = this.matrixClient;
313+
ReadyWatchingStore.matrixClient = this.matrixClient;
312314

313315
return opts;
314316
}

src/stores/AsyncStoreWithClient.ts

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

39-
public async start(): Promise<void> {
40-
await this.readyStore.start();
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);
4146
}
4247

4348
public get matrixClient(): MatrixClient | null {

src/stores/AutoRageshakeStore.ts

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

5452
private constructor() {

src/stores/BreadcrumbsStore.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ interface IState {
3030

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

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

src/stores/CallStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export class CallStore extends AsyncStoreWithClient<{}> {
3131
public static get instance(): CallStore {
3232
if (!this._instance) {
3333
this._instance = new CallStore();
34-
this._instance.start();
3534
}
3635
return this._instance;
3736
}

src/stores/ModalWidgetStore.ts

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

src/stores/OwnBeaconStore.ts

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

src/stores/OwnProfileStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ 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();
3231
return instance;
3332
})();
3433

src/stores/ReadyWatchingStore.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,40 @@
99
import { MatrixClient, SyncState } from "matrix-js-sdk/src/matrix";
1010
import { EventEmitter } from "events";
1111

12-
import { MatrixClientPeg } from "../MatrixClientPeg";
1312
import { ActionPayload } from "../dispatcher/payloads";
1413
import { IDestroyable } from "../utils/IDestroyable";
1514
import { Action } from "../dispatcher/actions";
1615
import { MatrixDispatcher } from "../dispatcher/dispatcher";
1716

1817
export abstract class ReadyWatchingStore extends EventEmitter implements IDestroyable {
19-
protected matrixClient: MatrixClient | null = null;
18+
private static instances: ReadyWatchingStore[] = [];
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+
2228
public constructor(protected readonly dispatcher: MatrixDispatcher) {
2329
super();
24-
}
2530

26-
public async start(): Promise<void> {
2731
this.dispatcherRef = this.dispatcher.register(this.onAction);
32+
}
2833

29-
// MatrixClientPeg can be undefined in tests because of circular dependencies with other stores
30-
const matrixClient = MatrixClientPeg?.get();
34+
public get matrixClient(): MatrixClient | null {
35+
return this._matrixClient;
36+
}
37+
38+
public async start(matrixClient: MatrixClient | null): Promise<void> {
39+
const oldClient = this._matrixClient;
40+
this._matrixClient = matrixClient;
41+
42+
if (oldClient !== matrixClient) {
43+
await this.onNotReady();
44+
}
3145
if (matrixClient) {
32-
this.matrixClient = matrixClient;
3346
await this.onReady();
3447
}
3548
}
@@ -38,8 +51,10 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
3851
return this.matrixClient; // for external readonly access
3952
}
4053

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

4560
public destroy(): void {
@@ -74,13 +89,13 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
7489
if (this.matrixClient) {
7590
await this.onNotReady();
7691
}
77-
this.matrixClient = payload.matrixClient;
92+
this._matrixClient = payload.matrixClient;
7893
await this.onReady();
7994
}
8095
} else if (payload.action === "on_client_not_viable" || payload.action === Action.OnLoggedOut) {
8196
if (this.matrixClient) {
8297
await this.onNotReady();
83-
this.matrixClient = null;
98+
this._matrixClient = null;
8499
}
85100
}
86101
};

src/stores/VoiceRecordingStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ 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();
3433
}
3534
return this.internalInstance;
3635
}

src/stores/WidgetStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ interface IRoomWidgets {
4545
export default class WidgetStore extends AsyncStoreWithClient<IState> {
4646
private static readonly internalInstance = (() => {
4747
const instance = new WidgetStore();
48-
instance.start();
4948
return instance;
5049
})();
5150

src/stores/local-echo/EchoStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ 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();
4241
}
4342
return this._instance;
4443
}

src/stores/notifications/RoomNotificationStateStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ 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();
3029
return instance;
3130
})();
3231
private roomMap = new Map<Room, RoomNotificationState>();

src/stores/right-panel/RightPanelStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ 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();
407406
}
408407
return this.internalInstance;
409408
}

src/stores/room-list/MessagePreviewStore.ts

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

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

133129
/**
134130
* @internal Public for test only

src/stores/room-list/RoomListLayoutStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ 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();
3231
}
3332
return RoomListLayoutStore.internalInstance;
3433
}

src/stores/room-list/RoomListStore.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,9 @@ 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();
647646
RoomListStore.internalInstance = instance;
648647
} else {
649648
const instance = new RoomListStoreClass(defaultDispatcher);
650-
instance.start();
651649
RoomListStore.internalInstance = instance;
652650
}
653651
}

src/stores/spaces/SpaceStore.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
MatrixEvent,
1818
ClientEvent,
1919
ISendEventResponse,
20+
MatrixClient,
2021
} from "matrix-js-sdk/src/matrix";
2122
import { KnownMembership } from "matrix-js-sdk/src/types";
2223
import { logger } from "matrix-js-sdk/src/logger";
@@ -1397,7 +1398,6 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
13971398
export default class SpaceStore {
13981399
private static readonly internalInstance = (() => {
13991400
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(): SpaceStoreClass {
1411+
public static testInstance(client: MatrixClient): SpaceStoreClass {
14121412
const store = new SpaceStoreClass();
1413-
store.start();
1413+
store.useUnitTestClient(client);
14141414
return store;
14151415
}
14161416
}

src/stores/widgets/WidgetLayoutStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
6060
public static get instance(): WidgetLayoutStore {
6161
if (!this.internalInstance) {
6262
this.internalInstance = new WidgetLayoutStore();
63-
this.internalInstance.start();
6463
}
6564
return this.internalInstance;
6665
}

src/stores/widgets/WidgetMessagingStore.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export enum WidgetMessagingStoreEvent {
2727
export class WidgetMessagingStore extends AsyncStoreWithClient<{}> {
2828
private static readonly internalInstance = (() => {
2929
const instance = new WidgetMessagingStore();
30-
instance.start();
3130
return instance;
3231
})();
3332

test/stores/AutoRageshakeStore-test.ts

Lines changed: 1 addition & 1 deletion
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.start();
50+
autoRageshakeStore.useUnitTestClient(client);
5151

5252
utdEvent = mkEvent({
5353
event: true,

test/stores/OwnProfileStore-test.ts

Lines changed: 3 additions & 3 deletions
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.start();
43+
await ownProfileStore.useUnitTestClient(client);
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.start();
57+
await ownProfileStore.useUnitTestClient(client);
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.start();
72+
await ownProfileStore.useUnitTestClient(client);
7373
} catch (ignore) {}
7474

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

test/stores/SpaceStore-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ describe("SpaceStore", () => {
14281428

14291429
it("passes that value in calls to getVisibleRooms during getSpaceFilteredRoomIds", () => {
14301430
// Given a store
1431-
const store = SpaceStore.testInstance();
1431+
const store = SpaceStore.testInstance(client);
14321432

14331433
// When we ask for filtered room ids
14341434
store.getSpaceFilteredRoomIds(MetaSpace.Home);
@@ -1478,7 +1478,7 @@ describe("SpaceStore", () => {
14781478

14791479
it("passes that value in calls to getVisibleRooms during getSpaceFilteredRoomIds", () => {
14801480
// Given a store
1481-
const store = SpaceStore.testInstance();
1481+
const store = SpaceStore.testInstance(client);
14821482
// When we ask for filtered room ids
14831483
store.getSpaceFilteredRoomIds(MetaSpace.Home);
14841484

test/stores/VoiceRecordingStore-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe("VoiceRecordingStore", () => {
3131

3232
const mkStore = (): VoiceRecordingStore => {
3333
const store = new VoiceRecordingStore();
34-
store.start();
34+
store.useUnitTestClient(stubClient);
3535
return store;
3636
};
3737

test/stores/WidgetLayoutStore-test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ describe("WidgetLayoutStore", () => {
167167

168168
it("should recalculate all rooms when the client is ready", async () => {
169169
mocked(client.getVisibleRooms).mockReturnValue([mockRoom]);
170-
await store.start();
170+
await store.start(client);
171171

172172
expect(roomUpdateListener).toHaveBeenCalled();
173173
expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([]);
@@ -243,7 +243,7 @@ describe("WidgetLayoutStore", () => {
243243
});
244244

245245
it("should copy the layout to the room", async () => {
246-
await store.start();
246+
await store.start(client);
247247
store.recalculateRoom(mockRoom);
248248
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
249249
store.copyLayoutToRoom(mockRoom);
@@ -297,7 +297,7 @@ describe("WidgetLayoutStore", () => {
297297
mocked(client.getVisibleRooms).mockReturnValue([]);
298298
// @ts-ignore bypass private ctor for tests
299299
const store = new WidgetLayoutStore();
300-
await store.start();
300+
await store.start(client);
301301
expect(client.getVisibleRooms).toHaveBeenCalledWith(false);
302302
});
303303
});
@@ -314,7 +314,7 @@ describe("WidgetLayoutStore", () => {
314314
mocked(client.getVisibleRooms).mockReturnValue([]);
315315
// @ts-ignore bypass private ctor for tests
316316
const store = new WidgetLayoutStore();
317-
await store.start();
317+
await store.start(client);
318318
expect(client.getVisibleRooms).toHaveBeenCalledWith(true);
319319
});
320320
});

test/stores/room-list/MessagePreviewStore-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe("MessagePreviewStore", () => {
8080
mocked(client.getRoom).mockReturnValue(room);
8181

8282
store = MessagePreviewStore.testInstance();
83-
await store.start();
83+
await store.useUnitTestClient(client);
8484
await setupAsyncStoreWithClient(store, client);
8585
});
8686

0 commit comments

Comments
 (0)