oficina / estilo Public

busy: take Fiesta's busy.js, which holds only a POST

Paul, 2026-11-10: "We should only show a loader if an action takes more
than 500ms. Probably put that in Estilo." and, narrowing it the same
day: "The busy 650ms should only really be for POST-style requests.
For GET style requests (like updating a view), it should either be
instant if it makes sense, or loading state only if the request takes
more than 500ms."

estilo's copy was rastrillo's ui/busy.js at eda484d, and rastrillo has
since moved busy.js into Fiesta. The new bytes are Fiesta's js/busy.js
at 8083eb92, the unmerged busy-reveal branch: a POST is held for 650ms
as before; a GET goes at once, guarded, and shows the busy state only
after 500ms. They also bring the live-form and pow.js hand-offs and the
beforeunload drop that rastrillo added after eda484d; each is inert
without its attribute.

provenance.json gives busy.js its own source, since the other two
files still come from rastrillo, and says it must be re-pointed at the
merged Fiesta commit before this lands. Prefix is the files' digest, so
the apps get a new URL without a version bump.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 28961a81ef06d88549ce9faf1c83f08fa63522a3 parent 7a19588
4 files changed, +184 −51
  • busy/busy.go +20 −8
  • busy/busy.js +154 −38
  • busy/busy_test.go +3 −1
  • busy/provenance.json +7 −4

CI passed — run details

diff --git a/busy/busy.go b/busy/busy.go
index 0ec2fc4..3421281 100644
--- a/busy/busy.go
+++ b/busy/busy.go
@@ -1,18 +1,30 @@
// Package busy carries rastrillo's busy rule to the Oficina apps (Paul,
-// 2026-09-29): a submit button's label gives way to a spinner for at
-// least 650ms, the button disables, its form refuses a second submit, and
-// Back hands the form back.
+// 2026-09-29): a submit button's label gives way to a spinner, the
+// button disables, its form refuses a second submit, and Back hands the
+// form back.
//
-// The rule is rastrillo's (ui/busy.js). The apps vendor older rastrillo
-// files, and moving every app to a newer rastrillo would carry far more
-// than this, so estilo carries three byte-exact copies instead, each
-// recorded in provenance.json and checked against it:
+// When the spinner shows depends on the method (Paul, 2026-11-10: "The
+// busy 650ms should only really be for POST-style requests. For GET
+// style requests (like updating a view), it should either be instant if
+// it makes sense, or loading state only if the request takes more than
+// 500ms."). A POST's shows at once and for at least 650ms, the submit
+// held until then, because a save should read as a deliberate beat. A
+// GET goes at once, and its button shows nothing unless the page is
+// still there 500ms later: holding a search or a filter made every look
+// slower for a spinner nobody needed. Both refuse a second submit from
+// the click.
+//
+// The rule is rastrillo's, and rastrillo now takes it from Fiesta
+// (js/busy.js). The apps vendor older rastrillo files, and moving every
+// app to a newer rastrillo would carry far more than this, so estilo
+// carries three byte-exact copies instead, each recorded in
+// provenance.json and checked against it:
//
// - ShimJS: rastrillo.js as it is now the busy rule has left it. An app
// replaces its vendored static/rastrillo.js with these bytes — its old
// copy still carries the old busy rule, and two busy rules on one
// page fight over every submit.
-// - JS: busy.js, the rule itself.
+// - JS: busy.js, the rule itself, from Fiesta.
// - CSS: the spinner-in-place block from rastrillo's tokens.css, for a
// tokens.css that predates it. Link it after tokens.css.
//
diff --git a/busy/busy.js b/busy/busy.js
index 550a843..4f07a5a 100644
--- a/busy/busy.js
+++ b/busy/busy.js
@@ -9,11 +9,26 @@
What a submitted button does: its label gives way to a spinner (the
label stays, beside the spinner, under reduced motion or forced
colours — tokens.css), it goes aria-busy and then disabled, and its
- form refuses a second submit. The spinner shows for at least
- HOLD_MS: a response faster than that is held back until then, so a
- quick save reads as a deliberate beat rather than a flicker. Going
- Back to the page hands every busy form back, and a submit still being
- held when the page is left is dropped rather than sent later.
+ form refuses a second submit. When depends on the method, the
+ submitter's formmethod beating the form's:
+
+ POST the spinner shows at once and for at least HOLD_MS: a
+ response faster than that is held back until then, so a
+ quick save reads as a deliberate beat rather than a flicker.
+ A save changes something, and the beat says it happened.
+ GET the submit goes at once, and the button shows nothing
+ unless the page is still here SHOW_AFTER_MS later. A GET
+ only looks something up (a search, a filter, a view):
+ holding it made every look slower for a spinner nobody
+ needed, and one that flashes on its way out is a glitch.
+ The form refuses a second submit from the start.
+
+ Going Back to the page hands every busy form back. A submit still
+ being held when the page is left is dropped rather than sent
+ later, and a GET's spinner still waiting is never shown. Left means
+ a navigation has started, not that it has finished. A form pow.js
+ protects is pow's to hold: this file arms only pow's re-send and
+ never holds it.
Vocabulary:
data-busy="false" on a <form> or on one submit button: opt OUT
@@ -22,6 +37,10 @@
included — changes nothing
data-busy-label="…" on the form or on the button: replacement
button text while it works
+ data-live-form on a <form>: live.js's to send, and to show
+ busy, whenever it claims the submit (marking
+ the event e.liveForm); this file arms it only
+ when live.js lets it go natively
A script that handles a submit itself (a fetch, say) cancels it in a
listener on the form or the document, as it would anyway: those run
@@ -34,10 +53,15 @@
(function () {
"use strict";
- // How long the spinner shows, at least. Long enough to read as an
- // answer to the click, short enough never to feel like waiting.
+ // How long a POST's spinner shows, at least. Long enough to read as
+ // an answer to the click, short enough never to feel like waiting.
var HOLD_MS = 650;
+ // How long a GET may take before its button says it is working.
+ // Under half a second a click and its answer read as one event; past
+ // it, a still button reads as a missed click.
+ var SHOW_AFTER_MS = 500;
+
// ── The busy rule ────────────────────────────────────────────────
//
// A button that CHANGES something says so while it works; a button
@@ -71,6 +95,8 @@
// Every busy element in the DOCUMENT, then the ownership test: a
// form="id" submit button is owned by a form it does not live in.
function busyOff(form) {
+ clearTimeout(reveals.get(form));
+ reveals.delete(form);
form.removeAttribute("aria-busy");
document.querySelectorAll('[aria-busy="true"]').forEach(function (b) {
if (b.form !== form && !form.contains(b)) return;
@@ -85,9 +111,25 @@
});
}
- function busySubmit(e) {
+ function busySubmit(e, late) {
var form = e.target;
if (!form || form.tagName !== "FORM") return;
+ // A swapping form is live.js's whenever live.js claims it: arming it
+ // here too would hand it back a tick after live.js cancels the
+ // submit, clearing live.js's busy state mid-request. Whether this
+ // capture listener runs before live.js's is down to script order, so
+ // it is armed late, from the window listener, and only if live.js
+ // did not claim it (e.liveForm, set in live.js's capture listener)
+ // and nobody cancelled it. One live.js leaves to the browser (a GET,
+ // another origin, a page live.js is not running on) keeps the rule.
+ if (form.hasAttribute("data-live-form") && !late) return;
+ // pow.js owns every submit of a form it protects: it cancels it,
+ // shows its own busy state (this file's vocabulary) while it waits,
+ // and re-sends it marked data-pow-released. Arming here before that
+ // would hand the form back a tick later and wipe pow's feedback, and
+ // a guard armed here would refuse pow's re-send. Arm on the re-send
+ // only.
+ if (form.hasAttribute("data-pow-form") && !form.hasAttribute("data-pow-released")) return;
if (form.getAttribute("data-busy") === "false") return;
// The button the browser submitted with: the one clicked, or — for
// Enter in a field — the default one it implicitly clicked. Only
@@ -109,48 +151,91 @@
// shadowable, and under strict mode assigning to a shadowed one throws.
var to = (btn && btn.getAttribute("formtarget")) || form.getAttribute("target");
if (to && to !== "_self") return;
- if (/^dialog$/i.test(btn && btn.getAttribute("formmethod") ||
- form.getAttribute("method"))) return;
+ var method = btn && btn.getAttribute("formmethod") || form.getAttribute("method");
+ if (/^dialog$/i.test(method)) return;
if (form === releasing) return; // the hold sending the submit it held
if (form.getAttribute("aria-busy") === "true") { e.preventDefault(); return; }
form.setAttribute("aria-busy", "true");
if (!btn || btn.getAttribute("data-busy") === "false") btn = null;
+ // Anything but post is a GET: a missing method, and one HTML does
+ // not know, both submit as one. Its button waits for the reveal, and
+ // with no busy button the hold below leaves its submit alone.
var label = btn && (btn.getAttribute("data-busy-label") ||
form.getAttribute("data-busy-label"));
- if (btn) {
- btn.setAttribute("aria-busy", "true");
- if (btn.tagName === "BUTTON") {
- if (label) {
- btn.setAttribute("data-idle-label", btn.textContent);
- btn.textContent = label;
- }
- // A child element, not a pseudo-element: the shim has to be
- // able to take it away again. An <input type="submit"> has no
- // children, which is the one shape this cannot draw a spinner
- // on. tokens.css stops it rotating under reduced motion.
- var spin = document.createElement("span");
- spin.setAttribute("rst-spin", "");
- spin.setAttribute("aria-hidden", "true");
- btn.insertBefore(spin, btn.firstChild);
- }
+ if (!/^post$/i.test(method || "")) {
+ if (btn) reveals.set(form, setTimeout(function () { reveal(form, btn, label); }, SHOW_AFTER_MS));
+ setTimeout(function () {
+ // Cancelled downstream, as below: hand the form back, and
+ // cancel the reveal with it.
+ if (e.defaultPrevented) busyOff(form);
+ }, 0);
+ return;
}
+ if (btn) dress(btn, label);
setTimeout(function () {
// Someone downstream cancelled the submit — an app handler doing
// the work itself, most likely. Nothing is on its way anywhere,
// so hand the form back, guard included, and let whoever took the
// job own the feedback too.
if (e.defaultPrevented && !held.has(e)) { busyOff(form); return; }
- if (!btn) return;
- if (label && btn.tagName === "INPUT") {
- btn.setAttribute("data-idle-label", btn.value);
- btn.value = label;
- }
- btn.disabled = true;
+ if (btn) harden(btn, label);
}, 0);
}
+ // The half of the busy state that cannot touch the payload. The
+ // spinner is a child element, not a pseudo-element, so busyOff can
+ // take it away again. An <input type="submit"> has no children,
+ // which is the one shape this cannot draw a spinner on. tokens.css
+ // stops it rotating under reduced motion.
+ function dress(btn, label) {
+ btn.setAttribute("aria-busy", "true");
+ if (btn.tagName !== "BUTTON") return;
+ if (label) {
+ btn.setAttribute("data-idle-label", btn.textContent);
+ btn.textContent = label;
+ }
+ var spin = document.createElement("span");
+ spin.setAttribute("rst-spin", "");
+ spin.setAttribute("aria-hidden", "true");
+ btn.insertBefore(spin, btn.firstChild);
+ }
+
+ // The half that can: a disabled button drops its name and value, and
+ // an <input type="submit">'s value is what it submits.
+ function harden(btn, label) {
+ if (label && btn.tagName === "INPUT") {
+ btn.setAttribute("data-idle-label", btn.value);
+ btn.value = label;
+ }
+ btn.disabled = true;
+ }
+
document.addEventListener("submit", busySubmit, true);
+ // ── The reveal ───────────────────────────────────────────────────
+ //
+ // A GET's page is still here SHOW_AFTER_MS after the click, so the
+ // answer is slow, and the button says so now: the busy state a POST
+ // shows at once. Nothing here can change the payload any more; the
+ // entry list was built long ago. A button that has left its form
+ // meanwhile (a polled fragment replacing it, a form= changed) is not
+ // the one working, and busyOff would never find it to hand it back.
+ var reveals = new Map(); // form -> the timer that will reveal its button
+ function reveal(form, btn, label) {
+ reveals.delete(form);
+ if (form.getAttribute("aria-busy") !== "true") return;
+ if (!btn.isConnected || btn.form !== form) return;
+ dress(btn, label);
+ harden(btn, label);
+ }
+
+ // Every reveal still waiting is dropped. Not on beforeunload: a GET's
+ // own navigation fires it, and a slow one is exactly when to show.
+ function dropReveals() {
+ reveals.forEach(function (timer) { clearTimeout(timer); });
+ reveals.clear();
+ }
+
// ── The hold ─────────────────────────────────────────────────────
//
// A spinner can only be promised a minimum time by holding the
@@ -174,6 +259,16 @@
var form = e.target;
if (!form || form.tagName !== "FORM") return;
if (form === releasing) { releasing = null; return; }
+ if (form.hasAttribute("data-live-form")) {
+ if (e.defaultPrevented || e.liveForm) return;
+ busySubmit(e, true);
+ }
+ // Never hold a pow form: pow has already held it while it solved,
+ // and a second hold is a second wait for nothing. Worse, holding it
+ // here and re-sending later would send it back through pow, which
+ // would hold it again with busy.js's guard still armed, and the form
+ // could never submit.
+ if (form.hasAttribute("data-pow-form")) return;
if (e.defaultPrevented || form.getAttribute("aria-busy") !== "true") return;
// Only a submit with a spinner to show is held: the hold is for the
// spinner. A script's requestSubmit() with no submitter, or a button
@@ -184,6 +279,13 @@
e.preventDefault();
held.add(e);
pending.set(form, setTimeout(function () { release(form, btn); }, HOLD_MS));
+ // pagehide fires only once a navigation commits, and a slow
+ // destination leaves the hold running until then: a held submit
+ // released meanwhile replaces the navigation the visitor chose.
+ // beforeunload fires as the navigation starts. Attached only while
+ // something is held, so it keeps no page out of the back-forward
+ // cache.
+ if (pending.size === 1) window.addEventListener("beforeunload", dropHeld);
});
// Send the held submit. The submitter goes with it, so its name and
@@ -195,6 +297,7 @@
// must be handed back with its errors showing, not left spinning.
function release(form, btn) {
pending.delete(form);
+ if (pending.size === 0) window.removeEventListener("beforeunload", dropHeld);
if (!form.isConnected) return;
var skip = form.hasAttribute("novalidate") ||
(btn && btn.hasAttribute("formnovalidate"));
@@ -234,19 +337,32 @@
}
}
+ // A navigation started while a submit was held: drop every held
+ // submit and hand the forms back, so a navigation that is then
+ // cancelled (a download, a beforeunload prompt) leaves no form
+ // spinning.
+ function dropHeld() {
+ pending.forEach(function (timer, form) {
+ clearTimeout(timer);
+ busyOff(form);
+ });
+ pending.clear();
+ window.removeEventListener("beforeunload", dropHeld);
+ }
+
// Leaving the page — a link, Back — while a submit is held drops it:
// the visitor has gone somewhere else, and a timer surviving into the
- // back/forward cache would otherwise send it on their return.
- window.addEventListener("pagehide", function () {
- pending.forEach(function (timer) { clearTimeout(timer); });
- pending.clear();
- });
+ // back/forward cache would otherwise send it on their return, or
+ // dress a form they walked away from as busy.
+ window.addEventListener("pagehide", function () { dropHeld(); dropReveals(); });
// The back/forward cache restores a page's DOM exactly as it was left
// — busy buttons still disabled, still wearing the busy label and the
// spinner — so a visitor who navigates back finds a dead form. Hand
// every busy form back.
window.addEventListener("pageshow", function (e) {
- if (e.persisted) document.querySelectorAll("form[aria-busy]").forEach(busyOff);
+ if (!e.persisted) return;
+ document.querySelectorAll("form[aria-busy]").forEach(busyOff);
+ dropReveals();
});
})();
diff --git a/busy/busy_test.go b/busy/busy_test.go
index 461afe2..dac0331 100644
--- a/busy/busy_test.go
+++ b/busy/busy_test.go
@@ -42,7 +42,9 @@ func TestTheShimLeftTheRuleToBusyJS(t *testing.T) {
if strings.Contains(string(ShimJS()), "busySubmit") || strings.Contains(string(ShimJS()), `"submit"`) {
t.Error("the shim still carries a busy rule; two on one page fight over every submit")
}
- for _, want := range []string{"HOLD_MS = 650", "busySubmit", "pageshow", "pagehide"} {
+ // Both timings: a POST's 650ms hold and a GET's 500ms wait before it
+ // shows anything. A copy with only the first holds every search.
+ for _, want := range []string{"HOLD_MS = 650", "SHOW_AFTER_MS = 500", "busySubmit", "pageshow", "pagehide"} {
if !strings.Contains(string(JS()), want) {
t.Errorf("busy.js lacks %q", want)
}
diff --git a/busy/provenance.json b/busy/provenance.json
index 9bfdbe7..0d47871 100644
--- a/busy/provenance.json
+++ b/busy/provenance.json
@@ -1,7 +1,7 @@
{
"repository": "https://amadan.net/rastrillo/rastrillo",
"commit": "eda484d4822c5a954b68411375075552c9aeda3e",
- "note": "rastrillo main, after busy-hold-needs-a-button (discussion 51)",
+ "note": "rastrillo.js and busy.css: rastrillo main, after busy-hold-needs-a-button (discussion 51). busy.js has its own source below.",
"files": {
"rastrillo.js": {
"from": "ui/rastrillo.js",
@@ -9,9 +9,12 @@
"bytes": 9784
},
"busy.js": {
- "from": "ui/busy.js",
- "sha256": "b5be5c1c7239b066ec5d13e0c9a21a06c7c7a448542dc7d664a989f4589bae02",
- "bytes": 12521
+ "repository": "https://amadan.net/rastrillo/fiesta",
+ "commit": "8083eb92170087b2cd59b3527d5cf273a2d6f8fa",
+ "note": "js/busy.js on fiesta's unmerged busy-reveal branch: the GET/POST split (Paul, 2026-11-10). Re-point this at the merged Fiesta commit or release before estilo's busy-reveal lands. busy.js moved from rastrillo's ui/ to Fiesta in rastrillo 6b19df56.",
+ "from": "js/busy.js",
+ "sha256": "b97dbe70effa06551d68a09e23cc81921f0076bb35c8d8590beb436938cdf1ff",
+ "bytes": 18440
},
"busy.css": {
"from": "ui/tokens.css (the block beginning \"With no busy label there are no words to show\", lines 333-374, after a four-line header)",