29 lines
1.2 KiB
TypeScript
29 lines
1.2 KiB
TypeScript
import { onScopeDispose, watch, type Ref } from 'vue'
|
|
import type { OverseasFee } from './api/overseasFees'
|
|
|
|
/** 跟踪尚未收到回执的事件;失败后自动重试期间继续回查,离开页面即停止。 */
|
|
export function useFeeStatusRefresh(rows: Ref<OverseasFee[]>, refresh: () => Promise<void>, enabled: Ref<boolean>) {
|
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
let remaining = 90
|
|
let stopped = false
|
|
const pending = () => rows.value.some(row => row.walletStatus !== 'SETTLED' && (['PENDING', 'PROCESSING'].includes(row.deliveryStatus || '') || row.walletStatus?.endsWith('_PENDING')))
|
|
function clear() { if (timer !== undefined) clearTimeout(timer); timer = undefined }
|
|
function schedule() {
|
|
clear()
|
|
if (stopped || !enabled.value || !pending() || remaining <= 0) return
|
|
timer = setTimeout(async () => {
|
|
timer = undefined
|
|
if (stopped || !enabled.value) return
|
|
remaining--
|
|
await refresh()
|
|
schedule()
|
|
}, remaining > 60 ? 2000 : 10000)
|
|
}
|
|
watch([rows, enabled], () => {
|
|
if (!pending()) remaining = 90
|
|
schedule()
|
|
}, { immediate: true })
|
|
onScopeDispose(() => { stopped = true; clear() })
|
|
return { reset: () => { remaining = 90; clear() } }
|
|
}
|