forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs0.js
More file actions
69 lines (58 loc) · 1.21 KB
/
Copy pathjs0.js
File metadata and controls
69 lines (58 loc) · 1.21 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
const pr = console.log;
const onRightA = (data) =>
new Promise((resolve, reject) => {
if (data.a == 1) setTimeout(() => resolve(data), 100);
else reject(`expect data.a == 1 but data.a == ${data.a}`);
});
onRightA({ a: 1 }).then((dat) => pr('ok', dat)), (s) => pr('NOT OK', s);
class Observable {
constructor(obj) {
this.obj = obj;
this.cbs = [];
}
update(fn) {
const res = fn(this.obj);
for (const cb of this.cbs) {
cb(this.obj);
}
return res;
}
observe(cb) {
this.cbs.push(cb);
}
unobserve(cb) {
this.cbs = this.cbs.filter((x) => x != cb);
}
}
function Cnt(cnt) {
function f(x) {
cnt++;
return x + cnt;
}
f.cnt = () => cnt;
return f;
}
class EventRegistry {
constructor() {
this.map = new Map();
}
fireEvent(event) {
for (const cb of this.map.get(event.name) || []) {
cb(event);
}
}
listen(eventName, cb) {
const cbs = this.map.get(eventName) || [];
cbs.push(cb);
this.map.set(eventName, cbs);
}
unListen(eventName, cb) {
const cbs = this.map.get(eventName) || [];
this.map.set(
eventName,
cbs.filter((x) => x != cb),
);
}
}
// let r = fetch('../data/cities.json');
// pr(r);