Comments (4)
observable
is returning a wrapper Proxy around the passed object. It is not doing anything with the original object. Your above example should look like this:
const observableForm = observable(this.form)
observe(() => {
console.log(observableForm.someField)
})
And then when you change someField
the observer will automatically run.
In case you use multiple fields of the observable the observer runs if any of those are mutated.
// this runs when someField or otherField is changed
observe(() => {
console.log(observableForm.someField + observableForm.otherField)
})
Any property access or mutation at any depth is picked up and causes the relevant observers to rerun. console.log
is a special edge case. It is a method that does not use JS to access the object's properties but uses some internal code in most platforms. This means that the observer-util can not pick up property accesses by console.log
. You can do this for your use case:
observe(() => {
console.log(JSON.stringify(observableForm))
})
JSON.stringify
is implemented in plain JS and the observer-util can pick up any property access that happens inside it.
If you are interested in how this library works you should read this article which explains it through a very light React wrapper.
I hope this helped.
from observer-util.
Thanks, that makes sense! What happens if I wanted to have two observable objects, like this?
const observableA = observable(a)
const observableB = observable(b)
observe(() => {
// Is this A or B that's changed?
})
from observer-util.
const observableA = observable({ prop1: 1, prop2: 2 })
const observableB = observable({ prop1: 12, prop2: 'Hello' })
observe(() => {
console.log(observableA.prop2 + observableB.prop1)
})
The observed function would run when observableA.prop2
or observableB.prop1
are changed since they are used by the observed function. It would not run when observableA.prop1
or observableB.prop2
are changed since they are not used inside the function (and wouldn't result in a different function effect).
from observer-util.
Interesting, thanks for explaining it to me!
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
- 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.