Created
May 1, 2018 08:13
-
-
Save konstantin24121/a52bae0f3a2041c2edc12971528a144a to your computer and use it in GitHub Desktop.
Middleware for syncing actions between tabs
This file contains 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
/** | |
* Middleware for syncing actions between tabs | |
*/ | |
export default () => (next) => (action) => { | |
if (action.sync) { | |
delete action.sync; | |
localStorage.setItem(`${__PROJECT_NAME__}.storeSync`, JSON.stringify({ | |
action, | |
_hash: Date.now(), | |
})); | |
} | |
return next(action); | |
}; | |
const syncListener = (store) => { | |
const handleListenStorage = (event) => { | |
if (event.key === `${__PROJECT_NAME__}.storeSync`) { | |
const storageValue = JSON.parse(event.newValue); | |
store.dispatch(storageValue.action); | |
} | |
}; | |
window.addEventListener('storage', handleListenStorage); | |
}; | |
export { syncListener }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment