Skip to content

Commit 42f0e4e

Browse files
authored
Revert "throw if getState, subscribe, or unsubscribe called while dispatching" (reduxjs#1882)
1 parent 1e46110 commit 42f0e4e

File tree

5 files changed

+3
-129
lines changed

5 files changed

+3
-129
lines changed

src/createStore.js

-24
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@ export default function createStore(reducer, preloadedState, enhancer) {
7272
* @returns {any} The current state tree of your application.
7373
*/
7474
function getState() {
75-
if (isDispatching) {
76-
throw new Error(
77-
'You may not call store.getState() while the reducer is executing. ' +
78-
'The reducer has already received the state as an argument. ' +
79-
'Pass it down from the top reducer instead of reading it from the store.'
80-
)
81-
}
82-
8375
return currentState
8476
}
8577

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

114-
if (isDispatching) {
115-
throw new Error(
116-
'You may not call store.subscribe() while the reducer is executing. ' +
117-
'If you would like to be notified after the store has been updated, subscribe from a ' +
118-
'component and invoke store.getState() in the callback to access the latest state. ' +
119-
'See http://redux.js.org/docs/api/Store.html#subscribe for more details.'
120-
)
121-
}
122-
123106
var isSubscribed = true
124107

125108
ensureCanMutateNextListeners()
@@ -130,13 +113,6 @@ export default function createStore(reducer, preloadedState, enhancer) {
130113
return
131114
}
132115

133-
if (isDispatching) {
134-
throw new Error(
135-
'You may not unsubscribe from a store listener while the reducer is executing. ' +
136-
'See http://redux.js.org/docs/api/Store.html#subscribe for more details.'
137-
)
138-
}
139-
140116
isSubscribed = false
141117

142118
ensureCanMutateNextListeners()

test/createStore.spec.js

+1-34
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import expect from 'expect'
22
import { createStore, combineReducers } from '../src/index'
3-
import {
4-
addTodo,
5-
dispatchInMiddle,
6-
getStateInMiddle,
7-
subscribeInMiddle,
8-
unsubscribeInMiddle,
9-
throwError,
10-
unknownAction
11-
} from './helpers/actionCreators'
3+
import { addTodo, dispatchInMiddle, throwError, unknownAction } from './helpers/actionCreators'
124
import * as reducers from './helpers/reducers'
135
import * as Rx from 'rxjs'
146
import $$observable from 'symbol-observable'
@@ -461,31 +453,6 @@ describe('createStore', () => {
461453
).toThrow(/may not dispatch/)
462454
})
463455

464-
it('does not allow getState() from within a reducer', () => {
465-
const store = createStore(reducers.getStateInTheMiddleOfReducer)
466-
467-
expect(() =>
468-
store.dispatch(getStateInMiddle(store.getState.bind(store)))
469-
).toThrow(/You may not call store.getState()/)
470-
})
471-
472-
it('does not allow subscribe() from within a reducer', () => {
473-
const store = createStore(reducers.subscribeInTheMiddleOfReducer)
474-
475-
expect(() =>
476-
store.dispatch(subscribeInMiddle(store.subscribe.bind(store, () => {})))
477-
).toThrow(/You may not call store.subscribe()/)
478-
})
479-
480-
it('does not allow unsubscribe from subscribe() from within a reducer', () => {
481-
const store = createStore(reducers.unsubscribeInTheMiddleOfReducer)
482-
const unsubscribe = store.subscribe(() => {})
483-
484-
expect(() =>
485-
store.dispatch(unsubscribeInMiddle(unsubscribe.bind(store)))
486-
).toThrow(/You may not unsubscribe from a store/)
487-
})
488-
489456
it('recovers from an error within a reducer', () => {
490457
const store = createStore(reducers.errorThrowingReducer)
491458
expect(() =>

test/helpers/actionCreators.js

+1-30
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
ADD_TODO,
3-
DISPATCH_IN_MIDDLE,
4-
GET_STATE_IN_MIDDLE,
5-
SUBSCRIBE_IN_MIDDLE,
6-
UNSUBSCRIBE_IN_MIDDLE,
7-
THROW_ERROR,
8-
UNKNOWN_ACTION
9-
} from './actionTypes'
1+
import { ADD_TODO, DISPATCH_IN_MIDDLE, THROW_ERROR, UNKNOWN_ACTION } from './actionTypes'
102

113
export function addTodo(text) {
124
return { type: ADD_TODO, text }
@@ -34,27 +26,6 @@ export function dispatchInMiddle(boundDispatchFn) {
3426
}
3527
}
3628

37-
export function getStateInMiddle(boundGetStateFn) {
38-
return {
39-
type: GET_STATE_IN_MIDDLE,
40-
boundGetStateFn
41-
}
42-
}
43-
44-
export function subscribeInMiddle(boundSubscribeFn) {
45-
return {
46-
type: SUBSCRIBE_IN_MIDDLE,
47-
boundSubscribeFn
48-
}
49-
}
50-
51-
export function unsubscribeInMiddle(boundUnsubscribeFn) {
52-
return {
53-
type: UNSUBSCRIBE_IN_MIDDLE,
54-
boundUnsubscribeFn
55-
}
56-
}
57-
5829
export function throwError() {
5930
return {
6031
type: THROW_ERROR

test/helpers/actionTypes.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
export const ADD_TODO = 'ADD_TODO'
22
export const DISPATCH_IN_MIDDLE = 'DISPATCH_IN_MIDDLE'
3-
export const GET_STATE_IN_MIDDLE = 'GET_STATE_IN_MIDDLE'
4-
export const SUBSCRIBE_IN_MIDDLE = 'SUBSCRIBE_IN_MIDDLE'
5-
export const UNSUBSCRIBE_IN_MIDDLE = 'UNSUBSCRIBE_IN_MIDDLE'
63
export const THROW_ERROR = 'THROW_ERROR'
74
export const UNKNOWN_ACTION = 'UNKNOWN_ACTION'

test/helpers/reducers.js

+1-38
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
ADD_TODO,
3-
DISPATCH_IN_MIDDLE,
4-
GET_STATE_IN_MIDDLE,
5-
SUBSCRIBE_IN_MIDDLE,
6-
UNSUBSCRIBE_IN_MIDDLE,
7-
THROW_ERROR
8-
} from './actionTypes'
1+
import { ADD_TODO, DISPATCH_IN_MIDDLE, THROW_ERROR } from './actionTypes'
92

103

114
function id(state = []) {
@@ -53,36 +46,6 @@ export function dispatchInTheMiddleOfReducer(state = [], action) {
5346
}
5447
}
5548

56-
export function getStateInTheMiddleOfReducer(state = [], action) {
57-
switch (action.type) {
58-
case GET_STATE_IN_MIDDLE:
59-
action.boundGetStateFn()
60-
return state
61-
default:
62-
return state
63-
}
64-
}
65-
66-
export function subscribeInTheMiddleOfReducer(state = [], action) {
67-
switch (action.type) {
68-
case SUBSCRIBE_IN_MIDDLE:
69-
action.boundSubscribeFn()
70-
return state
71-
default:
72-
return state
73-
}
74-
}
75-
76-
export function unsubscribeInTheMiddleOfReducer(state = [], action) {
77-
switch (action.type) {
78-
case UNSUBSCRIBE_IN_MIDDLE:
79-
action.boundUnsubscribeFn()
80-
return state
81-
default:
82-
return state
83-
}
84-
}
85-
8649
export function errorThrowingReducer(state = [], action) {
8750
switch (action.type) {
8851
case THROW_ERROR:

0 commit comments

Comments
 (0)