LIVE: a YouTube API that does not load offers Try again

A blocked or slow (over 5 s) IFrame API load reads as offline, reason
`api`, so both LIVE views show "Stream is offline" with Try again, which
loads the API afresh: the loader keeps no failed load. Embedding refused
by the video's owner (errors 101 and 150) stays unavailable. Tests cover
the loader's reuse and its retry after an error or a timeout.
This commit is contained in:
Denis Groshev committed 2026-11-20 13:12:14 +01:00
1 parent 07e89a7024
commit 7b7eb2318a
5 files changed
+94 -7

No files matched your search

+3 -2
View File
@@ -2185,8 +2185,9 @@ section leaving the page removes the player, so nothing plays unseen. ON AIR
with a blinking red dot while it plays; LOADING with an amber dot while it
starts; an offline card ("Stream is offline", with Try again) when
the player reports the video ended, errored or did not start within 25 s,
which is how an off-air broadcast shows; "This stream cannot play here" when
the owner refuses embedding or the API does not load. Stepping to another
which is how an off-air broadcast shows, or when YouTube's player API does not
load (Try again loads it afresh); "This stream cannot play here" when the
owner refuses embedding. Stepping to another
stream hands a playing one over; a selection that moves to an object
without that stream, clearing it or turning the layer off removes the
player, while another entry of the same station (a module, a docked vehicle,
+1
View File
@@ -1666,6 +1666,7 @@
"server/providers/space/issTelemetry.test.mjs",
"src/data/liveStreams.test.mjs",
"src/data/youtubeLivePlayer.test.mjs",
"src/data/youtubeApi.test.mjs",
"src/layers/installations/labelBoxClick.test.mjs",
"src/layers/satellites/cockpitSubject.test.mjs",
"src/layers/satellites/issArticulation.test.mjs",
+84
View File
@@ -0,0 +1,84 @@
// src/data/youtubeApi.test.mjs — YouTube's IFrame API loader, with the page
// faked: one load per document, and a load that fails is not kept, so the
// LIVE section's Try again loads the API afresh.
import test from 'node:test';
import assert from 'node:assert/strict';
import { loadYouTubeApi } from './youtubeApi.js';
/** A document whose head records the scripts put in it. */
function fakePage() {
const scripts = [];
const timers = [];
const documentRef = {
createElement(tag) {
const listeners = {};
return {
tag,
removed: false,
addEventListener(name, listener) {
listeners[name] = listener;
},
fire(name) {
listeners[name]?.();
},
remove() {
this.removed = true;
},
};
},
head: {
append(node) {
scripts.push(node);
},
},
};
const globalRef = {
setTimeout(callback, delay) {
timers.push({ callback, delay, cleared: false });
return timers.length;
},
clearTimeout(id) {
if (timers[id - 1]) timers[id - 1].cleared = true;
},
};
return { documentRef, globalRef, scripts, timers };
}
class Player {}
test('the API loads once per document, and a loaded one is reused', async () => {
const { documentRef, globalRef, scripts } = fakePage();
const first = loadYouTubeApi(documentRef, globalRef);
assert.equal(loadYouTubeApi(documentRef, globalRef), first);
assert.equal(scripts.length, 1);
assert.equal(scripts[0].src, 'https://www.youtube.com/iframe_api');
globalRef.YT = { Player };
globalRef.onYouTubeIframeAPIReady();
assert.equal(await first, globalRef.YT);
assert.equal(await loadYouTubeApi(documentRef, globalRef), globalRef.YT);
assert.equal(scripts.length, 1);
});
test('a script that fails to load is removed, and the next call loads afresh', async () => {
const { documentRef, globalRef, scripts } = fakePage();
const failed = loadYouTubeApi(documentRef, globalRef);
scripts[0].fire('error');
await assert.rejects(failed, /YouTube API unavailable/);
assert.equal(scripts[0].removed, true);
const again = loadYouTubeApi(documentRef, globalRef);
assert.notEqual(again, failed);
assert.equal(scripts.length, 2);
globalRef.YT = { Player };
globalRef.onYouTubeIframeAPIReady();
assert.equal(await again, globalRef.YT);
});
test('a load slower than 5 s fails, and the next call loads afresh', async () => {
const { documentRef, globalRef, scripts, timers } = fakePage();
const slow = loadYouTubeApi(documentRef, globalRef);
assert.equal(timers[0].delay, 5000);
timers[0].callback();
await assert.rejects(slow, /YouTube API timed out/);
loadYouTubeApi(documentRef, globalRef);
assert.equal(scripts.length, 2);
});
+4 -3
View File
@@ -8,8 +8,9 @@ import { YOUTUBE_EMBED_HOST, youtubeLivePlayerVars } from './liveStreams.js';
* never starts. The player cannot say "this broadcast is off air" in so
* many words: a live stream that is off air is reported as a video that
* cannot play, or that never reaches playing, so both read as offline here.
* Embedding refused by the video's owner is `unavailable`, which no retry
* will change.
* An API that does not load (blocked, or slower than its 5 s) is offline too,
* reason `api`: Try again loads it afresh. Embedding refused by the video's
* owner is `unavailable`, which no retry will change.
*/
/** A stream that has not started playing this long after its player was ready is offline. */
@@ -102,7 +103,7 @@ export function createYouTubeLivePlayer({
}
},
() => {
if (!destroyed) set('unavailable', 'api');
if (!destroyed) set('offline', 'api');
},
);
+2 -2
View File
@@ -157,10 +157,10 @@ test('a timer that fires after playback began changes nothing', async () => {
assert.deepEqual(states.at(-1), { state: 'playing', reason: null });
});
test('an API that does not load leaves the stream unavailable', async () => {
test('an API that does not load reads as offline, which Try again can change', async () => {
const { states, settle } = fixture({ apiFails: true });
await settle();
assert.deepEqual(states.at(-1), { state: 'unavailable', reason: 'api' });
assert.deepEqual(states.at(-1), { state: 'offline', reason: 'api' });
});
test('destroy empties the host, destroys the player and silences later events', async () => {