fix: 高延迟下连按「刷新」只发送一次请求(GitHub #501)

This commit is contained in:
robll-v1 committed 2026-10-11 16:43:51 +08:00
1 parent bc50cac01f
commit 289a454fa1
10 files changed
+186 -8

No files matched your search

+22 -1
View File
@@ -20,6 +20,21 @@ const emitBusy = () => { for (const fn of [...busyListeners]) { try { fn(infligh
/** Subscribe to the number of in-flight intents (for subtle busy indicators). */
export function onBusy(fn) { busyListeners.add(fn); return () => busyListeners.delete(fn); }
// Single-flight shop refresh (GitHub #501, single-ptilopsis): under a high RTT the shop has not answered yet while the
// player taps 刷新 again — every extra g.refresh the client sends is a paid refresh (server/match intents.js → ps.refresh).
// A tap while the previous request is still on the wire is dropped; the button dims and the R hotkey stays inert (both
// call actions.refresh) until the reply lands. ui/shopBar.js subscribes to onRefreshBusy for the dim.
let refreshPending = false;
const refreshBusyListeners = new Set();
const emitRefreshBusy = () => { for (const fn of [...refreshBusyListeners]) { try { fn(refreshPending); } catch { /* ignore */ } } };
/**
* Subscribe to the shop refresh's in-flight state — fires once with the current state.
* @param {(busy: boolean) => void} fn
* @returns {() => void} unsubscribe
*/
export function onRefreshBusy(fn) { refreshBusyListeners.add(fn); try { fn(refreshPending); } catch { /* ignore */ } return () => refreshBusyListeners.delete(fn); }
/**
* Send an intent. Resolves true on `ok`, false on error (already toasted).
* @param {string} t
@@ -55,7 +70,13 @@ export const actions = {
band: (bandId) => act('g.band', { bandId }),
bandSkip: () => act('g.bandSkip'),
buy: (slot) => act('g.buy', { slot }),
refresh: () => act('g.refresh'),
// a tap while the previous g.refresh is still on the wire is dropped (GitHub #501) — see onRefreshBusy above
refresh: () => {
if (refreshPending) return Promise.resolve(false);
refreshPending = true;
emitRefreshBusy();
return act('g.refresh').finally(() => { refreshPending = false; emitRefreshBusy(); });
},
freeze: () => act('g.freeze'),
levelUp: () => act('g.levelUp'),
sell: (uid) => act('g.sell', { uid }),