Comments (7)
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.
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.
Hm nvm, that defeats the purpose. Might as well just use raw(state.time)
.
from observer-util.
from observer-util.
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.
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.
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)
- Mutating observables in reactions is forbidden HOT 2
- 4.2.0 broke something HOT 3
- Trap "toJSON" to allow serialization of proxies HOT 2
- Deeply observe object HOT 12
- Documentation suggestion HOT 2
- Is it possible to receive the type of change when observing a value? HOT 9
- Observables arrays don't appear to observe correctly HOT 3
- Question about scheduler batching and observe optimizations. HOT 2
- Problem when changing whole objects HOT 1
- Understanding reactions HOT 1
- Angular 7 Integration HOT 1
- Not sure I'm understanding this at all HOT 4
- Object.defineProperty should trigger reactions HOT 3
- computed properties no cache? HOT 3
- Observing through a Proxy HOT 2
- `construct` trap fails for observed constructors HOT 1
- Bug or user error? Copying observable to other position in array converts it to the raw item
- should observe explicit array length changes
- Shallow observing HOT 1
- rawToProxy WeakMap.set is never called
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from observer-util.