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 <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
committed by
GitHub
parent
31bd10e887
commit
197168b3ba
@@ -43,6 +43,7 @@ import { formatList } from "./utils/FormattingUtils";
|
||||
import SdkConfig from "./SdkConfig";
|
||||
import { Features } from "./settings/Settings";
|
||||
import { setDeviceIsolationMode } from "./settings/controllers/DeviceIsolationModeController.ts";
|
||||
import { ReadyWatchingStore } from "./stores/ReadyWatchingStore.ts";
|
||||
|
||||
export interface IMatrixClientCreds {
|
||||
homeserverUrl: string;
|
||||
@@ -309,6 +310,7 @@ class MatrixClientPegClass implements IMatrixClientPeg {
|
||||
MatrixActionCreators.start(this.matrixClient);
|
||||
MatrixClientBackedSettingsHandler.matrixClient = this.matrixClient;
|
||||
MatrixClientBackedController.matrixClient = this.matrixClient;
|
||||
ReadyWatchingStore.matrixClient = this.matrixClient;
|
||||
|
||||
return opts;
|
||||
}
|
||||
|
||||
@@ -36,8 +36,13 @@ export abstract class AsyncStoreWithClient<T extends Object> extends AsyncStore<
|
||||
})(dispatcher);
|
||||
}
|
||||
|
||||
public async start(): Promise<void> {
|
||||
await this.readyStore.start();
|
||||
protected async start(matrixClient: MatrixClient | null): Promise<void> {
|
||||
await this.readyStore.start(matrixClient);
|
||||
}
|
||||
|
||||
// XXX: This method is intended only for use in tests.
|
||||
public async useUnitTestClient(cli: MatrixClient): Promise<void> {
|
||||
await this.readyStore.useUnitTestClient(cli);
|
||||
}
|
||||
|
||||
public get matrixClient(): MatrixClient | null {
|
||||
|
||||
@@ -46,9 +46,7 @@ interface IState {
|
||||
*/
|
||||
export default class AutoRageshakeStore extends AsyncStoreWithClient<IState> {
|
||||
private static readonly internalInstance = (() => {
|
||||
const instance = new AutoRageshakeStore();
|
||||
instance.start();
|
||||
return instance;
|
||||
return new AutoRageshakeStore();
|
||||
})();
|
||||
|
||||
private constructor() {
|
||||
|
||||
@@ -30,9 +30,7 @@ interface IState {
|
||||
|
||||
export class BreadcrumbsStore extends AsyncStoreWithClient<IState> {
|
||||
private static readonly internalInstance = (() => {
|
||||
const instance = new BreadcrumbsStore();
|
||||
instance.start();
|
||||
return instance;
|
||||
return new BreadcrumbsStore();
|
||||
})();
|
||||
|
||||
private waitingRooms: { roomId: string; addedTs: number }[] = [];
|
||||
|
||||
@@ -31,7 +31,6 @@ export class CallStore extends AsyncStoreWithClient<{}> {
|
||||
public static get instance(): CallStore {
|
||||
if (!this._instance) {
|
||||
this._instance = new CallStore();
|
||||
this._instance.start();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ interface IState {
|
||||
export class ModalWidgetStore extends AsyncStoreWithClient<IState> {
|
||||
private static readonly internalInstance = (() => {
|
||||
const instance = new ModalWidgetStore();
|
||||
instance.start();
|
||||
return instance;
|
||||
})();
|
||||
private modalInstance: IHandle<typeof ModalWidgetDialog> | null = null;
|
||||
|
||||
@@ -87,7 +87,6 @@ const getLocallyCreatedBeaconEventIds = (): string[] => {
|
||||
export class OwnBeaconStore extends AsyncStoreWithClient<OwnBeaconStoreState> {
|
||||
private static readonly internalInstance = (() => {
|
||||
const instance = new OwnBeaconStore();
|
||||
instance.start();
|
||||
return instance;
|
||||
})();
|
||||
// users beacons, keyed by event type
|
||||
|
||||
@@ -28,7 +28,6 @@ const KEY_AVATAR_URL = "mx_profile_avatar_url";
|
||||
export class OwnProfileStore extends AsyncStoreWithClient<IState> {
|
||||
private static readonly internalInstance = (() => {
|
||||
const instance = new OwnProfileStore();
|
||||
instance.start();
|
||||
return instance;
|
||||
})();
|
||||
|
||||
|
||||
@@ -9,27 +9,40 @@
|
||||
import { MatrixClient, SyncState } from "matrix-js-sdk/src/matrix";
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
import { MatrixClientPeg } from "../MatrixClientPeg";
|
||||
import { ActionPayload } from "../dispatcher/payloads";
|
||||
import { IDestroyable } from "../utils/IDestroyable";
|
||||
import { Action } from "../dispatcher/actions";
|
||||
import { MatrixDispatcher } from "../dispatcher/dispatcher";
|
||||
|
||||
export abstract class ReadyWatchingStore extends EventEmitter implements IDestroyable {
|
||||
protected matrixClient: MatrixClient | null = null;
|
||||
private static instances: ReadyWatchingStore[] = [];
|
||||
protected _matrixClient: MatrixClient | null = null;
|
||||
private dispatcherRef: string | null = null;
|
||||
|
||||
public static set matrixClient(client: MatrixClient) {
|
||||
for (const instance of ReadyWatchingStore.instances) {
|
||||
instance.start(client);
|
||||
}
|
||||
}
|
||||
|
||||
public constructor(protected readonly dispatcher: MatrixDispatcher) {
|
||||
super();
|
||||
|
||||
this.dispatcherRef = this.dispatcher.register(this.onAction);
|
||||
}
|
||||
|
||||
public async start(): Promise<void> {
|
||||
this.dispatcherRef = this.dispatcher.register(this.onAction);
|
||||
public get matrixClient(): MatrixClient | null {
|
||||
return this._matrixClient;
|
||||
}
|
||||
|
||||
// MatrixClientPeg can be undefined in tests because of circular dependencies with other stores
|
||||
const matrixClient = MatrixClientPeg?.get();
|
||||
public async start(matrixClient: MatrixClient | null): Promise<void> {
|
||||
const oldClient = this._matrixClient;
|
||||
this._matrixClient = matrixClient;
|
||||
|
||||
if (oldClient !== matrixClient) {
|
||||
await this.onNotReady();
|
||||
}
|
||||
if (matrixClient) {
|
||||
this.matrixClient = matrixClient;
|
||||
await this.onReady();
|
||||
}
|
||||
}
|
||||
@@ -38,8 +51,10 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
|
||||
return this.matrixClient; // for external readonly access
|
||||
}
|
||||
|
||||
public useUnitTestClient(cli: MatrixClient): void {
|
||||
this.matrixClient = cli;
|
||||
// XXX: This method is intended only for use in tests.
|
||||
public async useUnitTestClient(cli: MatrixClient): Promise<void> {
|
||||
this._matrixClient = cli;
|
||||
await this.onReady();
|
||||
}
|
||||
|
||||
public destroy(): void {
|
||||
@@ -74,13 +89,13 @@ export abstract class ReadyWatchingStore extends EventEmitter implements IDestro
|
||||
if (this.matrixClient) {
|
||||
await this.onNotReady();
|
||||
}
|
||||
this.matrixClient = payload.matrixClient;
|
||||
this._matrixClient = payload.matrixClient;
|
||||
await this.onReady();
|
||||
}
|
||||
} else if (payload.action === "on_client_not_viable" || payload.action === Action.OnLoggedOut) {
|
||||
if (this.matrixClient) {
|
||||
await this.onNotReady();
|
||||
this.matrixClient = null;
|
||||
this._matrixClient = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -30,7 +30,6 @@ export class VoiceRecordingStore extends AsyncStoreWithClient<IState> {
|
||||
public static get instance(): VoiceRecordingStore {
|
||||
if (!this.internalInstance) {
|
||||
this.internalInstance = new VoiceRecordingStore();
|
||||
this.internalInstance.start();
|
||||
}
|
||||
return this.internalInstance;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@ interface IRoomWidgets {
|
||||
export default class WidgetStore extends AsyncStoreWithClient<IState> {
|
||||
private static readonly internalInstance = (() => {
|
||||
const instance = new WidgetStore();
|
||||
instance.start();
|
||||
return instance;
|
||||
})();
|
||||
|
||||
|
||||
@@ -38,7 +38,6 @@ export class EchoStore extends AsyncStoreWithClient<IState> {
|
||||
public static get instance(): EchoStore {
|
||||
if (!this._instance) {
|
||||
this._instance = new EchoStore();
|
||||
this._instance.start();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ export const UPDATE_STATUS_INDICATOR = Symbol("update-status-indicator");
|
||||
export class RoomNotificationStateStore extends AsyncStoreWithClient<IState> {
|
||||
private static readonly internalInstance = (() => {
|
||||
const instance = new RoomNotificationStateStore();
|
||||
instance.start();
|
||||
return instance;
|
||||
})();
|
||||
private roomMap = new Map<Room, RoomNotificationState>();
|
||||
|
||||
@@ -403,7 +403,6 @@ export default class RightPanelStore extends ReadyWatchingStore {
|
||||
public static get instance(): RightPanelStore {
|
||||
if (!this.internalInstance) {
|
||||
this.internalInstance = new RightPanelStore();
|
||||
this.internalInstance.start();
|
||||
}
|
||||
return this.internalInstance;
|
||||
}
|
||||
|
||||
@@ -124,11 +124,7 @@ const mkMessagePreview = (text: string, event: MatrixEvent): MessagePreview => {
|
||||
};
|
||||
|
||||
export class MessagePreviewStore extends AsyncStoreWithClient<IState> {
|
||||
private static readonly internalInstance = (() => {
|
||||
const instance = new MessagePreviewStore();
|
||||
instance.start();
|
||||
return instance;
|
||||
})();
|
||||
private static readonly internalInstance = (() => new MessagePreviewStore())();
|
||||
|
||||
/**
|
||||
* @internal Public for test only
|
||||
|
||||
@@ -28,7 +28,6 @@ export default class RoomListLayoutStore extends AsyncStoreWithClient<IState> {
|
||||
public static get instance(): RoomListLayoutStore {
|
||||
if (!this.internalInstance) {
|
||||
this.internalInstance = new RoomListLayoutStore();
|
||||
this.internalInstance.start();
|
||||
}
|
||||
return RoomListLayoutStore.internalInstance;
|
||||
}
|
||||
|
||||
@@ -643,11 +643,9 @@ export default class RoomListStore {
|
||||
if (SettingsStore.getValue("feature_sliding_sync")) {
|
||||
logger.info("using SlidingRoomListStoreClass");
|
||||
const instance = new SlidingRoomListStoreClass(defaultDispatcher, SdkContextClass.instance);
|
||||
instance.start();
|
||||
RoomListStore.internalInstance = instance;
|
||||
} else {
|
||||
const instance = new RoomListStoreClass(defaultDispatcher);
|
||||
instance.start();
|
||||
RoomListStore.internalInstance = instance;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
MatrixEvent,
|
||||
ClientEvent,
|
||||
ISendEventResponse,
|
||||
MatrixClient,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { KnownMembership } from "matrix-js-sdk/src/types";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
@@ -1397,7 +1398,6 @@ export class SpaceStoreClass extends AsyncStoreWithClient<IState> {
|
||||
export default class SpaceStore {
|
||||
private static readonly internalInstance = (() => {
|
||||
const instance = new SpaceStoreClass();
|
||||
instance.start();
|
||||
return instance;
|
||||
})();
|
||||
|
||||
@@ -1408,9 +1408,9 @@ export default class SpaceStore {
|
||||
/**
|
||||
* @internal for test only
|
||||
*/
|
||||
public static testInstance(): SpaceStoreClass {
|
||||
public static testInstance(client: MatrixClient): SpaceStoreClass {
|
||||
const store = new SpaceStoreClass();
|
||||
store.start();
|
||||
store.useUnitTestClient(client);
|
||||
return store;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,6 @@ export class WidgetLayoutStore extends ReadyWatchingStore {
|
||||
public static get instance(): WidgetLayoutStore {
|
||||
if (!this.internalInstance) {
|
||||
this.internalInstance = new WidgetLayoutStore();
|
||||
this.internalInstance.start();
|
||||
}
|
||||
return this.internalInstance;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ export enum WidgetMessagingStoreEvent {
|
||||
export class WidgetMessagingStore extends AsyncStoreWithClient<{}> {
|
||||
private static readonly internalInstance = (() => {
|
||||
const instance = new WidgetMessagingStore();
|
||||
instance.start();
|
||||
return instance;
|
||||
})();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user