Warm the stream cache for likely next plays

This commit is contained in:
Claude
2026-09-30 06:53:48 +00:00
parent 67c2f6e46c
commit b24366d6c0
4 changed files with 40 additions and 1 deletions

View File

@@ -327,6 +327,20 @@ const API = {
: call('cache.clear', 'cache_clear', {}),
};
// Pre-resolve streams for videos the user is likely to play next (server
// /api/streams/warm). Each id is warmed at most once per 20 min per tab.
const warmedAt = new Map();
function warmStreams(ids) {
if (!WEB || navigator.onLine === false) return;
const now = Date.now();
for (const id of ids) {
if (!id || !/^[A-Za-z0-9_-]{11}$/.test(id) || cachedIds.has(id)) continue;
if (now - (warmedAt.get(id) || 0) < 20 * 60_000) continue;
warmedAt.set(id, now);
fetch(`/api/streams/warm?v=${encodeURIComponent(id)}`, { priority: 'low' }).catch(() => {});
}
}
// Resolve a native file path to a URL the WebView can load (Tauri asset
// protocol). Returns null on shells without it.
function toAssetUrl(path) {
@@ -2303,6 +2317,7 @@ function wirePlayerEvents() {
perfMeasure('ytp:tap-to-play', 'ytp:tap');
try { performance.clearMarks('ytp:tap'); } catch { /* ignore */ }
}
if (Array.isArray(queue) && queueIndex >= 0) warmStreams(queue.slice(queueIndex + 1, queueIndex + 3).map((x) => x && x.id));
showSpinner(false);
Player.clearBufferGrace();
if (Player.secondary && !el.paused && Player.secondary.paused) {
@@ -7795,6 +7810,7 @@ function renderCard(v, index, list) {
card.insertBefore(noteBtn, menuBtn);
}
card.addEventListener('pointerdown', () => warmStreams([v.id]), { passive: true });
card.addEventListener('click', (e) => {
if (e.target.closest('.card-menu') || e.target.closest('.card-del') || e.target.closest('.card-note-btn') || e.target.closest('.card-channel.link')) return;
if (selectMode) { toggleSelectCard(v.id); return; }
@@ -8625,6 +8641,7 @@ function wireUI() {
searchAttempted = true;
renderList();
RecentSearches.cacheResults(q, results);
warmStreams(results.slice(0, 3).map((r) => r.id));
perfMeasure('ytp:search', 'ytp:search-start');
} catch (err) {
if (mySeq !== searchSeq) return;