-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Next
- Loading branch information
Showing
7 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ lib/ | |
node_modules/ | ||
*.swp | ||
package-lock.json | ||
.idea/ |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { createDynamicMiddlewares } from 'redux-dynamic-middlewares' | ||
import { combineReducers } from 'redux'; | ||
|
||
import { configureStore } from './configure-store' | ||
import { reloadState } from './reload-state' | ||
import { createAttach } from './create-attach' | ||
import { createDetach } from './create-detach' | ||
|
||
const emptyMiddleware = () => next => action => next(action) | ||
const emptyReducer = (state = {}) => state | ||
|
||
const createInstanceFlat = ({ | ||
name = 'redux-dynamic', | ||
key = '__EMPTY__', | ||
initial = {}, | ||
thunk = {}, | ||
reducer = emptyReducer, | ||
middleware = emptyMiddleware, | ||
withDevTools, | ||
} = {}) => { | ||
const dynamicMiddlewares = createDynamicMiddlewares() | ||
const reducerWrapper = reducer.wrapper ? reducer.wrapper : combineReducers | ||
const reducerInitial = reducer.wrapper ? reducer.wrapper(reducer.reducers) : emptyReducer | ||
const reducers = reducer.reducers ? { ...reducer.reducers } : { [key]: (state = initial, action) => reducer(state, action) } | ||
|
||
dynamicMiddlewares.addMiddleware(middleware) | ||
|
||
const registry = { | ||
keys: { | ||
[key]: true, | ||
}, | ||
reducers, | ||
reducerWrapper, | ||
thunks: { | ||
[key]: thunk, | ||
}, | ||
middlewares: { | ||
[key]: middleware, | ||
}, | ||
} | ||
|
||
const store = configureStore({ | ||
name, | ||
withDevTools, | ||
key, | ||
initial, | ||
reducer: reducerInitial, | ||
dynamicMiddlewares: dynamicMiddlewares.enhancer, | ||
}) | ||
|
||
reloadState(registry, store, dynamicMiddlewares) | ||
|
||
const getStore = () => store | ||
const attach = createAttach(registry, store, dynamicMiddlewares) | ||
const detach = createDetach(registry, store, dynamicMiddlewares) | ||
|
||
return { | ||
getStore, | ||
attach, | ||
detach, | ||
} | ||
} | ||
|
||
export { | ||
createInstanceFlat, | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* eslint-disable no-console */ | ||
import { createInstanceFlat } from './index' | ||
|
||
const reducer = (state = {}, action) => { | ||
if (action.type === 'foo') return { ...state, foo: 'bar' } | ||
return state | ||
} | ||
|
||
const complexReducer = { | ||
reducers: { | ||
one: reducer, | ||
}, | ||
} | ||
|
||
test('flat - without params', () => { | ||
console.error = jest.fn() | ||
|
||
const dynamicStore = createInstanceFlat() | ||
|
||
const store = dynamicStore.getStore() | ||
expect(store.getState()).toEqual({ __EMPTY__: {} }) | ||
|
||
store.dispatch({ type: 'foo' }) | ||
expect(console.error).not.toBeCalled() | ||
}) | ||
|
||
test('flat - one module with simple reducer', () => { | ||
console.error = jest.fn() | ||
|
||
const dynamicStore = createInstanceFlat({ key: 'key', reducer }) | ||
|
||
const store = dynamicStore.getStore() | ||
expect(store.getState()).toEqual({ key: {} }) | ||
|
||
store.dispatch({ type: 'foo' }) | ||
expect(console.error).not.toBeCalled() | ||
expect(store.getState()).toEqual({ key: { foo: 'bar' } }) | ||
}) | ||
|
||
test('flat - one module with complex reducer', () => { | ||
console.error = jest.fn() | ||
|
||
const dynamicStore = createInstanceFlat({ key: 'key', reducer: complexReducer }) | ||
|
||
const store = dynamicStore.getStore() | ||
expect(store.getState()).toEqual({ one: {} }) | ||
|
||
store.dispatch({ type: 'foo' }) | ||
expect(console.error).not.toBeCalled() | ||
expect(store.getState()).toEqual({ one: { foo: 'bar' } }) | ||
}) | ||
|
||
test('flat - attach and detach module', () => { | ||
console.error = jest.fn() | ||
|
||
const dynamicStore = createInstanceFlat({ key: 'key', reducer: complexReducer }) | ||
dynamicStore.attach({ key: 'two', reducer }) | ||
|
||
const store = dynamicStore.getStore() | ||
expect(store.getState()).toEqual({ one: {}, two: {} }) | ||
|
||
store.dispatch({ type: 'foo' }) | ||
expect(console.error).not.toBeCalled() | ||
expect(store.getState()).toEqual({ one: { foo: 'bar' }, two: { foo: 'bar' } }) | ||
|
||
dynamicStore.detach({ key: 'two' }) | ||
expect(store.getState()).toEqual({ one: { foo: 'bar' } }) | ||
}) | ||
|
||
test('flat - initials', () => { | ||
const initial = { one: 'bar' } | ||
const initial2 = { foo: 'bar' } | ||
|
||
const dynamicStore = createInstanceFlat({ key: 'key', initial, reducer: complexReducer }) | ||
dynamicStore.attach({ key: 'two', initial: initial2 }) | ||
|
||
const store = dynamicStore.getStore() | ||
expect(store.getState()).toEqual({ one: 'bar', two: { foo: 'bar' } }) | ||
|
||
dynamicStore.detach({ key: 'two' }) | ||
expect(store.getState()).toEqual({ one: 'bar' }) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { createInstance } from './create-instance' | ||
import { createInstanceFlat } from './create-instance-flat' | ||
import { prepareActions } from './prepare-actions' | ||
|
||
export { | ||
createInstance, | ||
createInstanceFlat, | ||
prepareActions, | ||
} |
This file contains 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