Last active
May 5, 2018 08:21
-
-
Save AllenFang/424aa4899edeb46a883375053c8269ec to your computer and use it in GitHub Desktop.
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
//////// Background Scripts Entry, index.js //////// | |
import { createBackgroundStore } from 'redux-webext'; | |
import { createSubscriber } from './universalStore'; | |
createBackgroundStore({ ... }).then((store) => { | |
// create subscriber immediately!! | |
store.subscribe(createSubscriber(store)); | |
// ... | |
}); | |
//////// universalStore.js //////// | |
import { createDomainListener } from './listener/domain'; | |
export const createSubscriber = (store) => { | |
const domainListener = createDomainListener(store); // listener for domain change | |
return () => { // when state change, this function will be called | |
const state = store.getState(); | |
domainListener(state.domain); | |
} | |
} | |
//////// domain.js //////// | |
export const createDomainListener = (store) => { | |
let domain = store.getState().domain; | |
return (nextDomain) => { | |
if (nextDomain !== domain) { // domain change | |
store.dispatch(someAction()); | |
// .... | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment