object-path-immutable icon indicating copy to clipboard operation
object-path-immutable copied to clipboard

ability to call set after `value()` or generate snapshot at any time

Open ilovett opened this issue 7 years ago • 1 comments

It would be helpful to either be able to get a copy of the next immutable state, or at least fetch what the next values will be... This would be helpful for calculating aggregations on new data in reducers...

case: 'ADD_USER':
  next = immutable(state);
  next.set(['path' ,'to', 'users', 'byId', user.id], user);
  next.set(['path', 'to', 'users', 'aggregations', 'totalUsers', Object.keys(next.get(['path', 'to', 'users', 'byId'])).length);
  return next.value();

Getting would be preferable or at least being able to call next.value() then a set then another next.value() if I need to, but I get an error that I can't set after calling value:

Error: Cannot call set after `value`

ilovett avatar Jan 26 '18 23:01 ilovett

Another possibility would be having a constructor that recognizes if the passed in data is already an instance of immutable to just make a copy: EG:

next = immutable(state);
next.set('something', 33);
snapshot = immutable(next).value();
next.set('else', snapshot.something * 2);

ilovett avatar Jan 26 '18 23:01 ilovett