Skip to content

Instantly share code, notes, and snippets.

@konstantin24121
Created May 1, 2018 08:13
Show Gist options
  • Save konstantin24121/a52bae0f3a2041c2edc12971528a144a to your computer and use it in GitHub Desktop.
Save konstantin24121/a52bae0f3a2041c2edc12971528a144a to your computer and use it in GitHub Desktop.
Middleware for syncing actions between tabs
/**
* 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