arqex / freezer

A tree data structure that emits events on updates, even if the modification is triggered by one of the leaves, making it easier to think in a reactive way.
MIT License
1.28k stars 56 forks source link

Support for Maps #60

Closed Tjorriemorrie closed 8 years ago

Tjorriemorrie commented 8 years ago

Can support be added for ES2015 Map data structures? Converting it to and from json does not correctly convert Maps.

arqex commented 8 years ago

Hi @Tjorriemorrie

Map support is not planned. When you JSON.stringify a map object you get a simple object, and something similar happens when you add it to a freezer store, all the functions get stripped off.

Tjorriemorrie commented 8 years ago

I thought as much. What about passing a possible second argument when the freezer is created from an json object? And that argument is the schema of field types to parse it to?

arqex commented 8 years ago

Trying to parse class instances inside freezer would have a really big downside: those object could be mutated without triggering events.

For example, the Map class has all the getter and setter methods that modify its internal state. Even when you freeze it you are able to use that methods to update it:

var m = new Map(); // Map {}
Object.freeze( m ); // The map should be freezer
m.set('a', 1);
console.log( m ) // Map {"a" => 1} we have just updated the frozen map :(

This way we lose the point of using freezer.

Tjorriemorrie commented 8 years ago

Thank you for the detailed explanation.