forked from mirrorjs/mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefaults.js
58 lines (45 loc) · 1.51 KB
/
defaults.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { effects, addEffect } from './effects'
export const options = {
// global initial state
// initialState: undefined,
// Should be one of ['browser', 'hash', 'memory']
// Learn more: https://github.com/ReactTraining/history/blob/master/README.md
historyMode: 'browser',
// A list of the standard Redux middleware
middlewares: [],
// `options.reducers` will be directly handled by `combineReducers`,
// so reducers defined here must be standard Redux reducer:
//
// reducers: {
// add: (state, action) => {}
// }
//
reducers: {},
// An overwrite of the existing effect handler
addEffect: addEffect(effects),
}
const historyModes = ['browser', 'hash', 'memory']
export default function defaults(opts = {}) {
const {
historyMode,
middlewares,
addEffect
} = opts
if (historyMode && !historyModes.includes(historyMode)) {
throw new Error(`historyMode "${historyMode}" is invalid, must be one of ${historyModes.join(', ')}!`)
}
if (middlewares && !Array.isArray(middlewares)) {
throw new Error(`middlewares "${middlewares}" is invalid, must be an Array!`)
}
if (addEffect) {
if (typeof addEffect !== 'function' || typeof addEffect({}) !== 'function') {
throw new Error(`addEffect "${addEffect}" is invalid, must be a function that returns a function`)
} else {
// create effects handler with initial effects object
opts.addEffect = opts.addEffect(effects)
}
}
Object.keys(opts).forEach(key => {
options[key] = opts[key]
})
}