jesseskinner / hover

A very lightweight data store with action reducers and state change listeners.
MIT License
98 stars 7 forks source link

Allow stores to implement state cloning/copying or other immutable protections via getState #2

Closed jesseskinner closed 9 years ago

jesseskinner commented 9 years ago

Currently the state object returned by getState and this.state is created by making a serialized copy with this:

JSON.parse(JSON.stringify(state));

Users might want to use an immutable data structure instead. They might want to allow functions or use Object.assign to clone the state. Or any other combination of things.

To keep the API simple, we could just allow each store to define a getState() method to implement it's own cloning (or immutable object wrapping or whatever). It might look something like this:

Hoverboard({
    getState: function(state){
        return JSON.parse(JSON.stringify(state));
    }
});

If we do that, then the question is: should Hoverboard stop cloning the state by default with JSON serialization, and be agnostic by default with a mutable state?