BeTomorrow/micro-observables

Observable.proxy idea

Opened this issue · 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?

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?

+1 for the Immer integration!