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
}

0 commit comments

Comments
 (0)