-
Notifications
You must be signed in to change notification settings - Fork 82
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
Comments
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 😄 |
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) |
Hm nvm, that defeats the purpose. Might as well just use |
I think the issue with raw may be on your side
…On Mon, Mar 5, 2018 at 3:03 PM, Luke Burns ***@***.***> 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
<#19 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGoj7qyD-yJY0U3i-yaeYM5fLsYON7vLks5tbUWfgaJpZM4SSIqL>
.
|
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 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. |
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. |
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). |
@solkimicreb, would it be difficult to expose an
ignore
function that operates identically toraw
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:
The text was updated successfully, but these errors were encountered: