Refactor MatrixActions to something much easier to grok.

This commit is contained in:
Luke Barnard 2017-12-12 16:05:18 +00:00
parent 3e532e3722
commit 60d8ebb914
3 changed files with 52 additions and 85 deletions

View file

@ -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);
};
};
}