2017-05-24 15:56:13 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-07-24 15:50:34 +00:00
|
|
|
Copyright 2018 New Vector Ltd
|
2019-12-20 00:45:24 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2017-05-24 15:56:13 +00: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.
|
|
|
|
*/
|
|
|
|
import dis from '../dispatcher';
|
|
|
|
import {Store} from 'flux/utils';
|
|
|
|
|
2017-05-26 16:23:02 +00:00
|
|
|
const INITIAL_STATE = {
|
|
|
|
deferred_action: null,
|
|
|
|
};
|
|
|
|
|
2017-05-24 15:56:13 +00:00
|
|
|
/**
|
2019-01-21 22:11:10 +00:00
|
|
|
* A class for storing application state to do with authentication. This is a simple flux
|
|
|
|
* store that listens for actions and updates its state accordingly, informing any
|
2017-05-24 15:56:13 +00:00
|
|
|
* listeners (views) of state changes.
|
|
|
|
*/
|
|
|
|
class LifecycleStore extends Store {
|
|
|
|
constructor() {
|
|
|
|
super(dis);
|
|
|
|
|
|
|
|
// Initialise state
|
2017-05-26 16:23:02 +00:00
|
|
|
this._state = INITIAL_STATE;
|
2017-05-24 15:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_setState(newState) {
|
|
|
|
this._state = Object.assign(this._state, newState);
|
|
|
|
this.__emitChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
__onDispatch(payload) {
|
|
|
|
switch (payload.action) {
|
|
|
|
case 'do_after_sync_prepared':
|
|
|
|
this._setState({
|
|
|
|
deferred_action: payload.deferred_action,
|
|
|
|
});
|
|
|
|
break;
|
2017-06-05 17:37:38 +00:00
|
|
|
case 'cancel_after_sync_prepared':
|
|
|
|
this._setState({
|
|
|
|
deferred_action: null,
|
|
|
|
});
|
|
|
|
break;
|
2017-07-01 13:35:40 +00:00
|
|
|
case 'sync_state': {
|
2017-05-24 15:56:13 +00:00
|
|
|
if (payload.state !== 'PREPARED') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!this._state.deferred_action) break;
|
|
|
|
const deferredAction = Object.assign({}, this._state.deferred_action);
|
|
|
|
this._setState({
|
|
|
|
deferred_action: null,
|
|
|
|
});
|
|
|
|
dis.dispatch(deferredAction);
|
|
|
|
break;
|
2017-07-01 13:35:40 +00:00
|
|
|
}
|
2019-07-03 22:46:37 +00:00
|
|
|
case 'on_client_not_viable':
|
2017-05-25 16:16:16 +00:00
|
|
|
case 'on_logged_out':
|
2017-05-26 16:23:02 +00:00
|
|
|
this.reset();
|
2017-05-25 16:16:16 +00:00
|
|
|
break;
|
2017-05-24 15:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-26 16:23:02 +00:00
|
|
|
|
|
|
|
reset() {
|
|
|
|
this._state = Object.assign({}, INITIAL_STATE);
|
|
|
|
}
|
2017-05-24 15:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let singletonLifecycleStore = null;
|
|
|
|
if (!singletonLifecycleStore) {
|
|
|
|
singletonLifecycleStore = new LifecycleStore();
|
|
|
|
}
|
2019-12-20 00:45:24 +00:00
|
|
|
export default singletonLifecycleStore;
|