| 18 | import { createEffectsMiddleware } from './effects'; |
| 19 | |
| 20 | export function createStore(model, options = {}) { |
| 21 | const modelClone = clone(model); |
| 22 | const { |
| 23 | compose, |
| 24 | devTools = process.env.NODE_ENV !== 'production', |
| 25 | disableImmer = false, |
| 26 | enhancers = [], |
| 27 | initialState = undefined, |
| 28 | injections = {}, |
| 29 | middleware = [], |
| 30 | mockActions = false, |
| 31 | name: storeName = `EasyPeasyStore`, |
| 32 | version = 0, |
| 33 | reducerEnhancer = (rootReducer) => rootReducer, |
| 34 | } = options; |
| 35 | |
| 36 | const bindReplaceState = (modelDef) => ({ |
| 37 | ...modelDef, |
| 38 | ePRS: helpers.action((_, payload) => payload), |
| 39 | }); |
| 40 | |
| 41 | const _r = {}; |
| 42 | |
| 43 | let modeldef = bindReplaceState(modelClone); |
| 44 | let mockedActions = []; |
| 45 | |
| 46 | const persistKey = (targetPath) => |
| 47 | `[${storeName}][${version}]${ |
| 48 | targetPath.length > 0 ? `[${targetPath.join('.')}]` : '' |
| 49 | }`; |
| 50 | const persistor = createPersistor(persistKey, _r); |
| 51 | const persistMiddleware = createPersistMiddleware(persistor, _r); |
| 52 | |
| 53 | const replaceState = (nextState) => _r._i._aCD['@action.ePRS'](nextState); |
| 54 | |
| 55 | const bindStoreInternals = (state = {}) => { |
| 56 | const data = extractDataFromModel(modeldef, state, injections, _r); |
| 57 | _r._i = { |
| 58 | ...data, |
| 59 | reducer: reducerEnhancer( |
| 60 | createReducer(disableImmer, data._aRD, data._cR, data._cP), |
| 61 | ), |
| 62 | }; |
| 63 | }; |
| 64 | |
| 65 | const mockActionsMiddleware = () => () => (action) => { |
| 66 | if (action != null) { |
| 67 | mockedActions.push(action); |
| 68 | } |
| 69 | return undefined; |
| 70 | }; |
| 71 | |
| 72 | const devToolConfig = devTools === true ? {} : devTools; |
| 73 | |
| 74 | const composeEnhancers = |
| 75 | compose || |
| 76 | (devTools && |
| 77 | typeof window !== 'undefined' && |