redbadger / immutable-cursor

👊 Immutable cursors incorporating the Immutable.js interface over a Clojure-inspired atom
Other
59 stars 7 forks source link

.withMutations() doesn't work when mutations are performed on a derived cursor of the transient, mutable copy #7

Closed jameshopkins closed 8 years ago

jameshopkins commented 8 years ago
const data = Immutable.fromJS({
  a: 'wahey',
  b: 'moo',
  c: []
});

const c1 = Cursor.from(data, onChange);
const c2 = c1.withMutations(m => m.get('c').push('hello'));

yields:

{a: 'wahey', b: 'moo', c: []}

when I would expect:

{a: 'wahey', b: 'moo', c: ['hello']}
charypar commented 8 years ago

I'm not sure you can use withMutations to change things nested in the map. The mutable copy is of the map only, not everything stored in it. The documentation also points out that:

Note: Not all methods can be used on a mutable collection or within withMutations! Only set and merge may be used mutatively.

The point of withMutations is to speed up a series of sets, not all the calls of all the mutative APIs on everything stored within. I'm not even sure how that would work :)