Created
May 24, 2020 08:33
-
-
Save afflicted-cat/17bce990e6635e581e7cdc1c4fe6a89e to your computer and use it in GitHub Desktop.
effector-next & hmr
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
function attachHMR(domain: Domain) { | |
if (typeof window === "undefined") return; | |
const states = window.__EFFECTOR_STATES__ = {}; | |
domain.onCreateStore((store) => { | |
if (!store.sid) return; | |
if (store.sid in states) { | |
const { defaultState, actual } = states[store.sid]; | |
if (store.defaultState === defaultState) { | |
store.setState(actual); | |
} else { | |
states[store.sid] = { | |
defaultState: store.defaultState, | |
actual: store.getState(), | |
}; | |
} | |
} else { | |
states[store.sid] = { | |
defaultState: store.defaultState, | |
actual: store.getState(), | |
}; | |
} | |
store.updates.watch((value) => { | |
if (store.sid) { | |
states[store.sid].actual = value; | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment