boto-rb-web 0.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +17 -0
- data/assets/boto-web/boot.js +16 -0
- data/assets/boto-web/boto-web.css +32 -0
- data/assets/boto-web/dom/animate.js +49 -0
- data/assets/boto-web/dom/flip.js +41 -0
- data/assets/boto-web/dom/keyed.js +15 -0
- data/assets/boto-web/dom/morph.js +112 -0
- data/assets/boto-web/dom/navigation.js +72 -0
- data/assets/boto-web/dom/sync.js +42 -0
- data/assets/boto-web/net/base.js +5 -0
- data/assets/boto-web/net/protocol.js +46 -0
- data/assets/boto-web/net/sse.js +45 -0
- data/assets/boto-web/net/sse_stream.js +84 -0
- data/assets/boto-web/net/token.js +9 -0
- data/assets/boto-web/net/transport.js +99 -0
- data/assets/boto-web/runtime/component.js +138 -0
- data/assets/boto-web/runtime/events.js +54 -0
- data/assets/boto-web/runtime/hooks.js +82 -0
- data/assets/boto-web/runtime/live.js +12 -0
- data/assets/boto-web/runtime/on.js +36 -0
- data/assets/boto-web/runtime/runtime.js +100 -0
- data/assets/boto-web/runtime/scope.js +112 -0
- data/assets/boto-web/runtime/signals.js +123 -0
- data/assets/boto-web/template/decimal.js +93 -0
- data/assets/boto-web/template/helpers.js +31 -0
- data/assets/boto-web/template/html.js +61 -0
- data/assets/boto-web/template/hydrate.js +25 -0
- data/assets/boto-web/template/ruby/core.js +72 -0
- data/assets/boto-web/template/ruby/lists.js +63 -0
- data/assets/boto-web/template/ruby.js +5 -0
- data/lib/boto/component/arguments.rb +58 -0
- data/lib/boto/component/definition.rb +114 -0
- data/lib/boto/component.rb +107 -0
- data/lib/boto/helper.rb +14 -0
- data/lib/boto/view.rb +63 -0
- data/lib/boto/web/app_extension.rb +78 -0
- data/lib/boto/web/assets.rb +59 -0
- data/lib/boto/web/child_path.rb +38 -0
- data/lib/boto/web/code_class.rb +16 -0
- data/lib/boto/web/compiler/analysis/deps.rb +46 -0
- data/lib/boto/web/compiler/analysis/handlers.rb +47 -0
- data/lib/boto/web/compiler/analysis/walker.rb +174 -0
- data/lib/boto/web/compiler/analysis.rb +44 -0
- data/lib/boto/web/compiler/client_helper.rb +34 -0
- data/lib/boto/web/compiler/computed_body.rb +40 -0
- data/lib/boto/web/compiler/directives.rb +86 -0
- data/lib/boto/web/compiler/expression/calls.rb +149 -0
- data/lib/boto/web/compiler/expression/collections.rb +116 -0
- data/lib/boto/web/compiler/expression/literals.rb +75 -0
- data/lib/boto/web/compiler/expression/logic.rb +53 -0
- data/lib/boto/web/compiler/expression/operators.rb +85 -0
- data/lib/boto/web/compiler/expression/strings.rb +31 -0
- data/lib/boto/web/compiler/expression.rb +93 -0
- data/lib/boto/web/compiler/module_writer/handlers_js.rb +75 -0
- data/lib/boto/web/compiler/module_writer/js.rb +24 -0
- data/lib/boto/web/compiler/module_writer/tag_js.rb +132 -0
- data/lib/boto/web/compiler/module_writer/template_js.rb +89 -0
- data/lib/boto/web/compiler/module_writer/type_shape.rb +28 -0
- data/lib/boto/web/compiler/module_writer.rb +79 -0
- data/lib/boto/web/compiler/template.rb +136 -0
- data/lib/boto/web/compiler/type_map.rb +43 -0
- data/lib/boto/web/compiler/type_scope.rb +68 -0
- data/lib/boto/web/compiler.rb +9 -0
- data/lib/boto/web/dispatcher.rb +113 -0
- data/lib/boto/web/events_endpoint.rb +43 -0
- data/lib/boto/web/failure.rb +30 -0
- data/lib/boto/web/head.rb +24 -0
- data/lib/boto/web/helpers.rb +40 -0
- data/lib/boto/web/hooks.rb +39 -0
- data/lib/boto/web/html.rb +65 -0
- data/lib/boto/web/layouts/context.rb +22 -0
- data/lib/boto/web/layouts/template.rb +31 -0
- data/lib/boto/web/layouts.rb +69 -0
- data/lib/boto/web/modules.rb +37 -0
- data/lib/boto/web/page_endpoint.rb +65 -0
- data/lib/boto/web/project_helpers.rb +54 -0
- data/lib/boto/web/protocol.rb +85 -0
- data/lib/boto/web/registry.rb +86 -0
- data/lib/boto/web/registry_error.rb +8 -0
- data/lib/boto/web/renderer/erb_source.rb +199 -0
- data/lib/boto/web/renderer/render_context.rb +37 -0
- data/lib/boto/web/renderer.rb +94 -0
- data/lib/boto/web/runtime.rb +45 -0
- data/lib/boto/web/scoped_css.rb +113 -0
- data/lib/boto/web/secret_key.rb +8 -0
- data/lib/boto/web/settings.rb +27 -0
- data/lib/boto/web/snapshot/changes.rb +26 -0
- data/lib/boto/web/snapshot/inputs.rb +50 -0
- data/lib/boto/web/snapshot/token.rb +42 -0
- data/lib/boto/web/snapshot.rb +111 -0
- data/lib/boto/web/static_file.rb +35 -0
- data/lib/boto/web/stylesheet.rb +53 -0
- data/lib/boto/web/tailwind.rb +69 -0
- data/lib/boto/web/template_preview.rb +31 -0
- data/lib/boto/web/transports/http.rb +36 -0
- data/lib/boto/web/transports/sse.rb +45 -0
- data/lib/boto/web/transports.rb +21 -0
- data/lib/boto/web/version.rb +7 -0
- data/lib/boto/web/view_loader.rb +21 -0
- data/lib/boto/web.rb +17 -0
- metadata +172 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: efaa259cf78b84f8b78b45cb4e36452a982fba79986b5ecdf1f873c8eda9f0c9
|
|
4
|
+
data.tar.gz: be3d173870a9acf1553f890e337cc9d736522ccca6aec8d6e3a4e42b68d60602
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6b91aa308677c69355585d9db3989fabd180a454c4153a8bf5d442d707296fcbfefbefb582cf7432c4e7e7d4e82278c5d2ffd363d7174b6ab3511b791eaccdee
|
|
7
|
+
data.tar.gz: 5a261461c3671cf05306ada26d9e0f41ca8245ec71a9498a8ef96216b323e295f26209b148808a199d53445695b566d6afbc16aaeebd62b89089c9ed45bac92b
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Evo Tech Builder
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# boto-rb-web
|
|
2
|
+
|
|
3
|
+
Stateful views and components for Boto: server-rendered pages that react in the
|
|
4
|
+
browser. The gem is part of the `web` template, which is still in development.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```ruby
|
|
9
|
+
gem "boto-rb-web"
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Then run `bin/boto bundle`.
|
|
13
|
+
|
|
14
|
+
## Documentation
|
|
15
|
+
|
|
16
|
+
Options and how views work:
|
|
17
|
+
[Boto documentation](https://evotechbuilder.github.io/boto-rb/).
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
import { createRuntime } from "./runtime/runtime.js";
|
|
3
|
+
import { createNavigation } from "./dom/navigation.js";
|
|
4
|
+
import { HttpTransport } from "./net/transport.js";
|
|
5
|
+
import { appBase } from "./net/base.js";
|
|
6
|
+
|
|
7
|
+
const runtime = createRuntime({
|
|
8
|
+
transport: globalThis.botoTransport ?? new HttpTransport({ base: appBase(import.meta.url) }),
|
|
9
|
+
});
|
|
10
|
+
createNavigation({ runtime }).listen();
|
|
11
|
+
|
|
12
|
+
if (document.readyState === "loading") {
|
|
13
|
+
document.addEventListener("DOMContentLoaded", () => runtime.start(document));
|
|
14
|
+
} else {
|
|
15
|
+
runtime.start(document);
|
|
16
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* Components and views take no box of their own; the animations below are for data-enter and data-leave. */
|
|
2
|
+
|
|
3
|
+
boto-view, boto-block, [data-component] {
|
|
4
|
+
display: contents;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.fade-in { animation: fade-in 200ms ease-out both; }
|
|
8
|
+
.fade-out { animation: fade-out 150ms ease-in both; }
|
|
9
|
+
.slide-in { animation: slide-in 200ms ease-out both; }
|
|
10
|
+
.slide-out { animation: slide-out 200ms ease-in both; }
|
|
11
|
+
.scale-in { animation: scale-in 180ms ease-out both; }
|
|
12
|
+
.scale-out { animation: scale-out 150ms ease-in both; }
|
|
13
|
+
.shake { animation: shake 250ms ease-in-out both; }
|
|
14
|
+
|
|
15
|
+
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
|
|
16
|
+
@keyframes fade-out { from { opacity: 1; } to { opacity: 0; } }
|
|
17
|
+
@keyframes slide-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
|
|
18
|
+
@keyframes slide-out { from { opacity: 1; transform: none; } to { opacity: 0; transform: translateY(8px); } }
|
|
19
|
+
@keyframes scale-in { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: none; } }
|
|
20
|
+
@keyframes scale-out { from { opacity: 1; transform: none; } to { opacity: 0; transform: scale(0.95); } }
|
|
21
|
+
@keyframes shake {
|
|
22
|
+
0%, 100% { transform: translateX(0); }
|
|
23
|
+
25% { transform: translateX(-4px); }
|
|
24
|
+
75% { transform: translateX(4px); }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/* Zero duration still fires animationend, so a leaving element is removed at once. */
|
|
28
|
+
@media (prefers-reduced-motion: reduce) {
|
|
29
|
+
.fade-in, .fade-out, .slide-in, .slide-out, .scale-in, .scale-out, .shake {
|
|
30
|
+
animation-duration: 0s;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// A leaving element stays in the DOM, marked data-leaving, until its animation ends.
|
|
2
|
+
|
|
3
|
+
const MAX_MS = 1000;
|
|
4
|
+
|
|
5
|
+
export const classesOf = (value) => (value ?? "").split(/\s+/).filter(Boolean);
|
|
6
|
+
|
|
7
|
+
export function longestMs(...lists) {
|
|
8
|
+
const times = lists.flatMap((list) =>
|
|
9
|
+
(list ?? "").split(",").map((time) => (time.trim().endsWith("ms") ? parseFloat(time) : parseFloat(time) * 1000)),
|
|
10
|
+
);
|
|
11
|
+
return Math.max(0, ...times.map((ms) => (Number.isNaN(ms) ? 0 : ms)));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function finished(el, ms) {
|
|
15
|
+
return new Promise((resolve) => {
|
|
16
|
+
if (ms <= 0) return resolve();
|
|
17
|
+
const done = (ev) => {
|
|
18
|
+
if (ev.target === el) finish();
|
|
19
|
+
};
|
|
20
|
+
const timer = setTimeout(finish, Math.min(ms, MAX_MS));
|
|
21
|
+
function finish() {
|
|
22
|
+
clearTimeout(timer);
|
|
23
|
+
el.removeEventListener("animationend", done);
|
|
24
|
+
el.removeEventListener("transitionend", done);
|
|
25
|
+
resolve();
|
|
26
|
+
}
|
|
27
|
+
el.addEventListener("animationend", done);
|
|
28
|
+
el.addEventListener("transitionend", done);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const durationOf = (el) => {
|
|
33
|
+
const style = globalThis.getComputedStyle(el);
|
|
34
|
+
return longestMs(style.animationDuration, style.transitionDuration);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export function enter(el) {
|
|
38
|
+
const classes = classesOf(el.dataset.enter);
|
|
39
|
+
if (classes.length === 0) return;
|
|
40
|
+
el.classList.add(...classes);
|
|
41
|
+
finished(el, durationOf(el)).then(() => el.classList.remove(...classes));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Marks el as leaving before returning, so a morph running right away already skips it.
|
|
45
|
+
export function leave(el) {
|
|
46
|
+
el.setAttribute("data-leaving", "");
|
|
47
|
+
el.classList.add(...classesOf(el.dataset.leave));
|
|
48
|
+
return finished(el, durationOf(el)).then(() => el.remove());
|
|
49
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// FLIP for keyed lists: each element that moved starts at its old place and slides to the new one.
|
|
2
|
+
// The maps are keyed by element: data-key is only unique among siblings, so two lists can share a key.
|
|
3
|
+
|
|
4
|
+
export const prefersReducedMotion = () => globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false;
|
|
5
|
+
|
|
6
|
+
export function measure(elements) {
|
|
7
|
+
return new Map([...elements].map((el) => {
|
|
8
|
+
const { left, top } = el.getBoundingClientRect();
|
|
9
|
+
return [el, { left, top }];
|
|
10
|
+
}));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Only the elements present in both maps and whose position changed get a move.
|
|
14
|
+
export function deltas(before, after) {
|
|
15
|
+
const moves = new Map();
|
|
16
|
+
for (const [el, rect] of after) {
|
|
17
|
+
const old = before.get(el);
|
|
18
|
+
if (!old) continue;
|
|
19
|
+
const dx = old.left - rect.left;
|
|
20
|
+
const dy = old.top - rect.top;
|
|
21
|
+
if (dx !== 0 || dy !== 0) moves.set(el, { dx, dy });
|
|
22
|
+
}
|
|
23
|
+
return moves;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function play(before, elements, { duration = 200, reduced = prefersReducedMotion() } = {}) {
|
|
27
|
+
if (reduced) return;
|
|
28
|
+
const moves = deltas(before, measure(elements));
|
|
29
|
+
if (moves.size === 0) return;
|
|
30
|
+
for (const [el, { dx, dy }] of moves) {
|
|
31
|
+
el.style.transition = "none";
|
|
32
|
+
el.style.transform = `translate(${dx}px, ${dy}px)`;
|
|
33
|
+
void el.offsetWidth; // flush the start position so the next frame transitions from it
|
|
34
|
+
}
|
|
35
|
+
globalThis.requestAnimationFrame(() => {
|
|
36
|
+
for (const el of moves.keys()) {
|
|
37
|
+
el.style.transition = `transform ${duration}ms ease`;
|
|
38
|
+
el.style.transform = "";
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Keys are compared in order: a key repeated in the new list is added again, a key repeated in the old list is removed.
|
|
2
|
+
|
|
3
|
+
export function diffKeys(oldKeys, newKeys) {
|
|
4
|
+
const free = new Map();
|
|
5
|
+
oldKeys.forEach((key, oldIndex) => free.set(key, [...(free.get(key) ?? []), oldIndex]));
|
|
6
|
+
|
|
7
|
+
const order = newKeys.map((key, newIndex) => ({ newIndex, oldIndex: free.get(key)?.shift() ?? null }));
|
|
8
|
+
const kept = new Set(order.map((entry) => entry.oldIndex).filter((index) => index !== null));
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
order,
|
|
12
|
+
removed: oldKeys.flatMap((_, oldIndex) => (kept.has(oldIndex) ? [] : [oldIndex])),
|
|
13
|
+
added: order.filter((entry) => entry.oldIndex === null).map((entry) => entry.newIndex),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Applies the render to root's interior. A node that matches is reused and only updated, never replaced.
|
|
2
|
+
// Components, server blocks and hooks keep their interior: the render brings only their attributes.
|
|
3
|
+
|
|
4
|
+
import { diffKeys } from "./keyed.js";
|
|
5
|
+
import { enter, leave } from "./animate.js";
|
|
6
|
+
import { measure, play } from "./flip.js";
|
|
7
|
+
import { syncAttributes, syncProps } from "./sync.js";
|
|
8
|
+
|
|
9
|
+
const FROZEN = "[data-component], boto-block, [data-hook], [data-leaving]";
|
|
10
|
+
|
|
11
|
+
// A frozen element keeps its interior: the render brings only its attributes.
|
|
12
|
+
export const isFrozen = (el) => el.matches(FROZEN);
|
|
13
|
+
|
|
14
|
+
export const ownerOf = (el) =>
|
|
15
|
+
(el.hasAttribute("data-component") ? el.parentElement : el)?.closest("[data-component]") ?? null;
|
|
16
|
+
|
|
17
|
+
export function morph(root, html) {
|
|
18
|
+
const fresh = root.ownerDocument.createElement("template");
|
|
19
|
+
fresh.innerHTML = html;
|
|
20
|
+
const before = measure(keyedOf(root));
|
|
21
|
+
reconcile(root, fresh.content);
|
|
22
|
+
play(before, keyedOf(root));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function reconcile(parent, source) {
|
|
26
|
+
const oldNodes = [...parent.childNodes].filter((node) => !isLeaving(node));
|
|
27
|
+
const newNodes = [...source.childNodes];
|
|
28
|
+
const partner = pairs(oldNodes, newNodes);
|
|
29
|
+
|
|
30
|
+
const result = newNodes.map((node) => {
|
|
31
|
+
const old = partner.get(node);
|
|
32
|
+
if (!old) return node;
|
|
33
|
+
update(old, node);
|
|
34
|
+
return old;
|
|
35
|
+
});
|
|
36
|
+
const kept = new Set(result);
|
|
37
|
+
const dropped = oldNodes.filter((node) => !kept.has(node));
|
|
38
|
+
|
|
39
|
+
for (const node of dropped) if (isElement(node) && node.hasAttribute("data-leave")) leave(node);
|
|
40
|
+
placeInOrder(parent, result);
|
|
41
|
+
for (const node of dropped) if (!isLeaving(node)) node.remove();
|
|
42
|
+
for (const node of newNodes) if (!partner.has(node)) enterTree(node);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function pairs(oldNodes, newNodes) {
|
|
46
|
+
const partner = new Map();
|
|
47
|
+
const oldKeyed = oldNodes.filter(isKeyed);
|
|
48
|
+
const newKeyed = newNodes.filter(isKeyed);
|
|
49
|
+
for (const { newIndex, oldIndex } of diffKeys(identities(oldKeyed), identities(newKeyed)).order) {
|
|
50
|
+
if (oldIndex !== null) partner.set(newKeyed[newIndex], oldKeyed[oldIndex]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const oldPlain = oldNodes.filter((node) => !isKeyed(node));
|
|
54
|
+
newNodes.filter((node) => !isKeyed(node)).forEach((node, i) => {
|
|
55
|
+
const old = oldPlain[i];
|
|
56
|
+
if (old && old.nodeName === node.nodeName) partner.set(node, old);
|
|
57
|
+
});
|
|
58
|
+
return partner;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A data-key is the identity; a component is its name and path and a block its id, numbered when repeated (an each).
|
|
62
|
+
export function identities(nodes) {
|
|
63
|
+
const seen = new Map();
|
|
64
|
+
return nodes.map((el) => {
|
|
65
|
+
if (el.hasAttribute("data-key")) return `key:${el.dataset.key}`;
|
|
66
|
+
const base = el.hasAttribute("data-component")
|
|
67
|
+
? `component:${el.dataset.component}:${el.dataset.path ?? ""}`
|
|
68
|
+
: `block:${el.dataset.block}`;
|
|
69
|
+
const count = seen.get(base) ?? 0;
|
|
70
|
+
seen.set(base, count + 1);
|
|
71
|
+
return `${base}#${count}`;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function update(old, node) {
|
|
76
|
+
if (!isElement(old)) {
|
|
77
|
+
if (old.nodeValue !== node.nodeValue) old.nodeValue = node.nodeValue;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
syncAttributes(old, node);
|
|
81
|
+
if (isFrozen(old)) return;
|
|
82
|
+
reconcile(old, node);
|
|
83
|
+
syncProps(old, node);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function placeInOrder(parent, nodes) {
|
|
87
|
+
let cursor = parent.firstChild;
|
|
88
|
+
for (const node of nodes) {
|
|
89
|
+
while (cursor && isLeaving(cursor)) cursor = cursor.nextSibling;
|
|
90
|
+
if (cursor === node) cursor = cursor.nextSibling;
|
|
91
|
+
else parent.insertBefore(node, cursor);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function enterTree(node) {
|
|
96
|
+
if (!isElement(node)) return;
|
|
97
|
+
for (const el of [node, ...node.querySelectorAll("[data-enter]")]) enter(el);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function keyedOf(root) {
|
|
101
|
+
return [...root.querySelectorAll("[data-key]")].filter((el) => !el.hasAttribute("data-leaving") && !frozenAbove(root, el));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function frozenAbove(root, el) {
|
|
105
|
+
for (let p = el.parentElement; p && p !== root; p = p.parentElement) if (isFrozen(p)) return true;
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const isElement = (node) => node.nodeType === 1;
|
|
110
|
+
const isLeaving = (node) => isElement(node) && node.hasAttribute("data-leaving");
|
|
111
|
+
const isKeyed = (node) =>
|
|
112
|
+
isElement(node) && (node.hasAttribute("data-key") || node.hasAttribute("data-component") || node.localName === "boto-block");
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Moves between the project's views without a reload: fetch the HTML, morph the body, follow the history.
|
|
2
|
+
|
|
3
|
+
import { morph } from "./morph.js";
|
|
4
|
+
|
|
5
|
+
const samePath = (a, b) => a.pathname === b.pathname && a.search === b.search;
|
|
6
|
+
const KEYS = ["metaKey", "ctrlKey", "shiftKey", "altKey", "defaultPrevented"];
|
|
7
|
+
const OPT_OUT = ["download", "data-reload"];
|
|
8
|
+
|
|
9
|
+
export function shouldIntercept(link, event, location) {
|
|
10
|
+
const url = new URL(link.href, location.href);
|
|
11
|
+
const target = link.getAttribute("target");
|
|
12
|
+
return (
|
|
13
|
+
event.button === 0 &&
|
|
14
|
+
!KEYS.some((key) => event[key]) &&
|
|
15
|
+
!(target && target !== "_self") &&
|
|
16
|
+
!OPT_OUT.some((attr) => link.hasAttribute(attr)) &&
|
|
17
|
+
!link.getAttribute("rel")?.split(" ").includes("external") &&
|
|
18
|
+
url.origin === location.origin &&
|
|
19
|
+
!(url.hash && samePath(url, location))
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// env: the browser globals, injected in tests.
|
|
24
|
+
export function createNavigation({ runtime, env = globalThis }) {
|
|
25
|
+
let shown = env.location.href;
|
|
26
|
+
|
|
27
|
+
async function navigate(url, { push }) {
|
|
28
|
+
const doc = await fetchPage(url, env);
|
|
29
|
+
if (!doc) return env.location.assign(url);
|
|
30
|
+
|
|
31
|
+
// The old view goes first: a morph would keep its frozen interior.
|
|
32
|
+
const swap = async () => {
|
|
33
|
+
env.document.querySelector("boto-view")?.remove();
|
|
34
|
+
morph(env.document.body, doc.body.innerHTML);
|
|
35
|
+
await runtime.start(env.document);
|
|
36
|
+
};
|
|
37
|
+
await (env.document.startViewTransition?.(swap).updateCallbackDone ?? swap());
|
|
38
|
+
|
|
39
|
+
env.document.title = doc.title;
|
|
40
|
+
if (push) {
|
|
41
|
+
env.history.pushState(null, "", url);
|
|
42
|
+
env.scrollTo(0, 0);
|
|
43
|
+
}
|
|
44
|
+
shown = url;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
listen() {
|
|
49
|
+
env.document.addEventListener("click", (event) => {
|
|
50
|
+
const link = event.target.closest("a[href]");
|
|
51
|
+
if (!link || !shouldIntercept(link, event, env.location)) return;
|
|
52
|
+
event.preventDefault();
|
|
53
|
+
navigate(link.href, { push: true });
|
|
54
|
+
});
|
|
55
|
+
// A fragment change needs no page.
|
|
56
|
+
env.addEventListener("popstate", () => {
|
|
57
|
+
if (!samePath(new URL(shown), env.location)) navigate(env.location.href, { push: false });
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function fetchPage(url, env) {
|
|
64
|
+
try {
|
|
65
|
+
const res = await env.fetch(url, { headers: { accept: "text/html" } });
|
|
66
|
+
if (!res.headers.get("content-type")?.includes("text/html")) return null;
|
|
67
|
+
const doc = new env.DOMParser().parseFromString(await res.text(), "text/html");
|
|
68
|
+
return doc;
|
|
69
|
+
} catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
// data-snapshot is the runtime's (the server's token), never the render's.
|
|
3
|
+
const OWN_ATTRIBUTES = ["data-snapshot"];
|
|
4
|
+
|
|
5
|
+
export function syncAttributes(target, source) {
|
|
6
|
+
for (const { name } of [...target.attributes]) {
|
|
7
|
+
if (!OWN_ATTRIBUTES.includes(name) && !source.hasAttribute(name)) target.removeAttribute(name);
|
|
8
|
+
}
|
|
9
|
+
for (const { name, value } of [...source.attributes]) {
|
|
10
|
+
if (target.getAttribute(name) !== value) target.setAttribute(name, value);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// A value the user typed is replaced only when the render's value differs; a focused field keeps its cursor.
|
|
15
|
+
export function syncProps(target, source) {
|
|
16
|
+
for (const prop of ["checked", "selected"]) {
|
|
17
|
+
if (prop in source && target[prop] !== source[prop]) target[prop] = source[prop];
|
|
18
|
+
}
|
|
19
|
+
if (target.localName === "select" && "value" in source) {
|
|
20
|
+
syncSelect(target, source.value);
|
|
21
|
+
} else if ("value" in source && target.value !== source.value) {
|
|
22
|
+
writeValue(target, source.value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// The options are replaced and re-selected by the morph: the select shows the index the render's value gives.
|
|
27
|
+
// shortcut: a multiple select keeps only the first selected option, upgrade when multi-selects ship
|
|
28
|
+
export function syncSelect(select, value) {
|
|
29
|
+
const index = selectedIndexFor(select.options, value);
|
|
30
|
+
if (select.selectedIndex !== index) select.selectedIndex = index;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function selectedIndexFor(options, value) {
|
|
34
|
+
return [...options].findIndex((option) => option.value === value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function writeValue(el, value) {
|
|
38
|
+
const focused = el.ownerDocument.activeElement === el && typeof el.selectionStart === "number";
|
|
39
|
+
const selection = focused ? [el.selectionStart, el.selectionEnd] : null;
|
|
40
|
+
el.value = value;
|
|
41
|
+
if (selection) el.setSelectionRange(...selection);
|
|
42
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
|
|
2
|
+
export const PROTOCOL_VERSION = 1;
|
|
3
|
+
|
|
4
|
+
export class ProtocolError extends Error {
|
|
5
|
+
constructor({ status, reason, errors = [] }) {
|
|
6
|
+
super(reason);
|
|
7
|
+
this.name = "ProtocolError";
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.reason = reason;
|
|
10
|
+
this.errors = errors;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function envelope(kind, { view, path = [], component, changed = {} }, extra = {}) {
|
|
15
|
+
return {
|
|
16
|
+
v: PROTOCOL_VERSION,
|
|
17
|
+
kind,
|
|
18
|
+
view,
|
|
19
|
+
path,
|
|
20
|
+
...(path.length > 0 ? { component } : {}),
|
|
21
|
+
changed,
|
|
22
|
+
...extra,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function actionEnvelope({ action, args = {}, ...rest }) {
|
|
27
|
+
return envelope("action", rest, { action, args });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// inputs: sent only by a child whose inputs the parent changed (untrusted on the server).
|
|
31
|
+
export function refreshEnvelope({ inputs, ...params }) {
|
|
32
|
+
return envelope("refresh", params, inputs === undefined ? {} : { inputs });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function blockEnvelope({ block, ...rest }) {
|
|
36
|
+
return envelope("block", rest, { block });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function parseResponse(json) {
|
|
40
|
+
if (json?.v !== PROTOCOL_VERSION) {
|
|
41
|
+
throw new ProtocolError({ status: 400, reason: "unsupported_version" });
|
|
42
|
+
}
|
|
43
|
+
if (json.kind === "error") throw new ProtocolError(json);
|
|
44
|
+
if (json.kind !== "result") throw new ProtocolError({ status: 500, reason: "invalid_response" });
|
|
45
|
+
return json;
|
|
46
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// SSE parser (HTML spec, as the spa's events.ts): text in pieces of any size, events on a blank line.
|
|
2
|
+
|
|
3
|
+
const LINE_END = /\r\n|\r|\n/;
|
|
4
|
+
|
|
5
|
+
export function createEventParser(onEvent, onRetry) {
|
|
6
|
+
let partial = "";
|
|
7
|
+
let started = false;
|
|
8
|
+
let skipLineFeed = false;
|
|
9
|
+
let lastId;
|
|
10
|
+
let type = "";
|
|
11
|
+
let data = [];
|
|
12
|
+
|
|
13
|
+
function dispatch() {
|
|
14
|
+
if (data.length > 0) onEvent(type || "message", data.join("\n"), lastId);
|
|
15
|
+
type = "";
|
|
16
|
+
data = [];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function handleLine(line) {
|
|
20
|
+
if (line === "") return dispatch();
|
|
21
|
+
if (line.startsWith(":")) return;
|
|
22
|
+
const colon = line.indexOf(":");
|
|
23
|
+
const name = colon === -1 ? line : line.slice(0, colon);
|
|
24
|
+
const value = colon === -1 ? "" : line.slice(colon + 1).replace(/^ /, "");
|
|
25
|
+
if (name === "event") type = value;
|
|
26
|
+
else if (name === "data") data.push(value);
|
|
27
|
+
else if (name === "id" && !value.includes("\0")) lastId = value;
|
|
28
|
+
else if (name === "retry" && /^[0-9]+$/.test(value)) onRetry(Number(value));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
push(chunk) {
|
|
33
|
+
if (chunk.length === 0) return;
|
|
34
|
+
let text = chunk;
|
|
35
|
+
if (!started) text = text.replace(/^/, "");
|
|
36
|
+
started = true;
|
|
37
|
+
// A CR at a piece's end may start a CRLF.
|
|
38
|
+
if (skipLineFeed && text.startsWith("\n")) text = text.slice(1);
|
|
39
|
+
skipLineFeed = text.endsWith("\r");
|
|
40
|
+
const lines = (partial + text).split(LINE_END);
|
|
41
|
+
partial = lines.pop();
|
|
42
|
+
lines.forEach(handleLine);
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Keeps one SSE stream open, reconnecting with Last-Event-ID; stop() ends it.
|
|
2
|
+
|
|
3
|
+
import { createEventParser } from "./sse.js";
|
|
4
|
+
|
|
5
|
+
export const BASE_DELAY_MS = 1000;
|
|
6
|
+
export const MAX_DELAY_MS = 30000;
|
|
7
|
+
const RETRY_STATUSES = [408, 429, 503];
|
|
8
|
+
|
|
9
|
+
const isClientError = (status) => status >= 400 && status < 500;
|
|
10
|
+
const retryAfterMs = (response) => {
|
|
11
|
+
const value = response.headers.get("Retry-After");
|
|
12
|
+
return value !== null && Number.isFinite(Number(value)) ? Number(value) * 1000 : undefined;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export function openEventStream({ url, fetch, timers = globalThis, onEvent }) {
|
|
16
|
+
const controller = new AbortController();
|
|
17
|
+
let lastEventId;
|
|
18
|
+
let retryMs = BASE_DELAY_MS;
|
|
19
|
+
let failures = 0;
|
|
20
|
+
let stopped = false;
|
|
21
|
+
let timer;
|
|
22
|
+
|
|
23
|
+
// abort() also errors the body being read, so consume() ends.
|
|
24
|
+
function stop() {
|
|
25
|
+
if (stopped) return;
|
|
26
|
+
stopped = true;
|
|
27
|
+
timers.clearTimeout(timer);
|
|
28
|
+
controller.abort();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function reconnect(override) {
|
|
32
|
+
if (stopped) return;
|
|
33
|
+
const delay = override ?? Math.min(MAX_DELAY_MS, retryMs * 2 ** failures);
|
|
34
|
+
failures += 1;
|
|
35
|
+
timer = timers.setTimeout(connect, delay);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// A 4xx stops the stream, except the ones that may pass later; those wait Retry-After when it comes.
|
|
39
|
+
function refused(response) {
|
|
40
|
+
const { status } = response;
|
|
41
|
+
if (isClientError(status) && !RETRY_STATUSES.includes(status)) return stop();
|
|
42
|
+
reconnect(RETRY_STATUSES.includes(status) ? retryAfterMs(response) : undefined);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function dispatch(type, data, id) {
|
|
46
|
+
if (stopped) return;
|
|
47
|
+
failures = 0;
|
|
48
|
+
if (id !== undefined) lastEventId = id;
|
|
49
|
+
onEvent(type, data);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function consume(response) {
|
|
53
|
+
const reader = response.body.getReader();
|
|
54
|
+
const decoder = new TextDecoder();
|
|
55
|
+
const parser = createEventParser(dispatch, (ms) => (retryMs = ms));
|
|
56
|
+
for (;;) {
|
|
57
|
+
const { done, value } = await reader.read();
|
|
58
|
+
if (done) return;
|
|
59
|
+
parser.push(decoder.decode(value, { stream: true }));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function connect() {
|
|
64
|
+
if (stopped) return;
|
|
65
|
+
let response;
|
|
66
|
+
try {
|
|
67
|
+
response = await fetch(url, {
|
|
68
|
+
method: "GET",
|
|
69
|
+
headers: { Accept: "text/event-stream", ...(lastEventId === undefined ? {} : { "Last-Event-ID": lastEventId }) },
|
|
70
|
+
signal: controller.signal,
|
|
71
|
+
});
|
|
72
|
+
} catch {
|
|
73
|
+
return reconnect();
|
|
74
|
+
}
|
|
75
|
+
if (!response.ok) return refused(response);
|
|
76
|
+
if (!response.body) return reconnect();
|
|
77
|
+
// A broken body is a dropped connection: it reconnects below.
|
|
78
|
+
await consume(response).catch(() => {});
|
|
79
|
+
reconnect();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
void connect();
|
|
83
|
+
return { stop };
|
|
84
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// the server does, and the browser only needs the states, inputs and the class.
|
|
2
|
+
|
|
3
|
+
export function readPayload(token) {
|
|
4
|
+
const [head] = token.split(".");
|
|
5
|
+
const base64 = head.replace(/-/g, "+").replace(/_/g, "/");
|
|
6
|
+
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
|
7
|
+
const bytes = Uint8Array.from(atob(padded), (char) => char.charCodeAt(0));
|
|
8
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
9
|
+
}
|