Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore rather than expose raw object #19

Open
lukeburns opened this issue Feb 25, 2018 · 7 comments
Open

ignore rather than expose raw object #19

lukeburns opened this issue Feb 25, 2018 · 7 comments

Comments

@lukeburns
Copy link

lukeburns commented Feb 25, 2018

@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 #18 and the vulnerabilities that come with unwrapping. see lukeburns/immutator#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)
@solkimicreb
Copy link
Member

solkimicreb commented Mar 1, 2018

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 😄

@lukeburns
Copy link
Author

lukeburns commented Mar 5, 2018

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
Copy link
Author

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

@solkimicreb
Copy link
Member

solkimicreb commented Mar 5, 2018 via email

@solkimicreb
Copy link
Member

solkimicreb commented Mar 5, 2018

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.

@lukeburns
Copy link
Author

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.

@ryansolid
Copy link

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants