2019-04-24 11:54:26 +02:00
|
|
|
# 🦉 Observer 🦉
|
2019-04-23 15:26:00 +02:00
|
|
|
|
2019-10-17 09:50:03 +02:00
|
|
|
Owl needs to be able to react to state changes. For example, whenever the state
|
2019-10-20 09:01:38 +02:00
|
|
|
of a component is changed, Owl needs to rerender it. To help with that, there is
|
|
|
|
|
an Observer class. Its job is to observe the state of an object (or array), and
|
|
|
|
|
to react to any change. The observer is implemented with the native `Proxy`
|
|
|
|
|
object. Note that this means that it will not work on older browsers.
|
|
|
|
|
|
|
|
|
|
Note that the `Observer` is used by the `useState` and `useContext` hooks. This
|
|
|
|
|
is the way most Owl applications will create observers. For the majority of
|
|
|
|
|
use cases, there is no need to directly instantiate an observer.
|
|
|
|
|
|
|
|
|
|
## Example
|
2019-04-25 15:28:20 +02:00
|
|
|
|
|
|
|
|
For example, this code will display `update` in the console:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
const observer = new owl.Observer();
|
|
|
|
|
observer.notifyCB = () => console.log("update");
|
2019-09-26 21:56:01 +02:00
|
|
|
const obj = observer.observe({ a: { b: 1 } });
|
2019-04-25 15:28:20 +02:00
|
|
|
|
|
|
|
|
obj.a.b = 2;
|
|
|
|
|
```
|
2019-04-24 11:54:26 +02:00
|
|
|
|
2019-10-20 09:01:38 +02:00
|
|
|
This example shows that an observer can observe nested properties.
|
|
|
|
|
|
|
|
|
|
## Reference
|
|
|
|
|
|
|
|
|
|
**observe** An observer can observe multiple values with the `observe` method.
|
|
|
|
|
This method takes an object or an array as its argument and will return a proxy
|
|
|
|
|
(which is mapped to the initial object/array). With this proxy, the observer
|
|
|
|
|
can detect whenever any internal value is changed.
|
|
|
|
|
|
|
|
|
|
**Registering a callback** Whenever an observer sees a state change, it will
|
|
|
|
|
call its `notifyCB` method. No additional information is given to the callback.
|
|
|
|
|
|
|
|
|
|
**deepRevNumber** Each observed value has an internal revision number, which
|
|
|
|
|
is incremented every time the value is observed. Sometimes, it can be useful
|
|
|
|
|
to obtain that number:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const observer = new owl.Observer();
|
|
|
|
|
const obj = observer.observe({ a: { b: 1 } });
|
|
|
|
|
|
|
|
|
|
observer.deepRevNumber(obj.a); // 1
|
|
|
|
|
obj.a.b = 2;
|
|
|
|
|
|
|
|
|
|
observer.deepRevNumber(obj.a); // 2
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The `deepRevNumber` can also return 0, which indicates that the value is not
|
|
|
|
|
observed.
|