Convert LifecycleStore to Typescript

pull/21833/head
Michael Telatynski 2021-06-07 10:57:39 +01:00
parent 3431ebb995
commit b2d9dd7214
1 changed files with 27 additions and 23 deletions

View File

@ -1,7 +1,5 @@
/* /*
Copyright 2017 Vector Creations Ltd Copyright 2017-2021 The Matrix.org Foundation C.I.C.
Copyright 2018 New Vector Ltd
Copyright 2019 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -15,11 +13,18 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import dis from '../dispatcher/dispatcher';
import { Store } from 'flux/utils'; import { Store } from 'flux/utils';
import dis from '../dispatcher/dispatcher';
import { ActionPayload } from "../dispatcher/payloads";
interface IState {
deferredAction: any;
}
const INITIAL_STATE = { const INITIAL_STATE = {
deferred_action: null, deferredAction: null,
}; };
/** /**
@ -27,39 +32,38 @@ const INITIAL_STATE = {
* store that listens for actions and updates its state accordingly, informing any * store that listens for actions and updates its state accordingly, informing any
* listeners (views) of state changes. * listeners (views) of state changes.
*/ */
class LifecycleStore extends Store { class LifecycleStore extends Store<ActionPayload> {
private state: IState = INITIAL_STATE;
constructor() { constructor() {
super(dis); super(dis);
// Initialise state
this._state = INITIAL_STATE;
} }
_setState(newState) { private setState(newState: Partial<IState>) {
this._state = Object.assign(this._state, newState); this.state = Object.assign(this.state, newState);
this.__emitChange(); this.__emitChange();
} }
__onDispatch(payload) { protected __onDispatch(payload: ActionPayload) {
switch (payload.action) { switch (payload.action) {
case 'do_after_sync_prepared': case 'do_after_sync_prepared':
this._setState({ this.setState({
deferred_action: payload.deferred_action, deferredAction: payload.deferred_action,
}); });
break; break;
case 'cancel_after_sync_prepared': case 'cancel_after_sync_prepared':
this._setState({ this.setState({
deferred_action: null, deferredAction: null,
}); });
break; break;
case 'sync_state': { case 'syncstate': {
if (payload.state !== 'PREPARED') { if (payload.state !== 'PREPARED') {
break; break;
} }
if (!this._state.deferred_action) break; if (!this.state.deferredAction) break;
const deferredAction = Object.assign({}, this._state.deferred_action); const deferredAction = Object.assign({}, this.state.deferredAction);
this._setState({ this.setState({
deferred_action: null, deferredAction: null,
}); });
dis.dispatch(deferredAction); dis.dispatch(deferredAction);
break; break;
@ -71,8 +75,8 @@ class LifecycleStore extends Store {
} }
} }
reset() { private reset() {
this._state = Object.assign({}, INITIAL_STATE); this.state = Object.assign({}, INITIAL_STATE);
} }
} }