Files
ThreadNet-Web/src/stores/AsyncStoreWithClient.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

66 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-06-08 17:11:58 -06:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-06-08 17:11:58 -06:00
Copyright 2020 The Matrix.org Foundation C.I.C.
2024-09-09 14:57:16 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
2020-06-08 17:11:58 -06:00
*/
import { MatrixClient } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
2020-06-08 17:11:58 -06:00
import { AsyncStore } from "./AsyncStore";
import { ActionPayload } from "../dispatcher/payloads";
import { ReadyWatchingStore } from "./ReadyWatchingStore";
2023-03-08 14:19:05 +00:00
import { MatrixDispatcher } from "../dispatcher/dispatcher";
2020-06-08 17:11:58 -06:00
export abstract class AsyncStoreWithClient<T extends Object> extends AsyncStore<T> {
2021-01-18 20:53:15 -07:00
protected readyStore: ReadyWatchingStore;
2020-06-08 17:11:58 -06:00
2023-03-08 14:19:05 +00:00
protected constructor(dispatcher: MatrixDispatcher, initialState: T = <T>{}) {
super(dispatcher, initialState);
// Create an anonymous class to avoid code duplication
2021-01-18 20:53:15 -07:00
const asyncStore = this; // eslint-disable-line @typescript-eslint/no-this-alias
this.readyStore = new (class extends ReadyWatchingStore {
public get mxClient(): MatrixClient | null {
return this.matrixClient;
}
protected async onReady(): Promise<any> {
return asyncStore.onReady();
}
protected async onNotReady(): Promise<any> {
return asyncStore.onNotReady();
}
})(dispatcher);
}
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 {
return this.readyStore.mxClient;
}
protected async onReady(): Promise<void> {
2020-06-08 17:11:58 -06:00
// Default implementation is to do nothing.
}
protected async onNotReady(): Promise<void> {
2020-06-08 17:11:58 -06:00
// Default implementation is to do nothing.
}
protected abstract onAction(payload: ActionPayload): Promise<void>;
protected async onDispatch(payload: ActionPayload): Promise<void> {
2020-06-08 17:11:58 -06:00
await this.onAction(payload);
}
}