rtfeldman / seamless-immutable

Immutable data structures for JavaScript which are backwards-compatible with normal JS Arrays and Objects.
BSD 3-Clause "New" or "Revised" License
5.37k stars 195 forks source link

subscribe or notify callback handler option? #118

Closed mglazer-cengage closed 5 years ago

mglazer-cengage commented 8 years ago

Is it possible to have a callback handler to be raised on any changes to the original object, add, edit, delete.. which would receive the key, object, oldVal and newVal(s) - the changes ?

rtfeldman commented 8 years ago

Thanks for the question! I'm not sure what this would mean in the context of an immutable library, though; the original object never changes, so I don't think those handlers would ever file. :smile:

danprince commented 8 years ago

It sounds like you might be thinking of Clojure's atoms. Atoms normally wrap an immutable then watch for it being replaced from by an update (or reset) method.

If you pair together js-atom with seamless-immutable, you can simulate this behaviour.

import Atom from 'js-atom';
import Immutable from 'seamless-immutable';

const arr = Immutable([1, 2, 3]);
const atom = Atom(arr);

atom.addWatch('someKey', (_, _, old, new) => {
  console.log('update', old, new);
});

atom.update(arr => arr.concat([4]));
// 'update' [1, 2, 3] [1, 2, 3, 4]
tusharmath commented 8 years ago

@mglazer-cengage also take a look at https://github.com/tusharmath/reactive-storage. It will expose the store as an Observable.

apologies for a little bit of self promotion

jeffbski commented 8 years ago

I believe the question is asking whether there is anything like the Immutable.js cursor library for seamless. https://github.com/facebook/immutable-js/tree/master/contrib/cursor

With immutable.js and cursors you can easily create an atom that when you update things via the cursor the listeners fire automatically telling you of new state. Things are still immutable and all. Using immstruct makes that real easy to setup.

With the js-atom approach above you have to make changes from the top, possibly using a deep path, while with the cursor approach, you get cursors which are pointing to deep structure and you call update right on them which in turn causes new immutable atom to be created and listeners to fire.

MartinSnyder commented 8 years ago

I believe the question is asking whether there is anything like the Immutable.js cursor library for seamless. https://github.com/facebook/immutable-js/tree/master/contrib/cursor

I believe Issue #18 is relevant. In it, @rtfeldman says there are no plans for cursors to be part of this library.

MartinSnyder commented 8 years ago

I just made a gist of a cursor I wrote around seamless-immutable. Happy to submit a PR or publish an add-on if there is interest, but I don't want to clog the pipes with something that isn't needed.

In response to the original question, the top-level (Data) object allows subscribers to invoke "onChange" which is then invoked any time the managed data is updated via a cursor.

https://gist.github.com/MartinSnyder/2e7d2ecde16d910f25926fc8f59fe25b

rtfeldman commented 8 years ago

I'd definitely publish as a separate package. I'd like to keep the core decoupled from extras where possible. 😄

MartinSnyder commented 7 years ago

I finally got around to doing this. Presenting seamless-immutable-cursor: https://github.com/MartinSnyder/seamless-immutable-cursor

Version 0.1.1 is published to npm: https://www.npmjs.com/package/seamless-immutable-cursor