Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept: fully create store before dispatching init action. #1576

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
59 changes: 40 additions & 19 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,7 @@ export var ActionTypes = {
* @returns {Store} A Redux store that lets you read the state, dispatch actions
* and subscribe to changes.
*/
export default function createStore(reducer, initialState, enhancer) {
if (typeof initialState === 'function' && typeof enhancer === 'undefined') {
enhancer = initialState
initialState = undefined
}

if (typeof enhancer !== 'undefined') {
if (typeof enhancer !== 'function') {
throw new Error('Expected the enhancer to be a function.')
}

return enhancer(createStore)(reducer, initialState)
}

export function createStore(reducer, initialState) {
if (typeof reducer !== 'function') {
throw new Error('Expected the reducer to be a function.')
}
Expand Down Expand Up @@ -198,15 +185,49 @@ export default function createStore(reducer, initialState, enhancer) {
dispatch({ type: ActionTypes.INIT })
}

// When a store is created, an "INIT" action is dispatched so that every
// reducer returns their initial state. This effectively populates
// the initial state tree.
dispatch({ type: ActionTypes.INIT })

return {
dispatch,
subscribe,
getState,
replaceReducer
}
}

/**
*
* TODO: Docs.
*
* @param {Function} reducer @see createStore
*
* @param {any} [initialState] @see createStore
*
* @param {Function} enhancer The store enhancer. You may optionally specify it
* to enhance the store with third-party capabilities such as middleware,
* time travel, persistence, etc. The only store enhancer that ships with Redux
* is `applyMiddleware()`.
*
* @returns {Store} An initialized Redux store that lets you read the state,
* dispatch actions and subscribe to changes.
*/
export default function initializeStore(reducer, initialState, enhancer) {
if (typeof initialState === 'function' && typeof enhancer === 'undefined') {
enhancer = initialState
initialState = undefined
}

if (typeof enhancer !== 'undefined') {
if (typeof enhancer !== 'function') {
throw new Error('Expected the enhancer to be a function.')
}
}

var finalCreateStore = enhancer ? enhancer(createStore) : createStore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enhancer can potentially dispatch an action which will be called before ActionTypes.INIT therefore this does not ensure that first action has all the middlewares applied.

var store = finalCreateStore(reducer, initialState)

// When a store is created, an "INIT" action is dispatched so that every
// reducer returns their initial state. This effectively populates
// the initial state tree.
store.dispatch({ type: ActionTypes.INIT })

return store
}
16 changes: 16 additions & 0 deletions test/createStore.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import expect from 'expect'
import { createStore, combineReducers } from '../src/index'
import { ActionTypes } from '../src/createStore'
import { addTodo, dispatchInMiddle, throwError, unknownAction } from './helpers/actionCreators'
import * as reducers from './helpers/reducers'

Expand Down Expand Up @@ -610,4 +611,19 @@ describe('createStore', () => {
store.subscribe(undefined)
).toThrow()
})

it('fully initializes store before dispatching init action', () => {
const spyEnhancer = vanillaCreateStore => (...args) => {
const vanillaStore = vanillaCreateStore(...args)
return {
...vanillaStore,
dispatch: expect.createSpy(vanillaStore.dispatch).andCallThrough()
}
}

const store = createStore(reducers.todos, spyEnhancer)

expect(store.dispatch).toHaveBeenCalledWith({ type: ActionTypes.INIT })
})

})