189 build - уменьшинно количество запросов для одновременной работы на 2х матчах

This commit is contained in:
2026-10-02 18:39:15 +03:00
parent 74249d7fa2
commit e4bedb1264
10 changed files with 556 additions and 128 deletions

View File

@@ -14,6 +14,11 @@
root: null,
panelOpen: false,
loading: false,
deviceLoadPromise: null,
deviceLoadAgain: false,
deviceRevision: 0,
gameContextKey: "",
gameContextLanguage: runtimeStorage.getItem("hockey.language") === "en" ? "en" : "ru",
timer: null,
fastRefreshTimers: [],
message: "",
@@ -77,6 +82,15 @@
return String(runtimeData().hockey?.operator_session?.token || "").trim();
}
function gameContextKey() {
const hockey = runtimeData().hockey || {};
const session = hockey.operator_session || hockey.session || {};
return JSON.stringify([
currentGameId(), state.gameContextLanguage,
String(session.token || session.channel_key || "").trim(),
]);
}
function activeDevices() {
return state.devices.filter((item) => item.paired_to_me && item.active_for_account);
}
@@ -90,6 +104,7 @@
}
function selectLocalDevice(deviceId) {
state.deviceRevision += 1;
state.selectedDeviceId = String(deviceId || "").trim();
if (state.selectedDeviceId) runtimeStorage.setItem(selectedDeviceStorage, state.selectedDeviceId);
else runtimeStorage.removeItem(selectedDeviceStorage);
@@ -100,6 +115,7 @@
function mergeDevicePayload(payload) {
if (!payload || !payload.device_id) return;
state.deviceRevision += 1;
const index = state.devices.findIndex((item) => item.device_id === payload.device_id);
const previous = index >= 0 ? state.devices[index] : {};
const merged = { ...previous, ...payload };
@@ -112,7 +128,7 @@
function scheduleFastRefresh() {
state.fastRefreshTimers.forEach((timerId) => clearTimeout(timerId));
state.fastRefreshTimers = [0, 250, 1000].map((delay) => setTimeout(() => {
state.fastRefreshTimers = [1000].map((delay) => setTimeout(() => {
load(true).catch(() => {});
}, delay));
}
@@ -205,6 +221,7 @@
const deviceId = button.dataset.deviceId || "";
if (!deviceId) return;
state.loading = true;
state.deviceRevision += 1;
render();
try {
if (action === "pair") {
@@ -282,6 +299,7 @@
scheduleFastRefresh();
setMessage("Agent отвязан");
}
state.deviceRevision += 1;
await load(true);
} catch (error) {
setMessage(error.message || "Ошибка Agent", true);
@@ -291,35 +309,52 @@
}
}
async function load(force = false) {
if (state.loading && !force) return;
try {
const payload = await api("/api/hockey/agents/devices", { timeoutMs: 3500 });
state.devices = Array.isArray(payload.devices) ? payload.devices : [];
state.activeDeviceId = String(payload.active_device_id || "");
state.activeDeviceIds = Array.isArray(payload.active_device_ids)
? payload.active_device_ids.map(String)
: (state.activeDeviceId ? [state.activeDeviceId] : []);
const selectedStillValid = state.devices.some((item) => (
item.device_id === state.selectedDeviceId
&& item.paired_to_me
&& item.active_for_account
));
if (!selectedStillValid) {
const mine = activeDevices();
if (mine.length === 1) selectLocalDevice(mine[0].device_id);
else if (state.selectedDeviceId) selectLocalDevice("");
}
const editingTest = Boolean(state.root?.querySelector("[data-agent-test-form]")?.contains(document.activeElement));
if (!editingTest) render();
} catch (error) {
if (!String(error.message || "").includes("401")) {
console.warn("Could not load vMix agents", error);
if (state.panelOpen) setMessage(error.message || "Не удалось получить список Agent", true);
}
function load(force = false) {
if (state.loading && !force) return state.deviceLoadPromise;
if (state.deviceLoadPromise) {
if (force) state.deviceLoadAgain = true;
return state.deviceLoadPromise;
}
state.deviceLoadPromise = (async () => {
do {
state.deviceLoadAgain = false;
const revision = state.deviceRevision;
const contextKey = gameContextKey();
try {
const payload = await api("/api/hockey/agents/devices", { timeoutMs: 3500 });
// An older read must not undo a selection, a mutation or a match change.
if (revision !== state.deviceRevision || contextKey !== gameContextKey()) {
state.deviceLoadAgain = true;
}
if (state.deviceLoadAgain) continue;
state.devices = Array.isArray(payload.devices) ? payload.devices : [];
state.activeDeviceId = String(payload.active_device_id || "");
state.activeDeviceIds = Array.isArray(payload.active_device_ids)
? payload.active_device_ids.map(String)
: (state.activeDeviceId ? [state.activeDeviceId] : []);
const selectedStillValid = state.devices.some((item) => (
item.device_id === state.selectedDeviceId
&& item.paired_to_me
&& item.active_for_account
));
if (!selectedStillValid) {
const mine = activeDevices();
if (mine.length === 1) selectLocalDevice(mine[0].device_id);
else if (state.selectedDeviceId) selectLocalDevice("");
}
const editingTest = Boolean(state.root?.querySelector("[data-agent-test-form]")?.contains(document.activeElement));
if (!editingTest) render();
} catch (error) {
if (!String(error.message || "").includes("401")) {
console.warn("Could not load vMix agents", error);
if (state.panelOpen) setMessage(error.message || "Не удалось получить список Agent", true);
}
}
} while (state.deviceLoadAgain);
})().finally(() => { state.deviceLoadPromise = null; });
return state.deviceLoadPromise;
}
function deviceCard(item) {
@@ -465,9 +500,14 @@
render();
});
window.addEventListener("hockey:game-selected", () => {
setTimeout(() => load(true), 300);
setTimeout(() => load(true), 1300);
window.addEventListener("hockey:game-selected", (event) => {
if (event.detail?.language === "ru" || event.detail?.language === "en") {
state.gameContextLanguage = event.detail.language;
}
const contextKey = gameContextKey();
if (contextKey === state.gameContextKey) return;
state.gameContextKey = contextKey;
load(true);
});
window.addEventListener("focus", () => load(true));
document.addEventListener("visibilitychange", () => {
@@ -475,6 +515,7 @@
});
ensureRoot();
state.gameContextKey = gameContextKey();
load(true);
state.timer = window.setInterval(() => {
if (!document.hidden) load();