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

throw if getState, subscribe, or unsubscribe called while dispatching #1569

Merged
merged 5 commits into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 13 additions & 1 deletion src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export default function createStore(reducer, initialState, enhancer) {
* @returns {any} The current state tree of your application.
*/
function getState() {
if (isDispatching) {
throw new Error('Reducers may not access store state.')
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe change it to something a little more helpful? I’m up for more descriptive error messages. e.g. You may not call store.getState() while the reducer is executing. The reducer has already received the state as an argument, so pass it down from the top reducer instead of reading it from the store..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I was just going with similar format to dispatch message. I like the more descriptive messages though. How about for subscribe and unsubscribe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated in f957b9f

}

return currentState
}

Expand Down Expand Up @@ -102,16 +106,24 @@ export default function createStore(reducer, initialState, enhancer) {
throw new Error('Expected listener to be a function.')
}

if (isDispatching) {
throw new Error('Reducers may not subscribe.')
}

var isSubscribed = true

ensureCanMutateNextListeners()
nextListeners.push(listener)

return function unsubscribe() {
return function unsubscribe() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: empty spaces

if (!isSubscribed) {
return
}

if (isDispatching) {
throw new Error('Reducers may not unsubscribe.')
}

isSubscribed = false

ensureCanMutateNextListeners()
Expand Down
35 changes: 34 additions & 1 deletion test/createStore.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import expect from 'expect'
import { createStore, combineReducers } from '../src/index'
import { addTodo, dispatchInMiddle, throwError, unknownAction } from './helpers/actionCreators'
import {
addTodo,
dispatchInMiddle,
getStateInMiddle,
subscribeInMiddle,
unsubscribeInMiddle,
throwError,
unknownAction
} from './helpers/actionCreators'
import * as reducers from './helpers/reducers'

describe('createStore', () => {
Expand Down Expand Up @@ -451,6 +459,31 @@ describe('createStore', () => {
).toThrow(/may not dispatch/)
})

it('does not allow getState() from within a reducer', () => {
const store = createStore(reducers.getStateInTheMiddleOfReducer)

expect(() =>
store.dispatch(getStateInMiddle(store.getState.bind(store)))
).toThrow(/may not access store state/)
})

it('does not allow subscribe() from within a reducer', () => {
const store = createStore(reducers.subscribeInTheMiddleOfReducer)

expect(() =>
store.dispatch(subscribeInMiddle(store.subscribe.bind(store, () => {})))
).toThrow(/may not subscribe/)
})

it('does not allow unsubscribe from subscribe() from within a reducer', () => {
const store = createStore(reducers.unsubscribeInTheMiddleOfReducer)
const unsubscribe = store.subscribe(() => {})

expect(() =>
store.dispatch(unsubscribeInMiddle(unsubscribe.bind(store)))
).toThrow(/may not unsubscribe/)
})

it('recovers from an error within a reducer', () => {
const store = createStore(reducers.errorThrowingReducer)
expect(() =>
Expand Down
31 changes: 30 additions & 1 deletion test/helpers/actionCreators.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { ADD_TODO, DISPATCH_IN_MIDDLE, THROW_ERROR, UNKNOWN_ACTION } from './actionTypes'
import {
ADD_TODO,
DISPATCH_IN_MIDDLE,
GET_STATE_IN_MIDDLE,
SUBSCRIBE_IN_MIDDLE,
UNSUBSCRIBE_IN_MIDDLE,
THROW_ERROR,
UNKNOWN_ACTION
} from './actionTypes'

export function addTodo(text) {
return { type: ADD_TODO, text }
Expand Down Expand Up @@ -26,6 +34,27 @@ export function dispatchInMiddle(boundDispatchFn) {
}
}

export function getStateInMiddle(boundGetStateFn) {
return {
type: GET_STATE_IN_MIDDLE,
boundGetStateFn
}
}

export function subscribeInMiddle(boundSubscribeFn) {
return {
type: SUBSCRIBE_IN_MIDDLE,
boundSubscribeFn
}
}

export function unsubscribeInMiddle(boundUnsubscribeFn) {
return {
type: UNSUBSCRIBE_IN_MIDDLE,
boundUnsubscribeFn
}
}

export function throwError() {
return {
type: THROW_ERROR
Expand Down
3 changes: 3 additions & 0 deletions test/helpers/actionTypes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const ADD_TODO = 'ADD_TODO'
export const DISPATCH_IN_MIDDLE = 'DISPATCH_IN_MIDDLE'
export const GET_STATE_IN_MIDDLE = 'GET_STATE_IN_MIDDLE'
export const SUBSCRIBE_IN_MIDDLE = 'SUBSCRIBE_IN_MIDDLE'
export const UNSUBSCRIBE_IN_MIDDLE = 'UNSUBSCRIBE_IN_MIDDLE'
export const THROW_ERROR = 'THROW_ERROR'
export const UNKNOWN_ACTION = 'UNKNOWN_ACTION'
39 changes: 38 additions & 1 deletion test/helpers/reducers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ADD_TODO, DISPATCH_IN_MIDDLE, THROW_ERROR } from './actionTypes'
import {
ADD_TODO,
DISPATCH_IN_MIDDLE,
GET_STATE_IN_MIDDLE,
SUBSCRIBE_IN_MIDDLE,
UNSUBSCRIBE_IN_MIDDLE,
THROW_ERROR
} from './actionTypes'


function id(state = []) {
Expand Down Expand Up @@ -46,6 +53,36 @@ export function dispatchInTheMiddleOfReducer(state = [], action) {
}
}

export function getStateInTheMiddleOfReducer(state = [], action) {
switch (action.type) {
case GET_STATE_IN_MIDDLE:
action.boundGetStateFn()
return state
default:
return state
}
}

export function subscribeInTheMiddleOfReducer(state = [], action) {
switch (action.type) {
case SUBSCRIBE_IN_MIDDLE:
action.boundSubscribeFn()
return state
default:
return state
}
}

export function unsubscribeInTheMiddleOfReducer(state = [], action) {
switch (action.type) {
case UNSUBSCRIBE_IN_MIDDLE:
action.boundUnsubscribeFn()
return state
default:
return state
}
}

export function errorThrowingReducer(state = [], action) {
switch (action.type) {
case THROW_ERROR:
Expand Down