-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathreactive.js
More file actions
56 lines (47 loc) · 1.33 KB
/
reactive.js
File metadata and controls
56 lines (47 loc) · 1.33 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
'use strict';
class Observable {
constructor(value) {
this.value = value;
this.subscribers = [];
}
subscribe(fn) {
this.subscribers.push(fn);
fn(this.value);
return () => {
this.subscribers = this.subscribers.filter((f) => f !== fn);
};
}
next(value) {
this.value = value;
this.subscribers.forEach((fn) => fn(value));
}
map(fn) {
const obs = new Observable(fn(this.value));
this.subscribe((v) => obs.next(fn(v)));
return obs;
}
}
const validatePoint = (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;
};
const createPoint = (x, y) => {
const errors = validatePoint(x, y);
if (errors.length > 0) {
const cause = new AggregateError(errors, 'Validation');
throw new RangeError('Bad coordinates', { cause });
}
return new Observable({ x, y });
};
const move = (point, dx, dy) =>
point.map(({ x, y }) => ({ x: x + dx, y: y + dy }));
const clone = (point) => new Observable({ ...point.value });
const toString = (point) => point.map(({ x, y }) => `(${x}, ${y})`);
// Usage
const p1 = createPoint(10, 20);
toString(p1).subscribe(console.log);
const c1 = clone(p1);
const c2 = move(c1, -5, 10);
toString(c2).subscribe(console.log);