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.

61 lines
1.9 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.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2020-06-08 17:11:58 -06:00
*/
2025-02-05 13:25:06 +00:00
import { type 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";
2025-02-05 13:25:06 +00:00
import { type ActionPayload } from "../dispatcher/payloads";
import { ReadyWatchingStore } from "./ReadyWatchingStore";
2025-02-05 13:25:06 +00:00
import { type MatrixDispatcher } from "../dispatcher/dispatcher";
2020-06-08 17:11:58 -06:00
2024-10-16 16:43:07 +01: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 ?? null;
}
protected async onReady(): Promise<any> {
return asyncStore.onReady();
}
protected async onNotReady(): Promise<any> {
return asyncStore.onNotReady();
}
})(dispatcher);
}
2024-10-10 15:08:43 +01:00
public async start(): Promise<void> {
await this.readyStore.start();
}
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);
}
}