Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ function createStoreBase(reducer, initialState, onChange) {
}

function dispatch(action) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
}
if (typeof action.type === 'undefined') {
throw new Error(
'Actions may not have an undefined "type" property. ' +
'Have you misspelled a constant?'
)
}
if (isDispatching) {
throw new Error('Reducers may not dispatch actions.')
}
Expand All @@ -47,6 +35,37 @@ function createStoreBase(reducer, initialState, onChange) {
}
}

function coreEnhancer(nextCreateStoreBase) {
return (reducer, initialState, onChange) => {
const storeBase = nextCreateStoreBase(reducer, initialState, onChange)

function dispatch(action) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
}
if (typeof action.type === 'undefined') {
throw new Error(
'Actions may not have an undefined "type" property. ' +
'Have you misspelled a constant?'
)
}
return storeBase.dispatch(action)
}

function getState() {
return storeBase.getState()
}

return {
dispatch,
getState
}
}
}

export default function createStore(reducer, initialState, enhancer) {
if (typeof reducer !== 'function') {
throw new Error('Expected the reducer to be a function.')
Expand All @@ -60,7 +79,7 @@ export default function createStore(reducer, initialState, enhancer) {
}

enhancer = enhancer || (x => x)
var createFinalStoreBase = enhancer(createStoreBase)
var createFinalStoreBase = enhancer(coreEnhancer(createStoreBase))

var storeBase
var currentListeners = []
Expand Down