Files
ytplayer/frontend/prewarm-next.js
2026-10-07 16:30:01 +08:00

38 lines
1.5 KiB
JavaScript

/* Pure queue candidate selection for stream prewarming. */
(function (root, factory) {
if (typeof module === 'object' && module.exports) module.exports = factory();
else root.PrewarmNext = factory();
})(typeof globalThis !== 'undefined' ? globalThis : this, function () {
'use strict';
function candidates(queue, index, state = {}, n = 2, known = {}) {
if (!Array.isArray(queue) || !queue.length || !Number.isInteger(index) || index < 0 || index >= queue.length) return [];
if (state.loopOne) return [];
const count = Math.max(0, Math.floor(Number(n) || 0));
if (!count) return [];
const repeatAll = state.repeatMode === 'all' || state.repeatAll === true;
const offline = new Set(known.offline || []);
const serverCached = new Set(known.serverCached || []);
const current = queue[index];
const currentId = typeof current === 'string' ? current : current && current.id;
const seen = new Set(currentId ? [currentId] : []);
const ids = [];
for (let step = 1; step < queue.length + (repeatAll ? 1 : 0) && ids.length < count; step++) {
let at = index + step;
if (at >= queue.length) {
if (!repeatAll) break;
at %= queue.length;
}
const item = queue[at];
const id = typeof item === 'string' ? item : item && item.id;
if (!id || seen.has(id)) continue;
seen.add(id);
if (offline.has(id) || serverCached.has(id) || (item && item.serverCached)) continue;
ids.push(id);
}
return ids;
}
return { candidates };
});