Skip to content

Commit cc239ed

Browse files
committed
Merge pull request reduxjs#528 from rackt/remove-invariant-warning
Don't depend on invariant and warning
2 parents 7d2ef9e + d99b361 commit cc239ed

File tree

5 files changed

+48
-59
lines changed

5 files changed

+48
-59
lines changed

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@
6666
"webpack": "^1.9.6",
6767
"webpack-dev-server": "^1.8.2"
6868
},
69-
"dependencies": {
70-
"invariant": "^2.0.0",
71-
"warning": "^2.0.0"
72-
},
7369
"npmName": "redux",
7470
"npmFileMap": [
7571
{

src/createStore.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import invariant from 'invariant';
21
import isPlainObject from './utils/isPlainObject';
32

43
/**
@@ -32,10 +31,9 @@ export var ActionTypes = {
3231
* and subscribe to changes.
3332
*/
3433
export default function createStore(reducer, initialState) {
35-
invariant(
36-
typeof reducer === 'function',
37-
'Expected the reducer to be a function.'
38-
);
34+
if (typeof reducer !== 'function') {
35+
throw new Error('Expected the reducer to be a function.');
36+
}
3937

4038
var currentReducer = reducer;
4139
var currentState = initialState;
@@ -92,15 +90,13 @@ export default function createStore(reducer, initialState) {
9290
* return something else (for example, a Promise you can await).
9391
*/
9492
function dispatch(action) {
95-
invariant(
96-
isPlainObject(action),
97-
'Actions must be plain objects. Use custom middleware for async actions.'
98-
);
93+
if (!isPlainObject(action)) {
94+
throw new Error('Actions must be plain objects. Use custom middleware for async actions.');
95+
}
9996

100-
invariant(
101-
isDispatching === false,
102-
'Reducers may not dispatch actions.'
103-
);
97+
if (isDispatching) {
98+
throw new Error('Reducers may not dispatch actions.');
99+
}
104100

105101
try {
106102
isDispatching = true;

src/utils/bindActionCreators.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import invariant from 'invariant';
21
import mapValues from '../utils/mapValues';
32

43
function bindActionCreator(actionCreator, dispatch) {
@@ -31,12 +30,12 @@ export default function bindActionCreators(actionCreators, dispatch) {
3130
return bindActionCreator(actionCreators, dispatch);
3231
}
3332

34-
invariant(
35-
typeof actionCreators === 'object' && actionCreators != null,
36-
'bindActionCreators expected an object or a function, instead received %s. ' +
37-
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?',
38-
typeof actionCreators
39-
);
33+
if (typeof actionCreators !== 'object' || actionCreators == null) {
34+
throw new Error(
35+
`bindActionCreators expected an object or a function, instead received ${typeof actionCreators}. ` +
36+
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
37+
);
38+
}
4039

4140
return mapValues(actionCreators, actionCreator =>
4241
bindActionCreator(actionCreator, dispatch)

src/utils/combineReducers.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { ActionTypes } from '../createStore';
22
import isPlainObject from '../utils/isPlainObject';
33
import mapValues from '../utils/mapValues';
44
import pick from '../utils/pick';
5-
import invariant from 'invariant';
6-
import warning from 'warning';
75

86
function getErrorMessage(key, action) {
97
var actionType = action && action.type;
@@ -19,17 +17,15 @@ function verifyStateShape(initialState, currentState) {
1917
var reducerKeys = Object.keys(currentState);
2018

2119
if (reducerKeys.length === 0) {
22-
warning(
23-
false,
20+
console.error(
2421
'Store does not have a valid reducer. Make sure the argument passed ' +
2522
'to combineReducers is an object whose values are reducers.'
2623
);
2724
return;
2825
}
2926

3027
if (!isPlainObject(initialState)) {
31-
warning(
32-
false,
28+
console.error(
3329
'initialState has unexpected type of "' +
3430
({}).toString.call(initialState).match(/\s([a-z|A-Z]+)/)[1] +
3531
'". Expected initialState to be an object with the following ' +
@@ -42,12 +38,13 @@ function verifyStateShape(initialState, currentState) {
4238
key => reducerKeys.indexOf(key) < 0
4339
);
4440

45-
warning(
46-
unexpectedKeys.length === 0,
47-
`Unexpected ${unexpectedKeys.length > 1 ? 'keys' : 'key'} ` +
48-
`"${unexpectedKeys.join('", "')}" in initialState will be ignored. ` +
49-
`Expected to find one of the known reducer keys instead: "${reducerKeys.join('", "')}"`
50-
);
41+
if (unexpectedKeys.length > 0) {
42+
console.error(
43+
`Unexpected ${unexpectedKeys.length > 1 ? 'keys' : 'key'} ` +
44+
`"${unexpectedKeys.join('", "')}" in initialState will be ignored. ` +
45+
`Expected to find one of the known reducer keys instead: "${reducerKeys.join('", "')}"`
46+
);
47+
}
5148
}
5249

5350
/**
@@ -72,24 +69,26 @@ export default function combineReducers(reducers) {
7269

7370
Object.keys(finalReducers).forEach(key => {
7471
var reducer = finalReducers[key];
75-
invariant(
76-
typeof reducer(undefined, { type: ActionTypes.INIT }) !== 'undefined',
77-
`Reducer "${key}" returned undefined during initialization. ` +
78-
`If the state passed to the reducer is undefined, you must ` +
79-
`explicitly return the initial state. The initial state may ` +
80-
`not be undefined.`
81-
);
72+
if (typeof reducer(undefined, { type: ActionTypes.INIT }) === 'undefined') {
73+
throw new Error(
74+
`Reducer "${key}" returned undefined during initialization. ` +
75+
`If the state passed to the reducer is undefined, you must ` +
76+
`explicitly return the initial state. The initial state may ` +
77+
`not be undefined.`
78+
);
79+
}
8280

8381
var type = Math.random().toString(36).substring(7).split('').join('.');
84-
invariant(
85-
typeof reducer(undefined, { type }) !== 'undefined',
86-
`Reducer "${key}" returned undefined when probed with a random type. ` +
87-
`Don't try to handle ${ActionTypes.INIT} or other actions in "redux/*" ` +
88-
`namespace. They are considered private. Instead, you must return the ` +
89-
`current state for any unknown actions, unless it is undefined, ` +
90-
`in which case you must return the initial state, regardless of the ` +
91-
`action type. The initial state may not be undefined.`
92-
);
82+
if (typeof reducer(undefined, { type }) === 'undefined') {
83+
throw new Error(
84+
`Reducer "${key}" returned undefined when probed with a random type. ` +
85+
`Don't try to handle ${ActionTypes.INIT} or other actions in "redux/*" ` +
86+
`namespace. They are considered private. Instead, you must return the ` +
87+
`current state for any unknown actions, unless it is undefined, ` +
88+
`in which case you must return the initial state, regardless of the ` +
89+
`action type. The initial state may not be undefined.`
90+
);
91+
}
9392
});
9493

9594
var defaultState = mapValues(finalReducers, () => undefined);
@@ -98,10 +97,9 @@ export default function combineReducers(reducers) {
9897
return function combination(state = defaultState, action) {
9998
var finalState = mapValues(finalReducers, (reducer, key) => {
10099
var newState = reducer(state[key], action);
101-
invariant(
102-
typeof newState !== 'undefined',
103-
getErrorMessage(key, action)
104-
);
100+
if (typeof newState === 'undefined') {
101+
throw new Error(getErrorMessage(key, action));
102+
}
105103
return newState;
106104
});
107105

test/utils/bindActionCreators.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('bindActionCreators', () => {
3838
]);
3939
});
4040

41-
it('should throw an invariant violation for an undefined actionCreator', () => {
41+
it('should throw for an undefined actionCreator', () => {
4242
expect(() => {
4343
bindActionCreators(undefined, store.dispatch);
4444
}).toThrow(
@@ -47,7 +47,7 @@ describe('bindActionCreators', () => {
4747
);
4848
});
4949

50-
it('should throw an invariant violation for a null actionCreator', () => {
50+
it('should throw for a null actionCreator', () => {
5151
expect(() => {
5252
bindActionCreators(null, store.dispatch);
5353
}).toThrow(
@@ -56,7 +56,7 @@ describe('bindActionCreators', () => {
5656
);
5757
});
5858

59-
it('should throw an invariant violation for a primitive actionCreator', () => {
59+
it('should throw for a primitive actionCreator', () => {
6060
expect(() => {
6161
bindActionCreators('string', store.dispatch);
6262
}).toThrow(

0 commit comments

Comments
 (0)