import { api } from '@/api/client' import { StaleSessionError } from '@/api/errors' import type { GoldLandProgressResponse } from '../types/goldLand.types' /** Monotonic seq so an older in-flight progress response cannot overwrite a newer one (same session). */ let progressRequestSeq = 0 let lastCommittedProgressSeq = 0 let progressEpoch = 0 export function resetGoldLandProgressSync(): void { progressRequestSeq = 0 lastCommittedProgressSeq = 0 progressEpoch += 1 } export async function fetchGoldLandProgress(): Promise { const epochAtStart = progressEpoch const seq = ++progressRequestSeq const data = await api.getGoldLandProgress() if (epochAtStart !== progressEpoch) { throw new StaleSessionError() } if (seq < lastCommittedProgressSeq) { throw new StaleSessionError() } lastCommittedProgressSeq = seq return data }