canjs / can-observe

Observable objects
https://canjs.com/doc/can-observe.html
MIT License
20 stars 2 forks source link

Support observing Map/Set (and Weak conterparts) #48

Open DesignByOnyx opened 6 years ago

DesignByOnyx commented 6 years ago

There's another popular observable library out there that enables this, so it'd be cool if ours did too.

justinbmeyer commented 6 years ago

I think the first step would be to figure out what the patch event object should look like for these types:

https://canjs.com/doc/can-symbol/types/Patch.html

I think I already made the values one for set: {type: "values", delete, insert}

I think set, add, and delete should be fine for Map.

justinbmeyer commented 6 years ago

I'm not sure can-observation-recorder is setup so it can listen to patches yet. This would be the "easy" way to get these types to at least be bind-able ... at the cost of Map being inefficient.

This is to say, if someone reads from a Map or Set, the easy thing would be to call something like:

get(){ 
  ObservationRecorder.add( set, can.onPatchesSymbol );
}

We'd want ObservationRecorder and Observation to eventually do a canReflect.onPatch() binding.

The cost of Map being inefficient is that this would cause every handler to re-run when any property is changed. This would cause a lot of re-renders if used as a something like a ViewModel.

Eventually, we'd want to make Map support canReflect.onKeyValue() where the keys could be objects. To support this ... it would need a KeyTree like [Map, Map, Array]