Files
gold_land_user/src/modules/gold-land/services/goldLandProgressSync.ts
T

29 lines
885 B
TypeScript
Raw Normal View History

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<GoldLandProgressResponse> {
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
}