observer-util icon indicating copy to clipboard operation
observer-util copied to clipboard

ignore rather than expose raw object

Open lukeburns opened this issue 6 years ago • 7 comments

@solkimicreb, would it be difficult to expose an ignore function that operates identically to raw within an observer but without exposing the actual raw object?

this would allow me to avoid the unwrapping problem mentioned in https://github.com/nx-js/observer-util/issues/18 and the vulnerabilities that come with unwrapping. see https://github.com/lukeburns/immutator/issues/2 for some more details.

in short, it would nice to prevent reactions from triggering while still preserving the properties of proxies that wrap observables:

let { observable, ignore } = require('@nx-js/observer-util')
let state = anotherProxy(observable({ time: 0 })
observe(() => console.log(ignore(state.time))) // triggers only once on initial run
setInterval(() => state.time++, 1000)

lukeburns avatar Feb 25 '18 02:02 lukeburns

console.log(ignore(state.time)) would not work. In order to properly ignore reactivity I need to have a function that I can run and observe. console.log(ignore(state.time)) passes only the result of state.time to ignore and it has no idea what happened. ignore(() => state.time)) could work, but in this case I would rather overload the raw function with a possible function argument - like ignore(raw(() => state.time)).

I am still thinking about unwrapping though. It is a bit mind bending and I find it difficult to put it in a comment, but I am working on it 😄

solkimicreb avatar Mar 01 '18 11:03 solkimicreb

If you overload raw, would it make sense to have it return the return value of the function passed to it? So the example above would become...

let { observable, ignore } = require('@nx-js/observer-util')
let state = anotherProxy(observable({ time: 0 })
observe(() => console.log(raw(() => state.time))) // triggers only once on initial run
setInterval(() => state.time++, 1000)

lukeburns avatar Mar 05 '18 13:03 lukeburns

Hm nvm, that defeats the purpose. Might as well just use raw(state.time).

lukeburns avatar Mar 05 '18 14:03 lukeburns

I think the issue with raw may be on your side

On Mon, Mar 5, 2018 at 3:03 PM, Luke Burns [email protected] wrote:

Hm nvm, that defeats the purpose. Might as well just use raw(state.time).

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nx-js/observer-util/issues/19#issuecomment-370428398, or mute the thread https://github.com/notifications/unsubscribe-auth/AGoj7qyD-yJY0U3i-yaeYM5fLsYON7vLks5tbUWfgaJpZM4SSIqL .

solkimicreb avatar Mar 05 '18 14:03 solkimicreb

Storing the raw obj on a property of the object (proxy) has some delicate edge cases. Now I remember why I decided to use WeakMaps instead.

A WeakMap is basically a storage for private, own properties. If you store props with a property of the object itself, the raw retrieval follows the somewhat complex get operation rules instead of a simple hashmap lookup (in the WeakMap case).

It is intercepted by other proxies and it walks the prototype chain which can cause a mess in complex cases. Take a look at this example:

const obs = observable({})
const obj = {}

Object.setPrototypeOf(obj, obs)

// I expect this to be obj itself
raw(obj)

// if I store `raw` on object.raw -> raw(obj) is the raw version of its prototype (obs) instead
// if I store `raw` in a WeakMap raw(obj) is correctly obj

I suspect something similar happens with multi-proxy wrapping when you store raw on a props. I will try to make a sensible example for it.

solkimicreb avatar Mar 05 '18 14:03 solkimicreb

Suppose you have an observable object sandwiched between many other proxies (e.g. a logger, an observable, a db binding, and an immutator). Without something like an ignore function, one has to not just unwrap but also rewrap with the other proxies to preserve their behavior, which becomes a large and wasteful task (especially if each proxy is stateful and requires transfer of that state during rewrapping).

Bypassing a particular proxy without unwrapping / rewrapping is one that would save developer time and the extra computational costs due to the unwrapping / rewrapping.

Maybe there's a better solution to this problem. It's certainly not specific to observer-util, and given a multi-layer proxy, I would likely wish for a common interface across those proxies for bypassing behavior.

lukeburns avatar Mar 05 '18 15:03 lukeburns

So did this not go anywhere? Ignore would be useful in cases where you want inert layers in multi nested contexts. Like if you were writing a memoized mapping function that returns the previous rows of a list if they are already present. You wrap over the list with a observer but you don't want the mapping function to be active as you don't want any necessary accessors to trigger the whole list from updating. Sure you could raw all the references in the map function but it might not be obvious to the consumer they have to do so.

While an ignore function should return its value, it differs from raw I'd imagine since if your return value was an observable you'd expect raw to return the raw value, whereas ignore would return whatever you wanted (perhaps a newly mapped observable).

ryansolid avatar Apr 29 '19 01:04 ryansolid