-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathevent-target.js
More file actions
61 lines (51 loc) · 1.44 KB
/
event-target.js
File metadata and controls
61 lines (51 loc) · 1.44 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
'use strict';
class PointEvent extends Event {
constructor(type, detail = {}) {
super(type);
this.detail = detail;
}
}
class Point {
#x;
#y;
constructor({ x, y }) {
const errors = Point.validate(x, y);
if (errors.length > 0) {
const cause = new AggregateError(errors, 'Validation');
throw new RangeError('Bad coordinates', { cause });
}
this.#x = x;
this.#y = y;
this.target = new EventTarget();
this.target.addEventListener('move', (event) => {
const { x: dx, y: dy } = event.detail;
this.#x += dx;
this.#y += dy;
});
this.target.addEventListener('clone', (event) => {
event.detail.callback(new Point({ x: this.#x, y: this.#y }));
});
this.target.addEventListener('toString', (event) => {
event.detail.callback(`(${this.#x}, ${this.#y})`);
});
}
static validate(x, y) {
const errors = [];
if (!Number.isFinite(x)) errors.push(new TypeError(`Invalid x: ${x}`));
if (!Number.isFinite(y)) errors.push(new TypeError(`Invalid y: ${y}`));
return errors;
}
emit(type, detail = {}) {
this.target.dispatchEvent(new PointEvent(type, detail));
}
}
// Usage
const p1 = new Point({ x: 10, y: 20 });
p1.emit('toString', { callback: console.log });
p1.emit('clone', {
callback: (c1) => {
c1.emit('toString', { callback: console.log });
c1.emit('move', { x: -5, y: 10 });
c1.emit('toString', { callback: console.log });
},
});