Performance Seiteneditor #29
No files matched your search
+5
-3
@@ -1,4 +1,3 @@
|
||||
import Hls from 'hls.js';
|
||||
import './site-nav.js';
|
||||
import './card-clip.js';
|
||||
import './card-heights.js';
|
||||
@@ -8,7 +7,10 @@ import './inquiry-form.js';
|
||||
import './inquiry-widget.js';
|
||||
import './member-nav.js';
|
||||
import { canFullscreen, enterFullscreen } from './player/fullscreen.js';
|
||||
import { attachSource } from './player/source.js';
|
||||
import { bindLazySource, createHlsLoader } from './player/lazy.js';
|
||||
|
||||
// hls.js wird erst bei Bedarf geladen (eigener Teil im Build) und der Film erst beim ersten Abspielen gebunden.
|
||||
const hlsLoader = createHlsLoader();
|
||||
|
||||
function attachPlayback(video) {
|
||||
if (!(video instanceof HTMLVideoElement) || video.dataset.playbackReady === '1') {
|
||||
@@ -22,7 +24,7 @@ function attachPlayback(video) {
|
||||
}
|
||||
|
||||
video.dataset.playbackReady = '1';
|
||||
attachSource(video, { Hls, source, renewUrl: video.dataset.hlsRenew || null });
|
||||
bindLazySource(video, { source, renewUrl: video.dataset.hlsRenew || null, loader: hlsLoader });
|
||||
}
|
||||
|
||||
function scanPlayback(root) {
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Film erst beim Abspielen anbinden (Stufe 0.6).
|
||||
*
|
||||
* Zwei Auslöser mit verschiedener Wirkung:
|
||||
* 1. Sichtbar (oder erste Berührung): nur das Skript von hls.js wird geladen. Das ist ein statisches Skript ohne
|
||||
* Wirkung auf den Server, es sorgt nur dafür, dass der Start unten ohne Wartezeit abläuft.
|
||||
* 2. Erste Bedienung (Klick, Leertaste, Eingabe): Erst jetzt wird die Quelle gebunden (attachSource) und abgespielt.
|
||||
* Das Binden holt die Hauptliste und damit den Nachweis der Auslieferung. Eine Seite mit mehreren Filmen holt
|
||||
* also nichts für Filme, die niemand abspielt.
|
||||
*
|
||||
* Warum der Klick abgefangen wird: Ein <video controls> ohne Quelle löst in den nativen Bedienelementen kein
|
||||
* "play" aus (in Edge geprüft). Der erste Klick wird deshalb im Capture-Schritt aufgefangen, die Quelle gebunden
|
||||
* und mit demselben Klick abgespielt. Das Binden setzt die Quelle des Elements neu und setzt damit "paused" zurück,
|
||||
* darum folgt dem Binden ein eigener play()-Aufruf.
|
||||
*
|
||||
* Reihenfolge der Auslöser: Das Modul soll beim Klick schon geladen sein, damit play() noch in der Geste läuft
|
||||
* (iOS verlangt das). Ist es das nicht, wird auf das Modul gewartet und danach abgespielt; dann kann play()
|
||||
* abgelehnt werden, und ein zweiter Klick spielt ab (die Bedienelemente sind dann nativ wieder wirksam).
|
||||
*/
|
||||
import { attachSource } from './source.js';
|
||||
|
||||
const START_KEYS = new Set([' ', 'Enter', 'k', 'K']);
|
||||
|
||||
/**
|
||||
* Lädt hls.js höchstens einmal. `importer` ist austauschbar, damit die Tests ohne Browser laufen.
|
||||
*/
|
||||
export function createHlsLoader(importer = () => import('hls.js')) {
|
||||
let promise = null;
|
||||
let resolved = null;
|
||||
|
||||
return {
|
||||
/** Das Modul, falls schon geladen, sonst null (für den synchronen Start innerhalb der Geste). */
|
||||
get ready() {
|
||||
return resolved;
|
||||
},
|
||||
load() {
|
||||
promise ??= importer()
|
||||
.then((module) => {
|
||||
resolved = module.default ?? module;
|
||||
|
||||
return resolved;
|
||||
})
|
||||
.catch((error) => {
|
||||
promise = null;
|
||||
throw error;
|
||||
});
|
||||
|
||||
return promise;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function bindLazySource(video, {
|
||||
source,
|
||||
renewUrl = null,
|
||||
loader,
|
||||
attach = attachSource,
|
||||
observe = defaultObserve,
|
||||
}) {
|
||||
let state = 'idle'; // idle | starting | started
|
||||
|
||||
const unbind = () => {
|
||||
video.removeEventListener('click', onClick, true);
|
||||
video.removeEventListener('keydown', onKey, true);
|
||||
video.removeEventListener('pointerenter', prepare);
|
||||
video.removeEventListener('focus', prepare);
|
||||
stopObserving();
|
||||
};
|
||||
|
||||
const finish = (Hls) => {
|
||||
// Das Element kann während des Ladens entfernt worden sein (Seitenwechsel, Livewire): dann nichts binden.
|
||||
if (video.isConnected === false) {
|
||||
state = 'idle';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
attach(video, { Hls, source, renewUrl });
|
||||
state = 'started';
|
||||
unbind();
|
||||
Promise.resolve(video.play?.()).catch(() => undefined);
|
||||
};
|
||||
|
||||
const start = () => {
|
||||
if (state !== 'idle') {
|
||||
return;
|
||||
}
|
||||
|
||||
state = 'starting';
|
||||
|
||||
if (loader.ready) {
|
||||
finish(loader.ready);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
loader.load().then(finish).catch(() => {
|
||||
// Skript nicht ladbar (Netz): zurück in den Anfangszustand, der nächste Klick versucht es erneut.
|
||||
state = 'idle';
|
||||
});
|
||||
};
|
||||
|
||||
function prepare() {
|
||||
loader.load().catch(() => undefined);
|
||||
}
|
||||
|
||||
function swallow(event) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
function onClick(event) {
|
||||
swallow(event);
|
||||
start();
|
||||
}
|
||||
|
||||
function onKey(event) {
|
||||
if (!START_KEYS.has(event.key) || event.ctrlKey || event.metaKey || event.altKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
swallow(event);
|
||||
start();
|
||||
}
|
||||
|
||||
// Das Element selbst muss die Ereignisse im Capture-Schritt bekommen, bevor die nativen Bedienelemente sie sehen.
|
||||
video.addEventListener('click', onClick, true);
|
||||
video.addEventListener('keydown', onKey, true);
|
||||
|
||||
const stopObserving = observe(video, prepare) ?? (() => undefined);
|
||||
|
||||
return { start, prepare, get state() { return state; } };
|
||||
}
|
||||
|
||||
function defaultObserve(video, onVisible) {
|
||||
if (typeof IntersectionObserver === 'undefined') {
|
||||
// Ohne IntersectionObserver: bei Zeiger oder Fokus vorladen, spätestens der Klick lädt nach.
|
||||
video.addEventListener('pointerenter', onVisible, { once: true });
|
||||
video.addEventListener('focus', onVisible, { once: true });
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
const observer = new IntersectionObserver((entries) => {
|
||||
if (entries.some((entry) => entry.isIntersecting)) {
|
||||
observer.disconnect();
|
||||
onVisible();
|
||||
}
|
||||
}, { rootMargin: '300px' });
|
||||
|
||||
observer.observe(video);
|
||||
|
||||
return () => observer.disconnect();
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { bindLazySource, createHlsLoader } from './lazy.js';
|
||||
|
||||
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
||||
|
||||
function fakeVideo({ play = () => Promise.resolve() } = {}) {
|
||||
const listeners = {};
|
||||
|
||||
return {
|
||||
isConnected: true,
|
||||
play: vi.fn(play),
|
||||
listeners,
|
||||
addEventListener(name, handler, capture) {
|
||||
(listeners[name] ??= []).push({ handler, capture });
|
||||
},
|
||||
removeEventListener(name, handler) {
|
||||
listeners[name] = (listeners[name] ?? []).filter((entry) => entry.handler !== handler);
|
||||
},
|
||||
fire(name, event = {}) {
|
||||
const full = { preventDefault: vi.fn(), stopPropagation: vi.fn(), ...event };
|
||||
(listeners[name] ?? []).forEach((entry) => entry.handler(full));
|
||||
|
||||
return full;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function fakeLoader({ fail = false } = {}) {
|
||||
const importer = vi.fn(() => (fail ? Promise.reject(new Error('Netz')) : Promise.resolve({ default: { name: 'Hls' } })));
|
||||
|
||||
return { importer, loader: createHlsLoader(importer) };
|
||||
}
|
||||
|
||||
function setup({ video = fakeVideo(), loader, observer = null, attach = vi.fn() } = {}) {
|
||||
const hold = loader ?? fakeLoader().loader;
|
||||
const observe = vi.fn((target, onVisible) => {
|
||||
observer = { target, onVisible };
|
||||
|
||||
return vi.fn();
|
||||
});
|
||||
const controller = bindLazySource(video, { source: 'https://luf.test/m.m3u8', renewUrl: 'https://luf.test/e', loader: hold, attach, observe });
|
||||
|
||||
return { video, controller, attach, loader: hold, get observer() { return observer; } };
|
||||
}
|
||||
|
||||
describe('createHlsLoader', () => {
|
||||
it('lädt das Modul höchstens einmal und gibt den Standardexport heraus', async () => {
|
||||
const { importer, loader } = fakeLoader();
|
||||
|
||||
expect(loader.ready).toBeNull();
|
||||
const [a, b] = await Promise.all([loader.load(), loader.load()]);
|
||||
|
||||
expect(a).toBe(b);
|
||||
expect(a).toEqual({ name: 'Hls' });
|
||||
expect(importer).toHaveBeenCalledTimes(1);
|
||||
expect(loader.ready).toBe(a);
|
||||
});
|
||||
|
||||
it('versucht es nach einem Fehler beim nächsten Aufruf erneut', async () => {
|
||||
const importer = vi.fn()
|
||||
.mockRejectedValueOnce(new Error('Netz'))
|
||||
.mockResolvedValueOnce({ default: { name: 'Hls' } });
|
||||
const loader = createHlsLoader(importer);
|
||||
|
||||
await expect(loader.load()).rejects.toThrow('Netz');
|
||||
expect(loader.ready).toBeNull();
|
||||
await expect(loader.load()).resolves.toEqual({ name: 'Hls' });
|
||||
expect(importer).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('bindLazySource', () => {
|
||||
it('bindet nichts und lädt nichts, solange niemand das Element sieht oder bedient', () => {
|
||||
const { attach, loader } = setup();
|
||||
|
||||
expect(attach).not.toHaveBeenCalled();
|
||||
expect(loader.ready).toBeNull();
|
||||
});
|
||||
|
||||
it('lädt bei Sichtbarkeit nur das Skript, bindet aber keine Quelle', async () => {
|
||||
const state = setup();
|
||||
|
||||
state.observer.onVisible();
|
||||
await flush();
|
||||
|
||||
expect(state.loader.ready).toEqual({ name: 'Hls' });
|
||||
expect(state.attach).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('bindet beim ersten Klick sofort und spielt ab, wenn das Skript schon geladen ist', async () => {
|
||||
const state = setup();
|
||||
state.observer.onVisible();
|
||||
await flush();
|
||||
|
||||
const click = state.video.fire('click');
|
||||
|
||||
expect(state.attach).toHaveBeenCalledTimes(1);
|
||||
expect(state.attach).toHaveBeenCalledWith(state.video, {
|
||||
Hls: { name: 'Hls' },
|
||||
source: 'https://luf.test/m.m3u8',
|
||||
renewUrl: 'https://luf.test/e',
|
||||
});
|
||||
expect(state.video.play).toHaveBeenCalledTimes(1);
|
||||
// Der Klick gehört dem Starten; die nativen Bedienelemente ohne Quelle sollen ihn nicht auch noch sehen.
|
||||
expect(click.preventDefault).toHaveBeenCalled();
|
||||
expect(click.stopPropagation).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('spielt erst nach dem Binden ab', async () => {
|
||||
const order = [];
|
||||
const video = fakeVideo({ play: () => { order.push('play'); return Promise.resolve(); } });
|
||||
const state = setup({ video, attach: vi.fn(() => order.push('attach')) });
|
||||
state.observer.onVisible();
|
||||
await flush();
|
||||
|
||||
video.fire('click');
|
||||
|
||||
expect(order).toEqual(['attach', 'play']);
|
||||
});
|
||||
|
||||
it('wartet bei einem Klick vor dem Laden des Skripts darauf und spielt danach ab', async () => {
|
||||
const state = setup();
|
||||
|
||||
state.video.fire('click');
|
||||
expect(state.attach).not.toHaveBeenCalled();
|
||||
await flush();
|
||||
|
||||
expect(state.attach).toHaveBeenCalledTimes(1);
|
||||
expect(state.video.play).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('bindet bei mehreren Klicks nur einmal', async () => {
|
||||
const state = setup();
|
||||
|
||||
state.video.fire('click');
|
||||
state.video.fire('click');
|
||||
await flush();
|
||||
state.video.fire('click');
|
||||
|
||||
expect(state.attach).toHaveBeenCalledTimes(1);
|
||||
expect(state.loader.ready).not.toBeNull();
|
||||
});
|
||||
|
||||
it('gibt nach dem Start die Ereignisse an die nativen Bedienelemente frei', async () => {
|
||||
const state = setup();
|
||||
state.observer.onVisible();
|
||||
await flush();
|
||||
state.video.fire('click');
|
||||
|
||||
const later = state.video.fire('click');
|
||||
|
||||
expect(later.preventDefault).not.toHaveBeenCalled();
|
||||
expect(state.video.listeners.click).toEqual([]);
|
||||
expect(state.video.listeners.keydown).toEqual([]);
|
||||
});
|
||||
|
||||
it('startet mit Leertaste und Eingabe, lässt andere Tasten und Tastenkombinationen aber durch', async () => {
|
||||
const space = setup();
|
||||
const other = setup();
|
||||
const combo = setup();
|
||||
space.observer.onVisible();
|
||||
await flush();
|
||||
|
||||
const tab = other.video.fire('keydown', { key: 'Tab' });
|
||||
const ctrl = combo.video.fire('keydown', { key: ' ', ctrlKey: true });
|
||||
const down = space.video.fire('keydown', { key: ' ' });
|
||||
|
||||
expect(tab.preventDefault).not.toHaveBeenCalled();
|
||||
expect(ctrl.preventDefault).not.toHaveBeenCalled();
|
||||
expect(other.attach).not.toHaveBeenCalled();
|
||||
expect(combo.attach).not.toHaveBeenCalled();
|
||||
expect(down.preventDefault).toHaveBeenCalled();
|
||||
expect(space.attach).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('bindet ein inzwischen entferntes Element nicht mehr', async () => {
|
||||
const state = setup();
|
||||
|
||||
state.video.fire('click');
|
||||
state.video.isConnected = false;
|
||||
await flush();
|
||||
|
||||
expect(state.attach).not.toHaveBeenCalled();
|
||||
expect(state.video.play).not.toHaveBeenCalled();
|
||||
expect(state.controller.state).toBe('idle');
|
||||
});
|
||||
|
||||
it('geht nach einem nicht ladbaren Skript zurück in den Anfang, der nächste Klick versucht es erneut', async () => {
|
||||
const importer = vi.fn()
|
||||
.mockRejectedValueOnce(new Error('Netz'))
|
||||
.mockResolvedValueOnce({ default: { name: 'Hls' } });
|
||||
const state = setup({ loader: createHlsLoader(importer) });
|
||||
|
||||
state.video.fire('click');
|
||||
await flush();
|
||||
expect(state.attach).not.toHaveBeenCalled();
|
||||
expect(state.controller.state).toBe('idle');
|
||||
|
||||
state.video.fire('click');
|
||||
await flush();
|
||||
|
||||
expect(state.attach).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('übersteht eine abgelehnte Wiedergabe: gebunden bleibt gebunden', async () => {
|
||||
const video = fakeVideo({ play: () => Promise.reject(new DOMException('NotAllowed', 'NotAllowedError')) });
|
||||
const state = setup({ video });
|
||||
|
||||
state.video.fire('click');
|
||||
await flush();
|
||||
await flush();
|
||||
|
||||
expect(state.attach).toHaveBeenCalledTimes(1);
|
||||
expect(state.controller.state).toBe('started');
|
||||
});
|
||||
|
||||
it('meldet jede Erneuerungsadresse und Quelle genau so, wie sie das Element nennt', async () => {
|
||||
const attach = vi.fn();
|
||||
const video = fakeVideo();
|
||||
const loader = fakeLoader().loader;
|
||||
const controller = bindLazySource(video, { source: 'https://luf.test/a.m3u8', loader, attach, observe: () => null });
|
||||
|
||||
controller.start();
|
||||
await flush();
|
||||
|
||||
expect(attach).toHaveBeenCalledWith(video, { Hls: { name: 'Hls' }, source: 'https://luf.test/a.m3u8', renewUrl: null });
|
||||
});
|
||||
});
|
||||
@@ -130,11 +130,11 @@ describe('createRenewingLoader', () => {
|
||||
return { Base, instances };
|
||||
}
|
||||
|
||||
function start(renew) {
|
||||
function start(renew, startContext = null) {
|
||||
const { Base, instances } = fakeBase();
|
||||
const Loader = createRenewingLoader(Base, renew);
|
||||
const callbacks = { onSuccess: vi.fn(), onError: vi.fn(), onTimeout: vi.fn(), onAbort: vi.fn() };
|
||||
const context = { url: 'https://luf.test/bereich/abschnitt/1/2/3/ableitungen/3/480/s.ts', type: 'fragment' };
|
||||
const context = startContext ?? { url: 'https://luf.test/bereich/abschnitt/1/2/3/ableitungen/3/480/s.ts', type: 'fragment' };
|
||||
const loader = new Loader({ id: 'konfiguration' });
|
||||
loader.load(context, { timeout: 1 }, callbacks);
|
||||
|
||||
@@ -171,6 +171,20 @@ describe('createRenewingLoader', () => {
|
||||
expect(callbacks.onError).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('erneuert auch eine abgelaufene Hauptliste, wenn ein Film erst spät gestartet wird', async () => {
|
||||
const renew = vi.fn(async () => 'https://luf.test/frische-hauptliste');
|
||||
const expired = 'https://luf.test/bereich/ausgabe/1/2/3/streamen?expires=1&signature=x';
|
||||
const master = { url: expired, type: 'manifest' };
|
||||
const { instances, callbacks } = start(renew, master);
|
||||
|
||||
instances[0].callbacks.onError(forbidden, master, null, {});
|
||||
await flush();
|
||||
|
||||
expect(renew).toHaveBeenCalledWith(expired);
|
||||
expect(instances[1].context.url).toBe('https://luf.test/frische-hauptliste');
|
||||
expect(callbacks.onError).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('wiederholt nur einmal: ein zweites 403 geht an hls.js weiter', async () => {
|
||||
const renew = vi.fn(async () => 'https://luf.test/frisch');
|
||||
const { instances, callbacks, context } = start(renew);
|
||||
|
||||
Reference in new issue
Block a user