-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserver.js
More file actions
30 lines (28 loc) · 765 Bytes
/
Copy pathObserver.js
File metadata and controls
30 lines (28 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Observer {
constructor(data) {
this.observe(data);
}
observe(data) {
if (!data || typeof data !== 'object') return;
Object.keys(data).forEach(key => {
this.defineReactive(data, key, data[key]);
this.observe(data[key]);
});
}
defineReactive(obj, key, value) {
let dep = new Dep();
Object.defineProperty(obj, key, {
get() {
Dep.target && dep.addSub(Dep.target);
return value;
},
set: newVal => {
if (newVal !== value) {
this.observe(newVal);
value = newVal;
dep.notify();
}
}
});
}
}