Skip to content

Commit

Permalink
steal the store from redux
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Apr 18, 2019
1 parent 9f6ac1a commit 0968148
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/store/reducer.js
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand Down Expand Up @@ -78,6 +85,7 @@ export function addReducer (path, reducer) {
}

regenerateRootReducer(pathStart)
store && store.dispatch({ type: ATTACH_REDUCER, payload: { path, reducer } })
}

export function regenerateRootReducer (pathStart) {
Expand Down
7 changes: 6 additions & 1 deletion src/store/store.js
Original file line number Diff line number Diff line change
@@ -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__
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 0968148

Please sign in to comment.