Skip to content

Commit db26f25

Browse files
Velenirgajus
authored andcommitted
feat: add Immutable v4 compatibility (#61)
* fix: pass type check for Record in Immutable v4 * fix: extracting keys from Record in Immutable v4 * refactor: change warning to better reflect Immutable v4 types * docs: better reflect both Immutable v3 and v4 types * docs: replace Immutable.Iterable with Immutable.Collection BREAKING CHANGE: * build: change dependency type for immutable and extend version supported
1 parent 50e3656 commit db26f25

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
`redux-immutable` is used to create an equivalent function of Redux [`combineReducers`](http://redux.js.org/docs/api/combineReducers.html) that works with [Immutable.js](https://facebook.github.io/immutable-js/) state.
88

9-
When Redux [`createStore`](https://github.com/reactjs/redux/blob/master/docs/api/createStore.md) `reducer` is created using `redux-immutable` then `initialState` must be an instance of [`Immutable.Iterable`](https://facebook.github.io/immutable-js/docs/#/Iterable).
9+
When Redux [`createStore`](https://github.com/reactjs/redux/blob/master/docs/api/createStore.md) `reducer` is created using `redux-immutable` then `initialState` must be an instance of [`Immutable.Collection`](https://facebook.github.io/immutable-js/docs/#/Collection).
1010

1111
## Problem
1212

13-
When [`createStore`](https://github.com/reactjs/redux/blob/v3.0.6/docs/api/createStore.md) is invoked with `initialState` that is an instance of `Immutable.Iterable` further invocation of reducer will [produce an error](https://github.com/reactjs/redux/blob/v3.0.6/src/combineReducers.js#L31-L38):
13+
When [`createStore`](https://github.com/reactjs/redux/blob/v3.0.6/docs/api/createStore.md) is invoked with `initialState` that is an instance of `Immutable.Collection` further invocation of reducer will [produce an error](https://github.com/reactjs/redux/blob/v3.0.6/src/combineReducers.js#L31-L38):
1414

1515
> The initialState argument passed to createStore has unexpected type of "Object".
1616
> Expected argument to be an object with the following keys: "data"
@@ -21,7 +21,7 @@ This is because Redux `combineReducers` [treats `state` object as a plain JavaSc
2121

2222
## Usage
2323

24-
Create a store with `initialState` set to an instance of [`Immutable.Iterable`](https://facebook.github.io/immutable-js/docs/#/Iterable):
24+
Create a store with `initialState` set to an instance of [`Immutable.Collection`](https://facebook.github.io/immutable-js/docs/#/Collection):
2525

2626
```js
2727
import {
@@ -58,7 +58,7 @@ const rootReducer = combineReducers({foo: fooReducer}, StateRecord);
5858
// state now must always have 'foo' property with its default value returned from fooReducer(undefined, action)
5959
```
6060

61-
In general, `getDefaultState` function must return an instance of `Immutable.Iterable` that implements `get`, `set` and `withMutations` methods. Such iterables are `List`, `Map`, `OrderedMap` and `Record`.
61+
In general, `getDefaultState` function must return an instance of `Immutable.Record` or `Immutable.Collection` that implements `get`, `set` and `withMutations` methods. Such collections are `List`, `Map` and `OrderedMap`.
6262

6363
### Using with `react-router-redux`
6464

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
"url": "http://gajus.com"
1818
},
1919
"license": "BSD-3-Clause",
20-
"dependencies": {
21-
"immutable": "^3.8.1"
22-
},
20+
"peerDependencies": {
21+
"immutable": "^3.8.1 || ^4.0.0-rc.1"
22+
},
2323
"devDependencies": {
2424
"babel-cli": "^6.18.0",
2525
"babel-plugin-add-module-exports": "^0.2.1",
@@ -35,6 +35,7 @@
3535
"eslint-config-canonical": "^6.0.0",
3636
"flow-runtime": "0.0.6",
3737
"husky": "^0.12.0",
38+
"immutable": "^3.8.1 || ^4.0.0-rc.1",
3839
"mocha": "^3.2.0",
3940
"semantic-release": "^6.3.2"
4041
},

src/utilities/getUnexpectedInvocationParameterMessage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export default (state: Object, reducers: Object, action: Object) => {
1010

1111
const stateName = getStateName(action);
1212

13-
if (!Immutable.Iterable.isIterable(state)) {
14-
return 'The ' + stateName + ' is of unexpected type. Expected argument to be an instance of Immutable.Iterable with the following properties: "' + reducerNames.join('", "') + '".';
13+
if (Immutable.isImmutable ? !Immutable.isImmutable(state) : !Immutable.Iterable.isIterable(state)) {
14+
return 'The ' + stateName + ' is of unexpected type. Expected argument to be an instance of Immutable.Collection or Immutable.Record with the following properties: "' + reducerNames.join('", "') + '".';
1515
}
1616

17-
const unexpectedStatePropertyNames = state.keySeq().toArray().filter((name) => {
17+
const unexpectedStatePropertyNames = state.toSeq().keySeq().toArray().filter((name) => {
1818
return !reducers.hasOwnProperty(name);
1919
});
2020

tests/utilities/getUnexpectedInvocationParameterMessage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ describe('utilities', () => {
2929
expect(expectedErrorMessage).to.equal('Store does not have a valid reducer. Make sure the argument passed to combineReducers is an object whose values are reducers.');
3030
});
3131
});
32-
context('state is not an instance of Immutable.Iterable', () => {
32+
context('state is not an instance of Immutable.Collection or Immutable.Record', () => {
3333
it('returns error', () => {
3434
const expectedErrorMessage = getUnexpectedInvocationParameterMessage({}, validReducers, validAction);
3535

36-
expect(expectedErrorMessage).to.equal('The initialState argument passed to createStore is of unexpected type. Expected argument to be an instance of Immutable.Iterable with the following properties: "foo".');
36+
expect(expectedErrorMessage).to.equal('The initialState argument passed to createStore is of unexpected type. Expected argument to be an instance of Immutable.Collection or Immutable.Record with the following properties: "foo".');
3737
});
3838
});
3939
context('state defines properties that are not present in the reducer map', () => {

0 commit comments

Comments
 (0)