BeTomorrow / micro-observables

A simple Observable library that can be used for easy state management in React applications.
MIT License
104 stars 8 forks source link

Observable.proxy idea #15

Open pkieltyka opened 3 years ago

pkieltyka commented 3 years ago

I have a scenario where I have an object that I'd like to turn into a micro-observable so I can use with the rest of observable state in the project.

I initially thought I could use Observable.compute(), but of course this is to compute only other structures already represented as an Observable value from micro-observables.

How about the idea of adding a Observable.proxy or Observable.wrap method, which could use the Proxy API to listen for changes in these situations?

simontreny commented 3 years ago

Hi Peter!

Thanks for the very interesting idea! Personally, I'm not fond of the Proxy API as I think it adds too much implicitness and it makes it hard to track the code that mutates observables and that could trigger side-effects (e.g. re-rendering of components, writing to storage).

I have the idea to integrate Immer into Micro-observables to make it easier to update complex objects, without having to rely on complex spread operators. That would basically work like this:

const person = observable({
   firstName: "Steve",
   children: ["Mary"]
});

person.mutate(p => {
   p.firstName = "Tim";
   p.children.push("Oliver");
});

Do you think it could work for your use case? If not, could you tell me more about what you are trying to do?

otaviomad commented 2 years ago

+1 for the Immer integration!