-
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
import { attr, el, on, series, signal } from "./ef.js";
import { fetchUserInfo } from "./api.js";
const $id = signal("alice");
// Changing this Signal value does not trigger re-render until the body of `series`
// callback evaluates `$retry.get()`.
const $retry = signal(0);
// This element would be reused
// You can test on browser devtool
const retryButton = el(
"button",
[
on("click", () => {
$retry.update((prev) => prev + 1);
}),
],
["Retry (works only for error state)"],
);
document.body.appendChild(
el(
"div",
[],
[
el(
"select",
[
on("change", (ev) => {
if (!(ev.currentTarget instanceof HTMLSelectElement)) {
return;
}
$id.set(ev.currentTarget.value);
}),
],
[
el("option", [attr("value", "alice")], ["Alice"]),
el("option", [attr("value", "bob")], ["Bob"]),
el("option", [attr("value", "carol")], ["Carol"]),
el("option", [attr("value", "dave")], ["Dave (does not exist)"]),
],
),
series(el("p", [], ["Loading..."]), async function* (ctx, s) {
try {
const url = new URL("https://example.com");
// This `$id.get(ctx)` associates `$id` to the containing `series`.
// Everytime `$id` updated, the `series` re-runs thanks to this.
url.searchParams.set("id", $id.get(ctx));
const user = await fetchUserInfo(new Request(url), s);
yield el(
"div",
[],
[
el(
"dl",
[],
[
el("dt", [], ["ID"]),
el("dd", [], [user.id]),
el("dt", [], ["Display Name"]),
el("dd", [], [user.displayName]),
],
),
// This retry button do nothing because `$retry` is not associated to this `series`.
retryButton,
],
);
$retry.set(0);
} catch (error) {
yield el(
"div",
[],
[
el(
"p",
[],
[
"Failed to fetch users (retry=",
// This association occurs only control enters this `catch` branch.
// `ctx` is required here, otherwise calling `$retry.set` does not let
// this effect (`series`) to be re-run.
$retry.get(ctx).toString(10),
"): ",
String(error),
],
),
retryButton,
],
);
}
}),
],
),
);