/** * Polls the storeState for completion, and if found, dispatches MERGE_STORE. * Once that completes, test to see if we're ready, in which case we clear * the timer to stop polling * * @param {Object} storeToRehydrate the redux store to be rehydrated * (defaults to store variable in this module) */ function _rehydrationIntervalCallback(storeToRehydrate = store) { if (!areSelectorsReady(storeToRehydrate.getState())) { storeToRehydrate.dispatch({type: "MERGE_STORE", data: rehydrateFromLocalStorage()}); if (areSelectorsReady(storeToRehydrate.getState())) { clearInterval(_rehydrationIntervalTimer); store = null; // allow the reference to be GCed } } }
module.exports = function createActivityStreamStore(options) { const {incoming, outgoing, logger, rehydrate, middleware} = options || {}; // Add a channel if incoming and outgoing events were specified let channel; if (incoming && outgoing) { channel = new Channel({incoming, outgoing}); } const mw = [thunk]; if (channel) { mw.push(channel.middleware); } if (middleware) { mw.push(middleware); } // Logger should be last in the middleware array if (logger) { mw.push(loggerMiddleware); } let initialStore = rehydrate ? rehydrateFromLocalStorage() : {}; store = createStore( _mergeStateReducer(combineReducers(reducers)), initialStore, applyMiddleware(...mw) ); // we only want to rehydrate stores that are rehydratable, i.e. the content // stores. // if (rehydrate && !areSelectorsReady(store.getState())) { _startRehydrationPolling(); } if (channel) { channel.connectStore(store); } return store; };