Giter VIP home page Giter VIP logo

Comments (7)

solkimicreb avatar solkimicreb commented on May 28, 2024

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 😄

from observer-util.

lukeburns avatar lukeburns commented on May 28, 2024

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)

from observer-util.

lukeburns avatar lukeburns commented on May 28, 2024

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

from observer-util.

solkimicreb avatar solkimicreb commented on May 28, 2024

from observer-util.

solkimicreb avatar solkimicreb commented on May 28, 2024

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.

from observer-util.

lukeburns avatar lukeburns commented on May 28, 2024

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.

from observer-util.

ryansolid avatar ryansolid commented on May 28, 2024

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

from observer-util.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.