Last active
December 21, 2015 16:05
-
-
Save peteruithoven/59f188bfab035ce96948 to your computer and use it in GitHub Desktop.
Rehydrating Redux store when using systemjs hot reloader
This file contains hidden or 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
import {createStore} from 'redux'; | |
import reducer from './reducers/index.js' | |
import { rehydrate, rehydratingStore } from './utils/rehydratingStore.js'; | |
const store = rehydratingStore()(createStore)(reducer); | |
export function __reload(deletedModule){ | |
const prevState = deletedModule.getState(); | |
debug('Reloaded. rehydrate with state: ', prevState.sketcher.objectsById); | |
store.dispatch(rehydrate(prevState)); | |
} | |
export function getState() { | |
return store.getState(); | |
} |
This file contains hidden or 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
const REHYDRATE = 'REHYDRATE'; | |
export function rehydrate(state) { | |
return { type: REHYDRATE, state }; | |
}; | |
export function rehydratingStore (config = {}) { | |
return (next) => (reducer, initialState) => { | |
const rehydrationReducer = createRehydrationReducer(reducer); | |
const store = next(rehydrationReducer, initialState); | |
const dispatch = store.dispatch; | |
return { | |
...store, | |
dispatch | |
} | |
} | |
function createRehydrationReducer (reducer) { | |
return (state, action) => { | |
if (action.type === REHYDRATE) { | |
let state = action.state; | |
state = reducer(state, action) | |
return state | |
} else { | |
return reducer(state, action); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hrmm .. I'm having trouble with this. My store seems to be reseting state on every action.