Repository navigation
Expand file tree
/
Copy pathobjectWatcher.js
More file actions
109 lines (86 loc) · 2.28 KB
/
Copy pathobjectWatcher.js
File metadata and controls
109 lines (86 loc) · 2.28 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
objectListener.js
Listen the object is changes or not.
*/
'use strict';
class ObjectListener {
constructor(obj) {
this._listeners = new Map();
this._nextId = 1;
this._closed = false;
this.target = obj;
this.proxy = new Proxy(obj, {
set: (target, prop, value) => {
if (this._closed) return true;
const oldValue = target[prop];
target[prop] = value;
const event = {
prop,
oldValue,
newValue: value,
target
};
for (const fn of this._listeners.values()) {
try {
fn(event);
} catch (err) {
console.error("Listener error:", err);
}
}
return true;
}
});
return this.proxy;
}
addEventListener(fn) {
const id = this._nextId++;
this._listeners.set(id, fn);
return id;
}
cancelEventListener(id) {
this._listeners.delete(id);
}
close() {
this._listeners.clear();
this._closed = true;
}
};
export default ObjectListener;
/*
Code used for test out! ~~~
*/
if (false){
// 1. Your JSON-like tree
const filedata = {
user: {
profile: {
name: "Ray",
age: 16
}
}
};
// 2. Take a branch
const piece = filedata.user.profile;
// 3. Wrap it
const listener = new ObjectListener(piece);
// 4. Add listeners
const id1 = listener.addEventListener(event => {
console.log("Listener 1:", event);
});
const id2 = listener.addEventListener(async event => {
console.log("Async listener:", event);
await new Promise(r => setTimeout(r, 300));
console.log("Async done for:", event.prop);
});
// 5. Modify the object
listener.name = "Raymond";
listener.age = 17;
// 6. Remove one listener
listener.cancelEventListener(id1);
// 7. Modify again (only async listener fires)
listener.age = 18;
// 8. Close watcher
listener.close();
// 9. No more events
listener.age = 19; // ignored
}