add a potential initialState to the store (for testing)

main
Yanick Champoux 2022-04-10 17:41:50 -04:00
parent 24b07c3557
commit 012db36e65
1 changed files with 22 additions and 25 deletions

View File

@ -8,36 +8,33 @@ import { initial } from "lodash";
let composeEnhancers = compose; let composeEnhancers = compose;
if (dev && browser && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) { if (dev && browser && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__; composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
} }
export default () => { export default (initialState = undefined) => {
if (browser) {
const i = localStorage.getItem("ship");
let initialState = undefined; if (i) initialState = JSON.parse(localStorage.getItem("ship"));
}
if( browser ) { const duxStore = shipDux.createStore(initialState, (mw) =>
const i =localStorage.getItem('ship'); composeEnhancers(applyMiddleware(mw))
);
if(i) initialState = JSON.parse(localStorage.getItem('ship')); let previous;
} const state = readable(duxStore.getState(), (set) => {
duxStore.subscribe(() => {
const duxStore = shipDux.createStore(initialState, (mw) => if (previous === duxStore.getState()) return;
composeEnhancers(applyMiddleware(mw)) previous = duxStore.getState();
); set(previous);
if (browser) localStorage.setItem("ship", JSON.stringify(previous));
let previous;
const state = readable(duxStore.getState(), (set) => {
duxStore.subscribe(() => {
if (previous === duxStore.getState()) return;
previous = duxStore.getState();
set(previous);
if( browser ) localStorage.setItem('ship', JSON.stringify(previous));
});
}); });
});
return { return {
dispatch: duxStore.dispatch, dispatch: duxStore.dispatch,
state, state,
shipMass: derived(state, (state) => state.reqs.mass), shipMass: derived(state, (state) => state.reqs.mass),
}; };
}; };