chrome.js: Appearance aborts the save before it and lands transitions
Fable's P3.6, P3.7 and the first P3.8 mutant on estilo #34. - Rapid clicks sent unordered POSTs, so the first choice could be saved last. Each switch now aborts the save before it (AbortController); the saved choice is the response's cookie, so an aborted save leaves none. The aborted fetch fails quietly, like any failed save. - A switch did not land running transitions. It now does what rastrillo's shim does for its own scheme menu (land() in busy/rastrillo.js): after data-theme changes, every running transition is cancelled, which puts it on its end value; animations (a spinner) are left alone. - Removing redirect: 'manual' went unnoticed; the test now asserts it. Each new assertion was seen red against the old script, and four mutants (no redirect: 'manual', no abort, no land(), land() cancelling animations too) each fail the suite. In Chromium and WebKit, against a local server answering 303 after 300ms, light, system, dark clicked in a row leave data-theme="dark" with no navigation and no page error.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+79
−7
chrome/chrome.js+26 −7chrome/chrome.test.mjs+53 −0
diff --git a/chrome/chrome.js b/chrome/chrome.js| index d820315..9b39c8c 100644 |
| --- a/chrome/chrome.js |
| +++ b/chrome/chrome.js |
| @@ -143,12 +143,28 @@ |
| // Appearance switches at once (Paul: "They're not doing an action ... |
| // they should just switch immediately"). The page takes the scheme as |
| // chrome.ThemeAttr writes it, data-theme="light" or "dark" on <html> and |
| - // no attribute for System; the pressed segment moves; and the same |
| - // form data is POSTed to the form's action in the background to save |
| - // it. The form is data-busy="false", so the busy rule holds nothing. If |
| - // the POST fails the page stays switched for this visit and says |
| - // nothing; the next load shows the saved choice (spec, P3). Capture |
| - // phase, like the rest: an app's own submit handler cannot swallow it. |
| + // no attribute for System; whatever was mid-transition lands in it at |
| + // once; the pressed segment moves; and the same form data is POSTed to |
| + // the form's action in the background to save it, without following |
| + // the server's redirect back to the page. The form is |
| + // data-busy="false", so the busy rule holds nothing. If the POST fails |
| + // the page stays switched for this visit and says nothing; the next load |
| + // shows the saved choice (spec, P3). Each switch aborts the save before |
| + // it, so two quick clicks cannot reach the server in the wrong order |
| + // and leave the first choice saved. Capture phase, like the rest: an |
| + // app's own submit handler cannot swallow it. |
| + // |
| + // land() is rastrillo's shim's, for its own scheme menu: a change of |
| + // scheme would otherwise retarget running colour transitions while the |
| + // text changes at once. getAnimations() flushes style, which starts the |
| + // transitions the change causes, and cancelling one lands it on its end |
| + // value. Transitions only, told by transitionProperty: cancelling an |
| + // animation would stop a spinner for good. |
| + const land = () => { |
| + if (!document.getAnimations) return |
| + document.getAnimations().forEach((a) => { if (typeof a.transitionProperty === 'string') a.cancel() }) |
| + } |
| + let saving = null |
| document.addEventListener('submit', (e) => { |
| const form = e.target |
| const button = e.submitter |
| @@ -160,11 +176,14 @@ |
| const root = document.documentElement |
| if (mode === 'system') root.removeAttribute('data-theme') |
| else root.setAttribute('data-theme', mode) |
| + land() |
| Array.prototype.forEach.call(form.querySelectorAll('button[name="mode"]'), (b) => { |
| b.setAttribute('aria-pressed', b === button ? 'true' : 'false') |
| }) |
| const body = new URLSearchParams(new FormData(form)) |
| body.set('mode', mode) |
| - fetch(form.getAttribute('action'), { method: 'POST', body, credentials: 'same-origin', redirect: 'manual' }).catch(() => {}) |
| + if (saving) saving.abort() |
| + saving = typeof AbortController === 'function' ? new AbortController() : null |
| + fetch(form.getAttribute('action'), { method: 'POST', body, credentials: 'same-origin', redirect: 'manual', signal: saving ? saving.signal : undefined }).catch(() => {}) |
| }, true) |
| })() |
diff --git a/chrome/chrome.test.mjs b/chrome/chrome.test.mjs| index 07c5186..983c76d 100644 |
| --- a/chrome/chrome.test.mjs |
| +++ b/chrome/chrome.test.mjs |
| @@ -64,6 +64,7 @@ function load(root, globals = {}) { |
| activeElement: null, |
| documentElement: Object.assign(new El("html"), { style: fakeStyle() }), |
| fonts: globals.fonts, |
| + getAnimations: globals.getAnimations, |
| querySelector: (s) => root.querySelector(s), |
| querySelectorAll: (s) => root.querySelectorAll(s), |
| getElementById: () => null, |
| @@ -79,6 +80,7 @@ function load(root, globals = {}) { |
| FormData: globals.FormData ?? FakeFormData, |
| URLSearchParams, |
| ResizeObserver: globals.ResizeObserver, |
| + AbortController, |
| }); |
| const dispatch = (type, target, init = {}) => { |
| const e = { type, target, defaultPrevented: false, preventDefault() { this.defaultPrevented = true; }, ...init }; |
| @@ -227,6 +229,9 @@ test("Appearance switches in place and saves in the background", () => { |
| assert.equal(sent[0].url, "/appearance"); |
| assert.equal(sent[0].init.method, "POST"); |
| assert.equal(sent[0].init.credentials, "same-origin"); |
| + // Not followed: the server's answer is a 303 back to the page, and a |
| + // followed redirect would fetch the whole page again for nothing. |
| + assert.equal(sent[0].init.redirect, "manual"); |
| assert.equal(String(sent[0].init.body), "return_to=%2Fd%2Fabc&mode=light", "the same form data the form would post"); |
| // System is no attribute at all, as chrome.ThemeAttr writes it. |
| @@ -236,6 +241,54 @@ test("Appearance switches in place and saves in the background", () => { |
| assert.equal(String(sent[1].init.body), "return_to=%2Fd%2Fabc&mode=system"); |
| }); |
| +// Two quick clicks are two POSTs with no order between them, so the |
| +// server could save the first choice last. Each switch aborts the save |
| +// before it; the aborted one fails quietly, like any failed save. |
| +test("a second switch aborts the first save, quietly", async () => { |
| + const { form, buttons } = appearance(); |
| + const sent = []; |
| + const fetch = (url, init) => { |
| + sent.push(init); |
| + return new Promise((resolve, reject) => { |
| + if (init.signal) init.signal.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError"))); |
| + }); |
| + }; |
| + const dispatch = load(new El("", {}, [form]), { fetch }); |
| + let unhandled = null; |
| + const onUnhandled = (r) => { unhandled = r; }; |
| + process.on("unhandledRejection", onUnhandled); |
| + dispatch("submit", form, { submitter: buttons[0] }); |
| + dispatch("submit", form, { submitter: buttons[1] }); |
| + await new Promise((r) => setTimeout(r, 10)); |
| + process.off("unhandledRejection", onUnhandled); |
| + assert.equal(sent.length, 2); |
| + assert.ok(sent[0].signal, "the save can be aborted"); |
| + assert.equal(sent[0].signal.aborted, true, "the first save is aborted by the second"); |
| + assert.equal(sent[1].signal.aborted, false, "the last save stands"); |
| + assert.equal(String(sent[1].body), "return_to=%2Fd%2Fabc&mode=dark"); |
| + assert.equal(unhandled, null, "the aborted save surfaced"); |
| + assert.equal(doc.documentElement.getAttribute("data-theme"), "dark"); |
| +}); |
| + |
| +// A switch lands everything mid-transition in the new scheme at once, as |
| +// rastrillo's shim does for its own scheme menu (land()): transitions are |
| +// cancelled, which puts each on its end value, after the attribute has |
| +// changed (getAnimations() flushes style, starting the new ones); an |
| +// animation, a spinner, is left running. |
| +test("a switch lands running transitions and leaves animations alone", () => { |
| + const { form, buttons } = appearance(); |
| + const cancelled = []; |
| + let themeAtCall; |
| + const transition = { transitionProperty: "background-color", cancel() { cancelled.push("transition"); } }; |
| + const spinner = { animationName: "spin", cancel() { cancelled.push("spinner"); } }; |
| + const dispatch = load(new El("", {}, [form]), { |
| + getAnimations: () => { themeAtCall = doc.documentElement.getAttribute("data-theme"); return [transition, spinner]; }, |
| + }); |
| + dispatch("submit", form, { submitter: buttons[1] }); |
| + assert.equal(themeAtCall, "dark", "the transitions were read before the scheme changed"); |
| + assert.deepEqual(cancelled, ["transition"]); |
| +}); |
| + |
| test("a failed save leaves the page switched and says nothing", async () => { |
| const { form, buttons } = appearance(); |
| const dispatch = load(new El("", {}, [form]), { fetch: () => Promise.reject(new TypeError("offline")) }); |