2017-05-24 18:56:13 +03:00
|
|
|
/*
|
2021-06-07 12:57:39 +03:00
|
|
|
Copyright 2017-2021 The Matrix.org Foundation C.I.C.
|
2017-05-24 18:56:13 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-02-22 13:04:27 +03:00
|
|
|
import { Action } from "../dispatcher/actions";
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from "../dispatcher/dispatcher";
|
2021-06-07 12:57:39 +03:00
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
2022-02-22 13:04:27 +03:00
|
|
|
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 {
|
2023-02-15 16:36:22 +03:00
|
|
|
deferredAction: ActionPayload | null;
|
2021-06-07 12:57:39 +03:00
|
|
|
}
|
2017-05-24 18:56:13 +03:00
|
|
|
|
2023-02-13 14:39:16 +03:00
|
|
|
const INITIAL_STATE: IState = {
|
2021-06-07 12:57:39 +03:00
|
|
|
deferredAction: null,
|
2017-05-26 19:23:02 +03:00
|
|
|
};
|
|
|
|
|
2017-05-24 18:56:13 +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
|
2017-05-24 18:56:13 +03:00
|
|
|
* listeners (views) of state changes.
|
|
|
|
*/
|
2023-03-08 17:19:05 +03:00
|
|
|
class LifecycleStore extends AsyncStore<IState> {
|
2022-12-16 15:29:59 +03:00
|
|
|
public constructor() {
|
2023-03-08 17:19:05 +03:00
|
|
|
super(dis, INITIAL_STATE);
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
|
2023-03-08 17:19:05 +03:00
|
|
|
protected onDispatch(payload: ActionPayload | DoAfterSyncPreparedPayload<ActionPayload>): void {
|
2017-05-24 18:56:13 +03:00
|
|
|
switch (payload.action) {
|
2022-02-22 13:04:27 +03:00
|
|
|
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,
|
2017-05-24 18:56:13 +03:00
|
|
|
});
|
|
|
|
break;
|
2017-06-05 20:37:38 +03:00
|
|
|
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,
|
2017-06-05 20:37:38 +03:00
|
|
|
});
|
|
|
|
break;
|
2022-02-09 12:21:56 +03:00
|
|
|
case "MatrixActions.sync": {
|
2017-05-24 18:56:13 +03:00
|
|
|
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,
|
2017-05-24 18:56:13 +03:00
|
|
|
});
|
|
|
|
dis.dispatch(deferredAction);
|
|
|
|
break;
|
2017-07-01 16:35:40 +03:00
|
|
|
}
|
2019-07-04 01:46:37 +03:00
|
|
|
case "on_client_not_viable":
|
2022-05-26 11:56:53 +03:00
|
|
|
case Action.OnLoggedOut:
|
2017-05-26 19:23:02 +03:00
|
|
|
this.reset();
|
2017-05-25 19:16:16 +03:00
|
|
|
break;
|
2017-05-24 18:56:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:36:22 +03:00
|
|
|
let singletonLifecycleStore: LifecycleStore | null = null;
|
2017-05-24 18:56:13 +03:00
|
|
|
if (!singletonLifecycleStore) {
|
|
|
|
singletonLifecycleStore = new LifecycleStore();
|
|
|
|
}
|
2023-02-15 16:36:22 +03:00
|
|
|
export default singletonLifecycleStore!;
|