Skip to content

Commit 602fbf4

Browse files
Added module initial state.
1 parent 014a636 commit 602fbf4

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/bootstrap.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import configureStore from './configureStore'
44

55
export const BOOT = 'redux-boot/BOOT'
66

7-
export default function boot(initialState = {}, modules = []) {
8-
const {reducers, middlewares, enhancers} = processModules(modules)
7+
export default function boot(state = {}, modules = []) {
8+
const {reducers, middlewares, enhancers, initialState} = processModules(modules)
99

10-
let store = configureStore(initialState, reducers, middlewares, enhancers)
10+
state = {...initialState, ...state}
11+
12+
let store = configureStore(state, reducers, middlewares, enhancers)
1113

1214
return store
13-
.dispatch(bootAction(initialState))
15+
.dispatch(bootAction(state))
1416
.then(action => {
1517
return {
1618
action,

src/processModules.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ export default function processModules(modules) {
2323
))
2424
.map(module => module.enhancer)
2525

26+
const initialState = modules
27+
.map(module => module.initialState || (f => f))
28+
.reduce((state, initialState) => initialState(state), {})
29+
2630
return {
2731
reducers,
2832
middlewares,
29-
enhancers
33+
enhancers,
34+
initialState,
3035
}
3136
}
3237

test/bootstrap.test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ test('Boostrap new app with an initial state', assert => {
4545
})
4646

4747
test('Boostrap new app with initial state on modules', assert => {
48-
const testModule = { initialState: { foo: 'bar' } }
48+
const initialState = { foo: 'bar' }
49+
const testModule = { initialState: (state) => initialState }
4950

5051
const app = boot(null, [testModule])
5152

5253
app.then(({action, store}) => {
5354

5455
assert.deepEqual(
5556
store.getState(),
56-
testModule.initialState,
57+
initialState,
5758
'State is equal to module\'s initial state'
5859
)
5960

0 commit comments

Comments
 (0)