Skip to content

Instantly share code, notes, and snippets.

@jacobp100
Created October 7, 2016 20:33
Show Gist options
  • Save jacobp100/a04680bb579ab6ba807a12443d733191 to your computer and use it in GitHub Desktop.
Save jacobp100/a04680bb579ab6ba807a12443d733191 to your computer and use it in GitHub Desktop.
const subscribeMiddleware = () => store => {
let handlers = [];
store.subscribe = (cb) => {
handlers = handlers.concat(cb);
const removeSubscriber = () => { handlers = handlers.filter(fn => fn !== cb) };
return removeSubscriber;
};
return next => (action) => {
const previousState = store.getState();
next(action);
const nextState = store.getState();
if (previousState !== nextState) handlers.forEach(cb => cb());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment