micro-observables icon indicating copy to clipboard operation
micro-observables copied to clipboard

Observable.proxy idea

Open pkieltyka opened this issue 3 years ago • 2 comments

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?

pkieltyka avatar May 20 '21 19:05 pkieltyka

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?

simontreny avatar May 21 '21 19:05 simontreny

+1 for the Immer integration!

otaviomad avatar Nov 04 '21 17:11 otaviomad