From f5fde8d530e9c7a19fd8a50c4e102f02eb70011b Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 28 Jan 2016 03:31:24 +0000 Subject: [PATCH] Update to the proposal by @timdorr --- .../real-world/store/configureStore.dev.js | 8 ++++--- src/createStore.js | 21 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/examples/real-world/store/configureStore.dev.js b/examples/real-world/store/configureStore.dev.js index 45745b38da..2e9e027c64 100644 --- a/examples/real-world/store/configureStore.dev.js +++ b/examples/real-world/store/configureStore.dev.js @@ -1,4 +1,4 @@ -import { createStore, applyMiddleware } from 'redux' +import { createStore, applyMiddleware, compose } from 'redux' import { syncHistory } from 'react-router-redux' import { browserHistory } from 'react-router' import DevTools from '../containers/DevTools' @@ -13,8 +13,10 @@ export default function configureStore(initialState) { const store = createStore( rootReducer, initialState, - applyMiddleware(thunk, api, reduxRouterMiddleware, createLogger()), - DevTools.instrument() + compose( + applyMiddleware(thunk, api, reduxRouterMiddleware, createLogger()), + DevTools.instrument() + ) ) // Required for replaying actions from devtools to work diff --git a/src/createStore.js b/src/createStore.js index 97341c3e52..71a4030f50 100644 --- a/src/createStore.js +++ b/src/createStore.js @@ -1,5 +1,4 @@ import isPlainObject from './utils/isPlainObject' -import compose from './compose' /** * These are private action types reserved by Redux. @@ -28,16 +27,26 @@ export var ActionTypes = { * If you use `combineReducers` to produce the root reducer function, this must be * an object with the same shape as `combineReducers` keys. * + * @param {Function} enhancer The store enhancer. You may optionally specify it + * to enhance the store with third-party capabilities such as the middleware, + * time travel, persistence, etc. The only store enhancer that ships with Redux + * is `applyMiddleware()`. + * * @returns {Store} A Redux store that lets you read the state, dispatch actions * and subscribe to changes. */ -export default function createStore(reducer, initialState, ...enhancers) { - if (typeof initialState === 'function') { - enhancers.unshift(initialState) +export default function createStore(reducer, initialState, enhancer) { + if (typeof initialState === 'function' && typeof enhancer === 'undefined') { + enhancer = initialState initialState = undefined } - if (enhancers.length > 0) { - return compose(...enhancers)(createStore)(reducer, initialState) + + if (typeof enhancer !== 'undefined') { + if (typeof enhancer !== 'function') { + throw new Error('Expected the enhancer to be a function.') + } + + return enhancer(createStore)(reducer, initialState) } if (typeof reducer !== 'function') {