coopcycle-js icon indicating copy to clipboard operation
coopcycle-js copied to clipboard

Use Immutable.js

Open alexsegura opened this issue 8 years ago • 7 comments

We should use Immutable.js for the store, to simplify stuff.

alexsegura avatar Jul 25 '17 11:07 alexsegura

Hi @alexsegura

What about using: https://github.com/leoasis/redux-immutable-state-invariant? You wouldn't need to use an extra lib and you could still use native JS

mastilver avatar Jul 31 '17 12:07 mastilver

Thank you for your comment.

It says "For development use only".

The goal to use Immutable.js is to avoid having to clone arrays for example

alexsegura avatar Aug 01 '17 10:08 alexsegura

Yes you are right, you would still need to clone your array.

You should have a look at http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html to prevent that

mastilver avatar Aug 01 '17 11:08 mastilver

Immutable.js is great, but this is adding yet another Facebook library 😐

alexsegura avatar Aug 01 '17 11:08 alexsegura

Hi guys, great project. I had a quick look at the reducers and I don't think there is any need for cloning or deep cloning objects. One usual way to do it is to store objects by their ids (using normalizr for instance and as suggested by @mastilver in the link above) and then it's easy to update a given id in the reducer (es6 spread ... helps to keep things readable). Immutable could be the next step (i think it's a nice addition even though my experience tells me that well written reducer and reducers tests are usually enough to prevent mutating), but I think the reducers need to be reworked a bit before adding immutable. I hope it helps, I'd be happy to assist you or submit a pull request if that's of any interest to you.

tom-s avatar Aug 10 '17 18:08 tom-s

for instance case 'CREATE_ADDRESS_SUCCESS': user = _.cloneDeep(state); user.addresses.push(action.address);

could become

case 'CREATE_ADDRESS_SUCCESS': return { ...state, addresses: [ ...state.addresses, action.address ] }

It guaranties immutability because every action returns a new state (same goes for your "substates" like adresses)

tom-s avatar Aug 10 '17 18:08 tom-s

Hi @tom-s, many thanks for your kind comments.

Actually I confess this is the 2nd time in my life I'm using Redux, and the 1st time we used Immutable.js 😅

You can definitely submit a Pull Request! We would really be interested in reducers tests. Do you use Jest for testing?

alexsegura avatar Aug 11 '17 08:08 alexsegura