element-web/src/stores/LifecycleStore.ts

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

78 lines
2.6 KiB
TypeScript
Raw Normal View History

/*
2021-06-07 12:57:39 +03:00
Copyright 2017-2021 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.
*/
2021-06-07 12:57:39 +03:00
import { Action } from "../dispatcher/actions";
import dis from "../dispatcher/dispatcher";
2021-06-07 12:57:39 +03:00
import { ActionPayload } from "../dispatcher/payloads";
import { DoAfterSyncPreparedPayload } from "../dispatcher/payloads/DoAfterSyncPreparedPayload";
2023-03-08 17:19:05 +03:00
import { AsyncStore } from "./AsyncStore";
2021-06-07 12:57:39 +03:00
interface IState {
deferredAction: ActionPayload | null;
2021-06-07 12:57:39 +03:00
}
const INITIAL_STATE: IState = {
2021-06-07 12:57:39 +03:00
deferredAction: null,
2017-05-26 19:23:02 +03:00
};
/**
2023-03-08 17:19:05 +03:00
* A class for storing application state to do with authentication. This is a simple
2019-01-22 01:11:10 +03:00
* store that listens for actions and updates its state accordingly, informing any
* listeners (views) of state changes.
*/
2023-03-08 17:19:05 +03:00
class LifecycleStore extends AsyncStore<IState> {
public constructor() {
2023-03-08 17:19:05 +03:00
super(dis, INITIAL_STATE);
}
2023-03-08 17:19:05 +03:00
protected onDispatch(payload: ActionPayload | DoAfterSyncPreparedPayload<ActionPayload>): void {
switch (payload.action) {
case Action.DoAfterSyncPrepared:
2023-03-08 17:19:05 +03:00
this.updateState({
2021-06-07 12:57:39 +03:00
deferredAction: payload.deferred_action,
});
break;
case "cancel_after_sync_prepared":
2023-03-08 17:19:05 +03:00
this.updateState({
2021-06-07 12:57:39 +03:00
deferredAction: null,
});
break;
case "MatrixActions.sync": {
if (payload.state !== "PREPARED") {
break;
}
2021-06-07 12:57:39 +03:00
if (!this.state.deferredAction) break;
const deferredAction = Object.assign({}, this.state.deferredAction);
2023-03-08 17:19:05 +03:00
this.updateState({
2021-06-07 12:57:39 +03:00
deferredAction: null,
});
dis.dispatch(deferredAction);
break;
}
case "on_client_not_viable":
case Action.OnLoggedOut:
2017-05-26 19:23:02 +03:00
this.reset();
break;
}
}
}
let singletonLifecycleStore: LifecycleStore | null = null;
if (!singletonLifecycleStore) {
singletonLifecycleStore = new LifecycleStore();
}
export default singletonLifecycleStore!;