Skip to content

Instantly share code, notes, and snippets.

@afflicted-cat
Created May 24, 2020 08:33
Show Gist options
  • Save afflicted-cat/17bce990e6635e581e7cdc1c4fe6a89e to your computer and use it in GitHub Desktop.
Save afflicted-cat/17bce990e6635e581e7cdc1c4fe6a89e to your computer and use it in GitHub Desktop.
effector-next & hmr
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