Skip to content

Instantly share code, notes, and snippets.

@veggiemonk
Forked from peteruithoven/app.js
Created December 17, 2015 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veggiemonk/ce7893f435c0f02d5c76 to your computer and use it in GitHub Desktop.
Save veggiemonk/ce7893f435c0f02d5c76 to your computer and use it in GitHub Desktop.
Rehydrating Redux store when using systemjs hot reloader
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();
}
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