From 0968148b227aebc4393ea838b19769c192dbcbd8 Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Thu, 18 Apr 2019 20:18:55 +0200 Subject: [PATCH] steal the store from redux --- src/store/reducer.js | 10 +++++++++- src/store/store.js | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/store/reducer.js b/src/store/reducer.js index d3fbb326..63e0fe8a 100644 --- a/src/store/reducer.js +++ b/src/store/reducer.js @@ -1,15 +1,22 @@ import { combineReducers } from 'redux' +export const ATTACH_REDUCER = '@KEA/ATTACH_REDUCER' + // worker functions are loaded globally, reducers locally in store let defaultReducerRoot = null // all reducers that are created let reducerTree = {} let rootReducers = {} -let syncedWithStore = {} +let syncedWithStore = {} // TODO: remove? +let store const defaultState = {} +export function attachStore (storeReference) { + store = storeReference +} + export function clearReducerCache () { defaultReducerRoot = null reducerTree = {} @@ -78,6 +85,7 @@ export function addReducer (path, reducer) { } regenerateRootReducer(pathStart) + store && store.dispatch({ type: ATTACH_REDUCER, payload: { path, reducer } }) } export function regenerateRootReducer (pathStart) { diff --git a/src/store/store.js b/src/store/store.js index 38c99bf5..b10167c1 100644 --- a/src/store/store.js +++ b/src/store/store.js @@ -1,6 +1,6 @@ import { createStore, applyMiddleware, combineReducers, compose } from 'redux' -import { keaReducer } from './reducer' +import { keaReducer, attachStore } from './reducer' // import { globalPlugins, activatePlugin } from '../plugins' const reduxDevToolsCompose = typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ @@ -51,6 +51,11 @@ export function getStore (opts = {}) { // create store const store = finalCreateStore(combinedReducers, Object.assign({}, options.preloadedState)) + // give kea direct access to this store + // we need this to dispatch hydration actions when new kea logic stores are + // injected together with react components + attachStore(store) + // // run post-hooks // globalPlugins.afterReduxStore.forEach(f => f(options, store))