Created
October 7, 2016 20:33
-
-
Save jacobp100/a04680bb579ab6ba807a12443d733191 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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