From 60d8ebb914f3c44fa0db5a98ca977c8b4a2aecd4 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Tue, 12 Dec 2017 16:05:18 +0000 Subject: [PATCH] Refactor MatrixActions to something much easier to grok. --- src/actions/MatrixActionCreators.js | 68 +++++++++++++++++++++-------- src/actions/actionCreators.js | 67 ---------------------------- src/stores/TagOrderStore.js | 2 +- 3 files changed, 52 insertions(+), 85 deletions(-) diff --git a/src/actions/MatrixActionCreators.js b/src/actions/MatrixActionCreators.js index 0f02e7730e..0b42938caf 100644 --- a/src/actions/MatrixActionCreators.js +++ b/src/actions/MatrixActionCreators.js @@ -14,31 +14,65 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { - createMatrixActionCreator, - createMatrixSyncActionCreator, -} from './actionCreators'; +import dis from '../dispatcher'; -// Events emitted from the matrixClient that we want to dispatch as actions -// via MatrixActionCreators. See createMatrixActionCreator. -const REGISTERED_EVENTS = [ - "accountData", -]; +// TODO: migrate from sync_state to MatrixActions.sync so that more js-sdk events +// become dispatches in the same place. +/** + * An action creator that will map a `sync` event to a MatrixActions.sync action, + * each parameter mapping to a key-value in the action. + * + * @param {MatrixClient} matrixClient the matrix client + * @param {string} state the current sync state. + * @param {string} prevState the previous sync state. + * @returns {Object} an action of type MatrixActions.sync. + */ +function createSyncAction(matrixClient, state, prevState) { + return { + action: 'MatrixActions.sync', + state, + prevState, + matrixClient, + }; +} + +/** + * An action creator that will map an account data matrix event to a + * MatrixActions.accountData action. + * + * @param {MatrixClient} matrixClient the matrix client with which to + * register a listener. + * @param {MatrixEvent} accountDataEvent the account data event. + * @returns {Object} an action of type MatrixActions.accountData. + */ +function createAccountDataAction(matrixClient, accountDataEvent) { + return { + action: 'MatrixActions.accountData', + event: accountDataEvent, + event_type: accountDataEvent.getType(), + event_content: accountDataEvent.getContent(), + }; +} export default { - actionCreators: [], - actionCreatorsStop: [], + _matrixClientListenersStop: [], start(matrixClient) { - this.actionCreators = REGISTERED_EVENTS.map((eventId) => - createMatrixActionCreator(matrixClient, eventId), - ); - this.actionCreators.push(createMatrixSyncActionCreator(matrixClient)); + this._addMatrixClientListener(matrixClient, 'sync', createSyncAction); + this._addMatrixClientListener(matrixClient, 'accountData', createAccountDataAction); + }, - this.actionCreatorsStop = this.actionCreators.map((ac) => ac()); + _addMatrixClientListener(matrixClient, eventName, actionCreator) { + const listener = (...args) => { + dis.dispatch(actionCreator(matrixClient, ...args)); + }; + matrixClient.on(eventName, listener); + this._matrixClientListenersStop.push(() => { + matrixClient.removeListener(eventName, listener); + }); }, stop() { - this.actionCreatorsStop.map((stop) => stop()); + this._matrixClientListenersStop.forEach((stopListener) => stopListener()); }, }; diff --git a/src/actions/actionCreators.js b/src/actions/actionCreators.js index 60ff368fc8..b92e8ed950 100644 --- a/src/actions/actionCreators.js +++ b/src/actions/actionCreators.js @@ -36,70 +36,3 @@ export function createPromiseActionCreator(id, fn) { }); }; } - -/** - * Create an action creator that will listen to events of type eventId emitted - * by matrixClient and dispatch a corresponding action of the following shape: - * { - * action: 'MatrixActions.' + eventId, - * event: matrixEvent, - * event_type: matrixEvent.getType(), - * event_content: matrixEvent.getContent(), - * } - * @param {MatrixClient} matrixClient the matrix client with which to register - * a listener. - * @param {string} eventId the ID of the event that when emitted will cause the - * an action to be dispatched. - * @returns {function} a function that, when called, will begin to listen to - * dispatches from matrixClient. The result from that - * function can be called to stop listening. - */ -export function createMatrixActionCreator(matrixClient, eventId) { - const listener = (matrixEvent) => { - dis.dispatch({ - action: 'MatrixActions.' + eventId, - event: matrixEvent, - event_type: matrixEvent.getType(), - event_content: matrixEvent.getContent(), - }); - }; - return () => { - matrixClient.on(eventId, listener); - return () => { - matrixClient.removeListener(eventId, listener); - }; - }; -} - -// TODO: migrate from sync_state to MatrixSync so that more js-sdk events -// become dispatches in the same place. -/** - * Create an action creator that will listen to `sync` events emitted - * by matrixClient and dispatch a corresponding MatrixSync action. E.g: - * { - * action: 'MatrixSync', - * state: 'SYNCING', - * prevState: 'PREPARED' - * } - * @param {MatrixClient} matrixClient the matrix client with which to register - * a listener. - * @returns {function} a function that, when called, will begin to listen to - * dispatches from matrixClient. The result from that - * function can be called to stop listening. - */ -export function createMatrixSyncActionCreator(matrixClient) { - const listener = (state, prevState) => { - dis.dispatch({ - action: 'MatrixSync', - state, - prevState, - matrixClient, - }); - }; - return () => { - matrixClient.on('sync', listener); - return () => { - matrixClient.removeListener('sync', listener); - }; - }; -} diff --git a/src/stores/TagOrderStore.js b/src/stores/TagOrderStore.js index 819ad5abfe..2fdfbe5381 100644 --- a/src/stores/TagOrderStore.js +++ b/src/stores/TagOrderStore.js @@ -42,7 +42,7 @@ class TagOrderStore extends Store { __onDispatch(payload) { switch (payload.action) { // Initialise state after initial sync - case 'MatrixSync': { + case 'MatrixActions.sync': { if (!(payload.prevState === 'PREPARED' && payload.state === 'SYNCING')) { break; }