Close colleague wait modal when order is no longer pending.
Settle on pending:false or any non-waiting status, and stop polling after a max wait so rejected orders do not leave the overlay stuck. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -10,6 +10,7 @@ import { getToken } from '@/utils'
|
|||||||
export const colleagueOrderWaiting = ref(false)
|
export const colleagueOrderWaiting = ref(false)
|
||||||
|
|
||||||
const CLIENT_POLL_MS = 5000
|
const CLIENT_POLL_MS = 5000
|
||||||
|
const MAX_WAIT_MS = 15 * 60 * 1000
|
||||||
const STREAM_EVENT = 'colleague-order'
|
const STREAM_EVENT = 'colleague-order'
|
||||||
|
|
||||||
type WaitResult = 'success' | 'failed' | 'none'
|
type WaitResult = 'success' | 'failed' | 'none'
|
||||||
@@ -68,17 +69,31 @@ function stsFromPayload(payload: Record<string, any> | null | undefined): number
|
|||||||
return Number.isFinite(sts) ? sts : null
|
return Number.isFinite(sts) ? sts : null
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readStatus(orderId: number | null): Promise<number | null> {
|
/** نتیجه قطعی از پاسخ status/pending — null یعنی هنوز باید صبر کرد */
|
||||||
|
function settleFromPayload(payload: Record<string, any> | null | undefined): WaitResult | null {
|
||||||
|
if (!payload || typeof payload !== 'object') return null
|
||||||
|
|
||||||
|
const pending = payload.pending
|
||||||
|
const sts = stsFromPayload(payload)
|
||||||
|
|
||||||
|
if (sts === 2) return 'success'
|
||||||
|
if (sts === 3) return 'failed'
|
||||||
|
if (sts !== null && sts !== 4) return 'failed'
|
||||||
|
if (pending === false) {
|
||||||
|
if (sts === 2) return 'success'
|
||||||
|
return 'failed'
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readSettle(orderId: number | null): Promise<WaitResult | null> {
|
||||||
try {
|
try {
|
||||||
if (orderId && orderId > 0) {
|
if (orderId && orderId > 0) {
|
||||||
const row = (await getColleagueOrderStatus(orderId)) as Record<string, any>
|
const row = (await getColleagueOrderStatus(orderId)) as Record<string, any>
|
||||||
return stsFromPayload(row)
|
return settleFromPayload(row)
|
||||||
}
|
}
|
||||||
const row = (await getPendingColleagueOrder()) as Record<string, any>
|
const row = (await getPendingColleagueOrder()) as Record<string, any>
|
||||||
if (row?.pending === false && row?.sts == null) {
|
return settleFromPayload(row)
|
||||||
return null
|
|
||||||
}
|
|
||||||
return stsFromPayload(row)
|
|
||||||
} catch {
|
} catch {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -90,6 +105,7 @@ export async function waitForColleagueOrder(orderId?: number | null): Promise<Wa
|
|||||||
inflight = (async () => {
|
inflight = (async () => {
|
||||||
colleagueOrderWaiting.value = true
|
colleagueOrderWaiting.value = true
|
||||||
let settled: WaitResult | null = null
|
let settled: WaitResult | null = null
|
||||||
|
const startedAt = Date.now()
|
||||||
|
|
||||||
const finish = (result: WaitResult) => {
|
const finish = (result: WaitResult) => {
|
||||||
if (settled) return
|
if (settled) return
|
||||||
@@ -101,22 +117,21 @@ export async function waitForColleagueOrder(orderId?: number | null): Promise<Wa
|
|||||||
if (orderId && Number(detail?.order_id) > 0 && Number(detail.order_id) !== orderId) {
|
if (orderId && Number(detail?.order_id) > 0 && Number(detail.order_id) !== orderId) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const sts = stsFromPayload(detail)
|
const result = settleFromPayload(detail ?? null)
|
||||||
if (sts === 2) finish('success')
|
if (result) finish(result)
|
||||||
if (sts === 3) finish('failed')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener(STREAM_EVENT, onStream)
|
window.addEventListener(STREAM_EVENT, onStream)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (!settled) {
|
while (!settled) {
|
||||||
const sts = await readStatus(orderId ?? null)
|
if (Date.now() - startedAt > MAX_WAIT_MS) {
|
||||||
if (sts === 2) {
|
finish('failed')
|
||||||
finish('success')
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if (sts === 3) {
|
const result = await readSettle(orderId ?? null)
|
||||||
finish('failed')
|
if (result) {
|
||||||
|
finish(result)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
await new Promise<void>((resolve) => {
|
await new Promise<void>((resolve) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user