Every page reads the reserve for the Sokko TCG menu count, and those reads shared the game API limit of 60 requests per minute and per address. Browser tests reloading many pages from one address got 429 responses. The route now has its own limit, like the public configuration, and focus or tab returns read it at most once every 15 seconds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
60 lines
2.5 KiB
JavaScript
60 lines
2.5 KiB
JavaScript
// Rôle : partage la réserve d'ouvertures Sokko TCG entre la page de jeu et la pastille du menu.
|
|
// La page publie chaque état serveur reçu ; hors de la page, la route légère `/reserve` le relit.
|
|
import { useEffect, useSyncExternalStore } from "react";
|
|
import { loadCardsReserve } from "./cardsApi.js";
|
|
|
|
let reserve = null;
|
|
const listeners = new Set();
|
|
const subscribe = (listener) => {
|
|
listeners.add(listener);
|
|
return () => listeners.delete(listener);
|
|
};
|
|
const snapshot = () => reserve;
|
|
|
|
// `recharge` vient de l'état serveur ; `null` quand personne n'est connecté.
|
|
export function publishCardsReserve(recharge) {
|
|
const next = recharge ? { openingsBalance: recharge.openingsBalance, nextOpeningAt: recharge.nextOpeningAt } : null;
|
|
if (next?.openingsBalance === reserve?.openingsBalance && next?.nextOpeningAt === reserve?.nextOpeningAt) return;
|
|
reserve = next;
|
|
for (const listener of listeners) listener();
|
|
}
|
|
|
|
// Le focus et le retour sur l'onglet arrivent souvent ensemble : une relecture par période suffit.
|
|
const REFRESH_INTERVAL_MS = 15_000;
|
|
let lastRefreshAt = 0;
|
|
|
|
async function refreshReserve({ force = false } = {}) {
|
|
if (globalThis.document.visibilityState === "hidden") return;
|
|
if (!force && Date.now() - lastRefreshAt < REFRESH_INTERVAL_MS) return;
|
|
lastRefreshAt = Date.now();
|
|
try {
|
|
publishCardsReserve((await loadCardsReserve()).recharge);
|
|
} catch {
|
|
// Hors connexion, la pastille garde la dernière réserve connue.
|
|
}
|
|
}
|
|
|
|
// Renvoie le solde quand la réserve est pleine, sinon `null` : une réserve pleine n'a plus d'échéance de recharge.
|
|
export function useFullCardsReserve(enabled) {
|
|
const current = useSyncExternalStore(subscribe, snapshot);
|
|
const nextOpeningAt = current?.nextOpeningAt;
|
|
|
|
useEffect(() => {
|
|
if (!enabled) return undefined;
|
|
void refreshReserve({ force: true });
|
|
window.addEventListener("focus", refreshReserve);
|
|
globalThis.document.addEventListener("visibilitychange", refreshReserve);
|
|
return () => {
|
|
window.removeEventListener("focus", refreshReserve);
|
|
globalThis.document.removeEventListener("visibilitychange", refreshReserve);
|
|
};
|
|
}, [enabled]);
|
|
|
|
useEffect(() => {
|
|
if (!enabled || !nextOpeningAt) return undefined;
|
|
const timeout = window.setTimeout(() => refreshReserve({ force: true }), Math.max(0, nextOpeningAt - Date.now()) + 250);
|
|
return () => window.clearTimeout(timeout);
|
|
}, [enabled, nextOpeningAt]);
|
|
|
|
return enabled && current && current.nextOpeningAt == null && current.openingsBalance > 0 ? current.openingsBalance : null;
|
|
}
|