-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.html
144 lines (129 loc) · 3.68 KB
/
web.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Simply Reactive</title>
</head>
<body>
<div>
<span id="out-a"></span>
*
<span id="out-b"></span>
=
<span id="out-product"></span>
</div>
<lable>
A:
<input id="in-a" />
</lable>
<lable>
B:
<input id="in-b" />
</lable>
<br />
<label for="should-render">
Render Changes
<input type="checkbox" id="should-render" checked></input>
</label>
<br />
<label for="do-update">
Do Updates
<input type="checkbox" id="do-update" checked></input>
</label>
<br />
<pre class="mermaid" id="dep-graph"></pre>
<div id="dep-graph-out"></div>
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@9/dist/mermaid.esm.min.mjs';
mermaid.mermaidAPI.initialize({ startOnLoad: false });
import {
createAtom,
createSelector,
createEffect,
createEffectGroup,
visualizeDependencyGraph,
} from '../cdn/simply-reactive.mjs';
const renderGraph = async () =>
(document.querySelector('#dep-graph-out').innerHTML =
await mermaid.mermaidAPI.render(
'dep-graph',
visualizeDependencyGraph()
));
const A = createAtom({
key: 'Atom A',
default: 2,
});
const B = createAtom({
key: 'Atom B',
default: 3,
});
const Product = createSelector({
key: 'Product',
get: () => A.get() * B.get(),
});
const RenderA = createEffect(
() => {
document.getElementById('in-a').value = A.get();
document.getElementById('out-a').innerText = A.get();
},
{ key: 'Render A' }
);
const RenderB = createEffect(
() => {
document.getElementById('in-b').value = B.get();
document.getElementById('out-b').innerText = B.get();
},
{ key: 'Render B' }
);
const RenderProduct = createEffect(
() => {
document.getElementById('out-product').innerText = Product.get();
},
{ key: 'Render Product' }
);
const EventListeners = createEffectGroup([
() => {
const listener = (ev) => A.set(parseInt(ev.target.value, 10));
document
.getElementById('in-a')
.addEventListener('change', listener);
return () => document
.getElementById('in-a')
.removeEventListener('change', listener);
},
() => {
const listener = (ev) => B.set(parseInt(ev.target.value, 10));
document
.getElementById('in-b')
.addEventListener('change', listener);
return () => document
.getElementById('in-b')
.removeEventListener('change', listener);
}
]);
document.getElementById('do-update').addEventListener('click', (ev) => {
if (ev.target.checked) {
EventListeners.restore();
} else {
EventListeners.destroy();
}
renderGraph();
});
document.getElementById('should-render').addEventListener('click', (ev) => {
if (ev.target.checked) {
RenderA.restore();
RenderB.restore();
RenderProduct.restore();
} else {
RenderA.destroy();
RenderB.destroy();
RenderProduct.destroy();
}
renderGraph();
});
renderGraph();
</script>
</body>
</html>