BUILD198 — исправить плашки и ускорить управление vMix
This commit is contained in:
@@ -48,8 +48,8 @@
|
||||
const ready = new Promise(resolve => setTimeout(resolve, channel ? 120 : 0));
|
||||
let game = "", access = null, leaseDeadline = 0, suspended = true, epoch = 0;
|
||||
let queue = Promise.resolve(), queueSize = 0, periodPending = 0, polling = false;
|
||||
let lastJoin = 0, lastHeartbeat = 0, root = null, trigger = null, selectPromise = null, busy = false;
|
||||
let opening = false, openingFailed = false, selectionVersion = 0;
|
||||
let lastJoin = 0, lastHeartbeat = 0, root = null, trigger = null, selectPromise = null, selectPromiseVersion = 0, busy = false;
|
||||
let opening = false, openingFailed = false, selectionVersion = 0, selectRequestVersion = 0, accessChoice = null;
|
||||
const device = () => String(storage.getItem("hockey.vmix.selected_device") || "");
|
||||
const canControl = (gameId = game) => Boolean(game && String(gameId) === game && !suspended
|
||||
&& access?.can_control && performance.now() < leaseDeadline);
|
||||
@@ -196,13 +196,18 @@
|
||||
};
|
||||
async function beforeGameChange(next) {
|
||||
if (!game || String(next) === game) return;
|
||||
const selected = game, version = selectionVersion;
|
||||
const current = () => selected === game && version === selectionVersion;
|
||||
closeForm();
|
||||
if (canControl()) {
|
||||
await window.UIBuilderRuntime?.flushGameTimers?.();
|
||||
await queue;
|
||||
if (!current()) return;
|
||||
try { await api("release"); } catch (_) { /* Remaining lease expires on the server. */ }
|
||||
}
|
||||
if (!current()) return;
|
||||
try { await api("leave"); } catch (_) { /* Presence also expires if disconnected. */ }
|
||||
if (!current()) return;
|
||||
epoch += 1;
|
||||
selectionVersion += 1;
|
||||
suspended = true;
|
||||
@@ -210,10 +215,43 @@
|
||||
game = "";
|
||||
paint();
|
||||
}
|
||||
async function selectGame(gameId, { waitForWorkspace = false } = {}) {
|
||||
async function waitForAccessChoice(selected, version, waitForWorkspace, requestVersion) {
|
||||
if (!root || !access || canControl()) return access;
|
||||
// A coalesced caller can request this decision after a default non-waiting
|
||||
// selection has already opened the native dialog.
|
||||
opening = false;
|
||||
let choice = accessChoice;
|
||||
if (!choice || choice.selected !== selected || choice.version !== version) {
|
||||
let resolve;
|
||||
const promise = new Promise(yes => { resolve = yes; });
|
||||
choice = accessChoice = { selected, version, promise, resolve, waitForWorkspace: false };
|
||||
}
|
||||
choice.waitForWorkspace ||= waitForWorkspace;
|
||||
openForm();
|
||||
await choice.promise;
|
||||
if (selected !== game || version !== selectionVersion || requestVersion !== selectRequestVersion) return null;
|
||||
opening = choice.waitForWorkspace;
|
||||
paint();
|
||||
return access;
|
||||
}
|
||||
|
||||
async function selectGame(gameId, { waitForWorkspace = false, waitForChoice = false } = {}) {
|
||||
gameId = String(gameId || "");
|
||||
if (selectPromise && game === gameId) return selectPromise;
|
||||
if (selectPromise && game === gameId && selectPromiseVersion === selectRequestVersion) {
|
||||
const selected = game, version = selectionVersion, requestVersion = selectRequestVersion;
|
||||
const result = await selectPromise;
|
||||
if (result === null || selected !== game || version !== selectionVersion || requestVersion !== selectRequestVersion) return null;
|
||||
if (waitForChoice && root?.open && !canControl()) {
|
||||
const choice = waitForAccessChoice(selected, version, waitForWorkspace, requestVersion);
|
||||
selectPromise = choice;
|
||||
selectPromiseVersion = requestVersion;
|
||||
try { return await choice; } finally { if (selectPromise === choice) selectPromise = null; }
|
||||
}
|
||||
return result;
|
||||
}
|
||||
const requestVersion = ++selectRequestVersion;
|
||||
await beforeGameChange(gameId);
|
||||
if (requestVersion !== selectRequestVersion) return null;
|
||||
closeForm();
|
||||
selectionVersion += 1;
|
||||
game = gameId;
|
||||
@@ -223,19 +261,27 @@
|
||||
openingFailed = false;
|
||||
notice("");
|
||||
paint();
|
||||
const selected = game, version = selectionVersion;
|
||||
const current = () => selected === game && version === selectionVersion && requestVersion === selectRequestVersion;
|
||||
const task = (async () => {
|
||||
try {
|
||||
// Device validation belongs to the actual assignment. Joining as a
|
||||
// spectator remains possible with a stale local device selection.
|
||||
try { await api("join", { device_id: device() }); }
|
||||
catch (_) { await api("join", {}); }
|
||||
catch (error) { if (!current()) return null; await api("join", {}); }
|
||||
if (!current()) return null;
|
||||
lastJoin = Date.now();
|
||||
if (access && !access.can_control) openForm();
|
||||
await syncControl();
|
||||
if (!current()) return null;
|
||||
if (access && !canControl()) {
|
||||
if (waitForChoice) return await waitForAccessChoice(selected, version, waitForWorkspace, requestVersion);
|
||||
openForm();
|
||||
}
|
||||
return access;
|
||||
} catch (error) { notice(error.message); throw error; }
|
||||
} catch (error) { if (!current()) return null; notice(error.message); throw error; }
|
||||
})();
|
||||
selectPromise = task;
|
||||
selectPromiseVersion = requestVersion;
|
||||
try { return await task; } finally { if (selectPromise === task) selectPromise = null; }
|
||||
}
|
||||
function finishOpening(gameId, success = true) {
|
||||
@@ -273,7 +319,12 @@
|
||||
}
|
||||
} catch (error) {
|
||||
await recover(error.message);
|
||||
} finally { busy = false; root?.removeAttribute("aria-busy"); paint(); }
|
||||
} finally {
|
||||
busy = false;
|
||||
root?.removeAttribute("aria-busy");
|
||||
paint();
|
||||
if (accessChoice && accessChoice.selected === game && accessChoice.version === selectionVersion && canControl()) closeForm();
|
||||
}
|
||||
}
|
||||
async function poll() {
|
||||
if (!game || polling || selectPromise || busy || opening || openingFailed) return;
|
||||
@@ -367,11 +418,17 @@
|
||||
paintControls();
|
||||
}
|
||||
function closeForm() {
|
||||
if (!root?.open) return;
|
||||
if (typeof root.close === "function") root.close();
|
||||
else root.removeAttribute("open");
|
||||
trigger?.setAttribute("aria-expanded", "false");
|
||||
trigger?.focus({ preventScroll: true });
|
||||
if (root?.open) {
|
||||
if (typeof root.close === "function") root.close();
|
||||
else root.removeAttribute("open");
|
||||
trigger?.setAttribute("aria-expanded", "false");
|
||||
trigger?.focus({ preventScroll: true });
|
||||
}
|
||||
if (accessChoice) {
|
||||
const choice = accessChoice;
|
||||
accessChoice = null;
|
||||
choice.resolve();
|
||||
}
|
||||
}
|
||||
function openForm() {
|
||||
if (!game || !root || root.open) return;
|
||||
|
||||
@@ -2391,7 +2391,6 @@ function gameCard(game) {
|
||||
if (state.matchOpeningJob) cancelOpeningMatchLoad(state.matchOpeningJob);
|
||||
state.matchOpeningJob = job;
|
||||
const loadingGame = matchLabelById(id) || `${t("gameNumber")} ID ${id}`;
|
||||
setMatchLoading(true, { game: loadingGame, stage: t("loadingMatchDetails") });
|
||||
try {
|
||||
await window.HockeyMatchAccess?.beforeGameChange?.(String(id));
|
||||
if (!openingMatchLoadCurrent(job)) return;
|
||||
@@ -2466,9 +2465,9 @@ function gameCard(game) {
|
||||
const cachedDetailsPromise = manual ? Promise.resolve(null)
|
||||
: loadCachedGameSnapshot(id, generation).then((cached) => { resolvedCachedDetails = cached; return cached; });
|
||||
|
||||
await window.HockeyMatchAccess?.selectGame?.(String(id), { waitForWorkspace: true });
|
||||
if (!openingMatchLoadCurrent(job)) return;
|
||||
updateMatchLoading(t("loadingMatchSession"));
|
||||
const selectedAccess = await window.HockeyMatchAccess?.selectGame?.(String(id), { waitForWorkspace: true, waitForChoice: true });
|
||||
if (selectedAccess === null || !openingMatchLoadCurrent(job)) return;
|
||||
setMatchLoading(true, { game: loadingGame, stage: t("loadingMatchSession") });
|
||||
// /api/hockey/sessions already reuses the active operator row and replaces
|
||||
// its match binding. A GET + DELETE of the previous token here only added
|
||||
// two extra PostgreSQL round-trips before the new match could open.
|
||||
|
||||
Reference in New Issue
Block a user