diff --git a/specs/components/metadata/types.d.ts b/specs/components/metadata/types.d.ts index ed6d3b45..a5689843 100644 --- a/specs/components/metadata/types.d.ts +++ b/specs/components/metadata/types.d.ts @@ -4,6 +4,12 @@ import type { Track } from "~/definitions/types.d.ts"; export type Actions = { patch(track: Track): Promise; + + /** + * Best-effort cancellation of an in-flight `patch` for a track (e.g. when + * the track stops playing). Providers that can't cancel simply omit it. + */ + abort?(trackId: string): void; }; export type MetadataElement = DiffuseElement & ProxiedActions; diff --git a/src/common/element.js b/src/common/element.js index c54179a6..3f1660a6 100644 --- a/src/common/element.js +++ b/src/common/element.js @@ -275,6 +275,9 @@ export class BroadcastableDiffuseElement extends DiffuseElement { /** @type {{ assumeLeadership?: boolean }} */ #broadcastingOptions = {}; + /** Whether the leadership election has already been started for this element. */ + #leadershipRequested = false; + #broadcastingStatus; broadcastingStatus; @@ -426,6 +429,13 @@ export class BroadcastableDiffuseElement extends DiffuseElement { if (!this.broadcasted) return; + // A reconnect (e.g. lit-html moving an element to another position in a + // keyed list) must not run the election again: the lock is still held — or + // the request to wait for it is still queued — from the first connection. + // Re-requesting it would steal the lock from ourselves on every move. + if (this.#leadershipRequested) return; + this.#leadershipRequested = true; + // Grab a lock if it isn't acquired yet and if needed, // and hold it until `this.lock.promise` resolves. const assumeLeadership = this.#broadcastingOptions?.assumeLeadership; diff --git a/src/components/engine/audio/element.js b/src/components/engine/audio/element.js index 49cc9690..a1e2cd3d 100644 --- a/src/components/engine/audio/element.js +++ b/src/components/engine/audio/element.js @@ -1,4 +1,4 @@ -import { keyed } from "lit-html/directives/keyed.js"; +import { repeat } from "lit-html/directives/repeat.js"; import { BroadcastableDiffuseElement, @@ -25,6 +25,19 @@ import { computed, signal, untracked } from "~/common/signal.js"; const IS_IOS = /iPhone|iPad|iPod/.test(navigator.userAgent) || (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1); +/** + * A valid, silent WAV. Assigned to a media element that is being torn down to + * force resource selection away from its network source. + * + * Removing `src` and calling `load()` is not enough everywhere: Firefox keeps + * an in-flight media request (and its socket) alive until the element is + * garbage collected, which may never happen while the tab lives. Selecting a + * local data URL makes the element drop the network resource immediately. + * (Also documented in `specs/components/engine/audio/SPEC.md`.) + */ +const SILENT_AUDIO_SRC = + "data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAIA+AAACABAAZGF0YQAAAAA="; + /** * Module-internal Web Audio routing hooks. Accessed only by {@link AudioEngine} * and {@link AudioEngineItem} (both live in this file), so these stay out of @@ -350,10 +363,7 @@ class AudioEngine extends BroadcastableDiffuseElement { } catch { return; } - audio.pause(); - audio.removeAttribute("src"); - item.querySelectorAll("source").forEach((s) => s.removeAttribute("src")); - audio.load(); + releaseAudioElement(audio); }); // NOTE: The shared Web Audio graph is intentionally left untouched here. @@ -524,7 +534,8 @@ class AudioEngine extends BroadcastableDiffuseElement { * @type {Actions["supply"]} */ supply(args) { - const existingMap = new Map(this.#items.value.map((a) => [a.id, a])); + const previous = this.#items.value; + const existingMap = new Map(previous.map((a) => [a.id, a])); // Start loading new streams for (const item of args.audio) { @@ -585,20 +596,32 @@ class AudioEngine extends BroadcastableDiffuseElement { }); const hasNewIds = resolvedAudio.some((a) => !existingMap.has(a.id)); - const hasPreloadChanges = resolvedAudio.some( - (a) => existingMap.get(a.id)?.isPreload !== a.isPreload, - ); const hasUrlChanges = resolvedAudio.some( (a) => existingMap.get(a.id)?.url !== a.url, ); - if (hasNewIds || hasPreloadChanges || hasUrlChanges) { + // Recompose the item list whenever it differs from what is rendered, + // including *removals* and reordering. Comparing only additions/preload/url + // changes left dropped items (e.g. a preload whose track is no longer next) + // rendered forever — with its