From be68ccb3244a0bb2c0a56193e0ea2d9971cedeff Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Thu, 19 Nov 2026 15:09:13 +0100 Subject: [PATCH] feat: fallback to local output when appropriate --- src/components/configurator/output/element.js | 104 ++++++++++++++++++ tests/components/configurator/output/test.ts | 69 ++++++++++++ 2 files changed, 173 insertions(+) diff --git a/src/components/configurator/output/element.js b/src/components/configurator/output/element.js index ae92bff0..c64ce0ea 100644 --- a/src/components/configurator/output/element.js +++ b/src/components/configurator/output/element.js @@ -23,6 +23,13 @@ import { const STORAGE_PREFIX = "diffuse/configurator/output"; +/** + * How long a newly selected output gets to finish its initial connection + * check before it is treated as disconnected. Outputs that were already + * usable fall back immediately. + */ +const DISCONNECT_GRACE_MS = 5_000; + const COLLECTIONS = /** @type {const} */ ([ "collaborations", "facets", @@ -366,6 +373,77 @@ class OutputConfigurator extends BroadcastableDiffuseElement { this.#carryingOver.add(id); void this.#carryOver(out, def, id); }); + + /** + * Fall back to the default output when the selected one can't read or + * write: a remote output reports `ready: false` when its credentials or + * session are gone, or when the browser is offline. Without this the UI + * keeps presenting a disconnected store as the one in use. + * + * A previously usable output is replaced immediately. On boot the output + * gets a grace period, because its connection check is asynchronous and + * `ready` starts out false. + */ + this.effect(() => { + const out = this.#selected.value; + const online = this.#isOnline.value; + + if (out !== this.#readinessOutput) { + this.#readinessOutput = out; + this.#readiness = undefined; + } + + const ready = out ? (out.ready?.() ?? true) : true; + const becameUnready = this.#readiness === true && !ready; + this.#readiness = ready; + + this.#clearDisconnectTimer(); + + if (!out || out === this.#defaultOutput.value) return; + if (ready || !online) return; + + if (becameUnready) { + void this.#fallBackToDefault(out); + return; + } + + this.#disconnectTimer = setTimeout(() => { + this.#disconnectTimer = undefined; + + const current = this.#selected.value; + if (!current || current === this.#defaultOutput.value) return; + if (current.ready?.() ?? true) return; + if (!this.#isOnline.value) return; + + void this.#fallBackToDefault(current); + }, DISCONNECT_GRACE_MS); + }); + } + + /** + * Stop using a selected output that isn't usable, falling back to the + * default (local) output. Clearing the selection matches a manual + * disconnect, so the choice isn't silently re-applied while the output is + * still unavailable. + * + * @param {Output} out + */ + async #fallBackToDefault(out) { + if (this.#selected.value !== out) return; + if (out.ready?.() ?? true) return; + + console.warn( + `Output "${out.id}" is not ready; falling back to the default output.`, + ); + + await this.deselect(); + } + + #clearDisconnectTimer() { + if (this.#disconnectTimer === undefined) return; + + clearTimeout(this.#disconnectTimer); + this.#disconnectTimer = undefined; } /** @@ -486,6 +564,20 @@ class OutputConfigurator extends BroadcastableDiffuseElement { #setupFinished = signal(false); + #isOnline = signal(navigator.onLine); + + /** @type {ReturnType | undefined} */ + #disconnectTimer; + + /** @type {Output | null | undefined} */ + #readinessOutput; + + /** @type {boolean | undefined} */ + #readiness; + + #handleOnline = () => this.#isOnline.value = true; + #handleOffline = () => this.#isOnline.value = false;; + // STATE activated = this.#activated.get; @@ -514,6 +606,9 @@ class OutputConfigurator extends BroadcastableDiffuseElement { // Super super.connectedCallback(); + globalThis.addEventListener("online", this.#handleOnline); + globalThis.addEventListener("offline", this.#handleOffline); + // Outputs const def_ault = this.getAttribute("default"); const selectedOutputId = this.#selectedOutputId(); @@ -544,6 +639,15 @@ class OutputConfigurator extends BroadcastableDiffuseElement { }); } + /** @override */ + disconnectedCallback() { + globalThis.removeEventListener("online", this.#handleOnline); + globalThis.removeEventListener("offline", this.#handleOffline); + + this.#clearDisconnectTimer(); + super.disconnectedCallback(); + } + // MISC /** diff --git a/tests/components/configurator/output/test.ts b/tests/components/configurator/output/test.ts index 037e6f0e..b3c6f0c4 100644 --- a/tests/components/configurator/output/test.ts +++ b/tests/components/configurator/output/test.ts @@ -142,6 +142,75 @@ describe("components/configurator/output", () => { expect(result).toBe(false); }); + it("falls back to the default output when the selected output disconnects", async () => { + const result = await testWeb(async () => { + const idb = await import( + "~/components/output/polymorphic/indexed-db/element.js" + ); + const { CLASS } = await import( + "~/components/configurator/output/element.js" + ); + const { signal } = await import("~/common/signal.js"); + + const suffix = crypto.randomUUID(); + const ready = signal(true); + + const def = new idb.CLASS(); + def.id = "disconnect-default"; + def.setAttribute("namespace", `disconnect-default-${suffix}`); + + const remote = new idb.CLASS(); + remote.id = "disconnect-remote"; + remote.setAttribute("namespace", `disconnect-remote-${suffix}`); + remote.ready = () => ready.get(); + + const configurator = new CLASS(); + configurator.setAttribute("default", def.id); + configurator.append(def, remote); + document.body.append(configurator); + + // Let the configurator finish its initial (async) setup before changing + // the selection, or its connectedCallback would overwrite it. + await new Promise((resolve) => setTimeout(resolve, 100)); + + const waitFor = async (predicate: () => boolean) => { + const deadline = Date.now() + 3_000; + while (Date.now() < deadline) { + if (predicate()) return true; + await new Promise((resolve) => setTimeout(resolve, 20)); + } + + return predicate(); + }; + + await configurator.select(remote.id); + + const selectedRemote = await waitFor(() => + configurator.selected()?.id === remote.id + ); + + ready.set(false); + + const fellBack = await waitFor(() => + configurator.selected() === null && configurator.hasSelected() === false + ); + + return { + selectedRemote, + fellBack, + selected: configurator.selected()?.id ?? null, + hasSelected: configurator.hasSelected(), + }; + }); + + expect(result).toEqual({ + selectedRemote: true, + fellBack: true, + selected: null, + hasSelected: false, + }); + }); + it("options lists child elements by id and label", async () => { const result = await testWeb(async () => { const mod = await import( -- knotmirror 0.0.1