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";
|
|
|
|
import { AsyncStore } from "./AsyncStore";
|
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
2020-07-30 01:52:20 +03:00
|
|
|
import { Dispatcher } from "flux";
|
2021-01-19 03:41:17 +03:00
|
|
|
import { ReadyWatchingStore } from "./ReadyWatchingStore";
|
2020-06-09 02:11:58 +03:00
|
|
|
|
|
|
|
export abstract class AsyncStoreWithClient<T extends Object> extends AsyncStore<T> {
|
2021-01-19 03:41:17 +03:00
|
|
|
private readyStore: ReadyWatchingStore;
|
2020-06-09 02:11:58 +03:00
|
|
|
|
2020-07-30 01:52:20 +03:00
|
|
|
protected constructor(dispatcher: Dispatcher<ActionPayload>, initialState: T = <T>{}) {
|
|
|
|
super(dispatcher, initialState);
|
|
|
|
|
2021-01-19 03:41:17 +03:00
|
|
|
// Create an anonymous class to avoid code duplication
|
|
|
|
const asyncStore = this;
|
|
|
|
this.readyStore = new (class extends ReadyWatchingStore {
|
|
|
|
public get mxClient(): MatrixClient {
|
|
|
|
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
|
|
|
|
2021-01-19 03:41:17 +03:00
|
|
|
protected get matrixClient(): MatrixClient {
|
|
|
|
return this.readyStore.mxClient;
|
2020-07-30 01:52:20 +03:00
|
|
|
}
|
|
|
|
|
2020-06-09 02:11:58 +03:00
|
|
|
protected async onReady() {
|
|
|
|
// Default implementation is to do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async onNotReady() {
|
|
|
|
// Default implementation is to do nothing.
|
|
|
|
}
|
|
|
|
|
2021-01-19 03:41:17 +03:00
|
|
|
protected abstract async onAction(payload: ActionPayload);
|
|
|
|
|
2020-06-09 02:11:58 +03:00
|
|
|
protected async onDispatch(payload: ActionPayload) {
|
|
|
|
await this.onAction(payload);
|
|
|
|
}
|
|
|
|
}
|