forked from exercism/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
101 lines (82 loc) · 1.9 KB
/
Copy pathexample.js
File metadata and controls
101 lines (82 loc) · 1.9 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
class InputCell {
constructor(value) {
this.value = value;
this.updated = true;
this.subscribers = [];
}
setValue(value) {
if (value !== this.value) {
this.value = value;
this.notify();
}
}
notify() {
this.subscribers.forEach(sub => sub.markForUpdate());
this.subscribers.forEach(sub => sub.update());
}
addSubscriber(sub) {
this.subscribers.push(sub);
}
}
class ComputeCell {
constructor(inputCells, fn) {
this.fn = fn;
this.inputCells = inputCells;
this.inputCells.forEach(cell => cell.addSubscriber(this));
this.subscribers = [];
this.value = fn(inputCells);
this.callbacks = [];
this.updated = true;
this.lastValue = this.value;
}
update() {
const value = this.fn(this.inputCells);
if (value !== this.value) {
this.value = value;
this.updated = true;
this.notify();
}
}
notify() {
this.subscribers.forEach(sub => sub.markForUpdate());
this.subscribers.forEach(sub => sub.update());
this.runCallbacks();
}
markForUpdate() {
this.updated = false;
this.subscribers.forEach(sub => sub.markForUpdate());
}
runCallbacks() {
if (this.allInputsUpdated() && this.valueChanged()) {
this.lastValue = this.value;
this.callbacks.forEach(cb => cb.run(this));
}
}
allInputsUpdated() {
return this.inputCells
.filter(cell => cell.updated)
.length === this.inputCells.length;
}
valueChanged() {
return this.lastValue !== this.value;
}
addSubscriber(sub) {
this.subscribers.push(sub);
}
addCallback(cb) {
this.callbacks.push(cb);
}
removeCallback(cb) {
this.callbacks = this.callbacks.filter(c => c !== cb);
}
}
class CallbackCell {
constructor(fn) {
this.fn = fn;
this.values = [];
}
run(cell) {
this.values.push(this.fn(cell));
}
}
export { InputCell, ComputeCell, CallbackCell };