Skip to content

Commit

Permalink
Revert "throw if getState, subscribe, or unsubscribe called while dis…
Browse files Browse the repository at this point in the history
…patching" (reduxjs#1882)
  • Loading branch information
gaearon authored and seantcoyote committed Jan 14, 2018
1 parent 777b4c6 commit dcd0307
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 129 deletions.
24 changes: 0 additions & 24 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ export default function createStore(reducer, preloadedState, enhancer) {
* @returns {any} The current state tree of your application.
*/
function getState() {
if (isDispatching) {
throw new Error(
'You may not call store.getState() while the reducer is executing. ' +
'The reducer has already received the state as an argument. ' +
'Pass it down from the top reducer instead of reading it from the store.'
)
}

return currentState
}

Expand Down Expand Up @@ -111,15 +103,6 @@ export default function createStore(reducer, preloadedState, enhancer) {
throw new Error('Expected listener to be a function.')
}

if (isDispatching) {
throw new Error(
'You may not call store.subscribe() while the reducer is executing. ' +
'If you would like to be notified after the store has been updated, subscribe from a ' +
'component and invoke store.getState() in the callback to access the latest state. ' +
'See http://redux.js.org/docs/api/Store.html#subscribe for more details.'
)
}

var isSubscribed = true

ensureCanMutateNextListeners()
Expand All @@ -130,13 +113,6 @@ export default function createStore(reducer, preloadedState, enhancer) {
return
}

if (isDispatching) {
throw new Error(
'You may not unsubscribe from a store listener while the reducer is executing. ' +
'See http://redux.js.org/docs/api/Store.html#subscribe for more details.'
)
}

isSubscribed = false

ensureCanMutateNextListeners()
Expand Down
35 changes: 1 addition & 34 deletions test/createStore.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import expect from 'expect'
import { createStore, combineReducers } from '../src/index'
import {
addTodo,
dispatchInMiddle,
getStateInMiddle,
subscribeInMiddle,
unsubscribeInMiddle,
throwError,
unknownAction
} from './helpers/actionCreators'
import { addTodo, dispatchInMiddle, throwError, unknownAction } from './helpers/actionCreators'
import * as reducers from './helpers/reducers'
import * as Rx from 'rxjs'
import $$observable from 'symbol-observable'
Expand Down Expand Up @@ -461,31 +453,6 @@ 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(/You may not call store.getState()/)
})

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

expect(() =>
store.dispatch(subscribeInMiddle(store.subscribe.bind(store, () => {})))
).toThrow(/You may not call store.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(/You may not unsubscribe from a store/)
})

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

export function addTodo(text) {
return { type: ADD_TODO, text }
Expand Down Expand Up @@ -34,27 +26,6 @@ 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: 0 additions & 3 deletions test/helpers/actionTypes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
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: 1 addition & 38 deletions test/helpers/reducers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
ADD_TODO,
DISPATCH_IN_MIDDLE,
GET_STATE_IN_MIDDLE,
SUBSCRIBE_IN_MIDDLE,
UNSUBSCRIBE_IN_MIDDLE,
THROW_ERROR
} from './actionTypes'
import { ADD_TODO, DISPATCH_IN_MIDDLE, THROW_ERROR } from './actionTypes'


function id(state = []) {
Expand Down Expand Up @@ -53,36 +46,6 @@ 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

0 comments on commit dcd0307

Please sign in to comment.