189 build - уменьшинно количество запросов для одновременной работы на 2х матчах
This commit is contained in:
@@ -16,6 +16,9 @@
|
||||
controlGameId: "",
|
||||
controlLoading: false,
|
||||
controlFetchedAt: 0,
|
||||
controlContextKey: "",
|
||||
controlRefreshQueued: false,
|
||||
controlReadRevision: 0,
|
||||
periodEditing: false,
|
||||
};
|
||||
|
||||
@@ -99,26 +102,48 @@
|
||||
return payload;
|
||||
}
|
||||
|
||||
function controlContext() {
|
||||
const hockey = window.UIBuilderRuntime?.getData?.()?.hockey || {};
|
||||
const gameId = String(hockey.selected_game?.external_id || hockey.selected_game?.id || "").trim();
|
||||
const language = hockey.language?.display === "en" ? "en" : "ru";
|
||||
const session = hockey.operator_session || hockey.session || {};
|
||||
const token = String(session.token || session.channel_key || "");
|
||||
return { gameId, language, key: JSON.stringify([gameId, language, token]) };
|
||||
}
|
||||
|
||||
async function loadControl(force = false) {
|
||||
const data = window.UIBuilderRuntime?.getData?.() || {};
|
||||
const gameId = String(data.hockey?.selected_game?.external_id || data.hockey?.selected_game?.id || "").trim();
|
||||
const language = data.hockey?.language?.display === "en" ? "en" : "ru";
|
||||
const { gameId, language, key } = controlContext();
|
||||
if (!gameId) {
|
||||
state.control = null;
|
||||
state.controlGameId = "";
|
||||
state.controlContextKey = key;
|
||||
return;
|
||||
}
|
||||
if (state.controlGameId !== gameId) {
|
||||
if (state.controlContextKey !== key) {
|
||||
state.control = null;
|
||||
state.controlGameId = gameId;
|
||||
state.controlContextKey = key;
|
||||
state.controlFetchedAt = 0;
|
||||
force = true;
|
||||
}
|
||||
if (state.controlLoading) return;
|
||||
if (state.controlLoading) {
|
||||
if (force) state.controlRefreshQueued = true;
|
||||
return;
|
||||
}
|
||||
if (!force && Date.now() - state.controlFetchedAt < 1800) return;
|
||||
state.controlLoading = true;
|
||||
const readRevision = state.controlReadRevision;
|
||||
try {
|
||||
const previousPeriod = String(state.control?.current_period || "");
|
||||
state.control = await controlRequest(`/games/${encodeURIComponent(gameId)}/control?language=${language}`);
|
||||
const payload = await controlRequest(`/games/${encodeURIComponent(gameId)}/control?language=${language}`);
|
||||
if (key !== state.controlContextKey || key !== controlContext().key) {
|
||||
state.controlRefreshQueued = true;
|
||||
return;
|
||||
}
|
||||
// A timer/period mutation can publish a newer snapshot while this read
|
||||
// is pending. Keep that snapshot until the next ordinary refresh.
|
||||
if (readRevision !== state.controlReadRevision) return;
|
||||
state.control = payload;
|
||||
state.controlFetchedAt = Date.now();
|
||||
const nextPeriod = String(state.control?.current_period || "");
|
||||
if (!window.HockeyMatchAccess?.canControl?.(gameId)) {
|
||||
@@ -136,6 +161,10 @@
|
||||
console.error("Could not load hockey period state", error);
|
||||
} finally {
|
||||
state.controlLoading = false;
|
||||
if (state.controlRefreshQueued) {
|
||||
state.controlRefreshQueued = false;
|
||||
void loadControl(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -420,12 +449,16 @@
|
||||
}, 250);
|
||||
}
|
||||
|
||||
window.addEventListener("hockey:game-selected", () => { loadControl(true); render(true); });
|
||||
window.addEventListener("hockey:game-selected", () => { loadControl(); render(true); });
|
||||
window.addEventListener("hockey:settings-updated", () => { loadControl(true); render(true); });
|
||||
window.addEventListener("hockey:game-control-updated", (event) => {
|
||||
const payload = event.detail?.control;
|
||||
const gameId = String(event.detail?.game_id || payload?.game_id || "");
|
||||
if (!payload || (state.controlGameId && gameId && state.controlGameId !== gameId)) return;
|
||||
const context = controlContext();
|
||||
if (!payload || (gameId && context.gameId !== gameId)) return;
|
||||
state.controlReadRevision += 1;
|
||||
state.controlGameId = context.gameId;
|
||||
state.controlContextKey = context.key;
|
||||
state.control = payload;
|
||||
state.controlFetchedAt = Date.now();
|
||||
render(true);
|
||||
|
||||
Reference in New Issue
Block a user