Skip to content

Instantly share code, notes, and snippets.

@itrelease
Last active August 29, 2015 14:25
Show Gist options
  • Save itrelease/2eb5fd39a44b66591b08 to your computer and use it in GitHub Desktop.
Save itrelease/2eb5fd39a44b66591b08 to your computer and use it in GitHub Desktop.
Delayed redux action
export default function delay({ dispatch, getState }) {
const delayed = {};
return (next) => {
return (action) => {
const actionIndex = (delayed[action.type] || []).indexOf(action.delay);
if (delayed[action.type] && actionIndex > -1) {
const delayedActionCreator = delayed[action.type][actionIndex];
delayed[action.type].splice(actionIndex, 1);
delayedActionCreator(dispatch, getState, action);
return next(action);
}
if (action.delay) {
if (!delayed[action.until]) {
delayed[action.until] = [];
}
delayed[action.until].push(action.delay);
}
return next(action);
}
}
}
function switchLang(lang) {
return {
delay: function (dispatch, getState, action) {
dispatch({
type: 'SWITCH_LANG',
lang: lang
});
},
until: 'DATA_LOADED'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment