2020-06-09 02:11:58 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
2021-10-23 01:23:32 +03:00
|
|
|
|
2020-06-09 02:11:58 +03:00
|
|
|
import { AsyncStore } from "./AsyncStore";
|
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
2021-01-19 03:41:17 +03:00
|
|
|
import { ReadyWatchingStore } from "./ReadyWatchingStore";
|
2023-03-08 17:19:05 +03:00
|
|
|
import { MatrixDispatcher } from "../dispatcher/dispatcher";
|
2020-06-09 02:11:58 +03:00
|
|
|
|
|
|
|
export abstract class AsyncStoreWithClient<T extends Object> extends AsyncStore<T> {
|
2021-01-19 06:53:15 +03:00
|
|
|
protected readyStore: ReadyWatchingStore;
|
2020-06-09 02:11:58 +03:00
|
|
|
|
2023-03-08 17:19:05 +03:00
|
|
|
protected constructor(dispatcher: MatrixDispatcher, initialState: T = <T>{}) {
|
2020-07-30 01:52:20 +03:00
|
|
|
super(dispatcher, initialState);
|
|
|
|
|
2021-01-19 03:41:17 +03:00
|
|
|
// Create an anonymous class to avoid code duplication
|
2021-01-19 06:53:15 +03:00
|
|
|
const asyncStore = this; // eslint-disable-line @typescript-eslint/no-this-alias
|
2021-01-19 03:41:17 +03:00
|
|
|
this.readyStore = new (class extends ReadyWatchingStore {
|
2023-04-06 13:10:14 +03:00
|
|
|
public get mxClient(): MatrixClient | null {
|
2021-01-19 03:41:17 +03:00
|
|
|
return this.matrixClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async onReady(): Promise<any> {
|
|
|
|
return asyncStore.onReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async onNotReady(): Promise<any> {
|
|
|
|
return asyncStore.onNotReady();
|
|
|
|
}
|
|
|
|
})(dispatcher);
|
|
|
|
}
|
2020-07-30 01:52:20 +03:00
|
|
|
|
2022-08-30 22:13:39 +03:00
|
|
|
public async start(): Promise<void> {
|
|
|
|
await this.readyStore.start();
|
|
|
|
}
|
|
|
|
|
2023-04-06 13:10:14 +03:00
|
|
|
public get matrixClient(): MatrixClient | null {
|
2021-01-19 03:41:17 +03:00
|
|
|
return this.readyStore.mxClient;
|
2020-07-30 01:52:20 +03:00
|
|
|
}
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
protected async onReady(): Promise<void> {
|
2020-06-09 02:11:58 +03:00
|
|
|
// Default implementation is to do nothing.
|
|
|
|
}
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
protected async onNotReady(): Promise<void> {
|
2020-06-09 02:11:58 +03:00
|
|
|
// Default implementation is to do nothing.
|
|
|
|
}
|
|
|
|
|
2021-01-20 16:40:46 +03:00
|
|
|
protected abstract onAction(payload: ActionPayload): Promise<void>;
|
2021-01-19 03:41:17 +03:00
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
protected async onDispatch(payload: ActionPayload): Promise<void> {
|
2020-06-09 02:11:58 +03:00
|
|
|
await this.onAction(payload);
|
|
|
|
}
|
|
|
|
}
|