2017-06-21 06:40:28 +00:00
|
|
|
import { ADD_MESSAGES, ADD_FETCHED_MESSAGES } from 'state/actions';
|
2017-06-06 22:04:37 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// This middleware handles waiting until MessageBox
|
|
|
|
// is ready before adding messages to the top
|
|
|
|
//
|
|
|
|
const message = store => next => {
|
|
|
|
const ready = {};
|
|
|
|
const cache = {};
|
|
|
|
|
|
|
|
return action => {
|
2017-06-21 06:40:28 +00:00
|
|
|
if (action.type === ADD_MESSAGES && action.prepend) {
|
2020-06-15 08:58:51 +00:00
|
|
|
const key = `${action.network} ${action.channel}`;
|
2017-06-06 22:04:37 +00:00
|
|
|
|
|
|
|
if (ready[key]) {
|
|
|
|
ready[key] = false;
|
|
|
|
return next(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
cache[key] = action;
|
2017-06-21 06:40:28 +00:00
|
|
|
} else if (action.type === ADD_FETCHED_MESSAGES) {
|
2020-06-15 08:58:51 +00:00
|
|
|
const key = `${action.network} ${action.channel}`;
|
2017-06-06 22:04:37 +00:00
|
|
|
ready[key] = true;
|
|
|
|
|
|
|
|
if (cache[key]) {
|
|
|
|
store.dispatch(cache[key]);
|
|
|
|
cache[key] = undefined;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return next(action);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default message;
|