fix: useRequest 请求序号防竞态 + ty-crud 搜索/重置去重
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
ed1a1e115c
commit
77c9c3fd25
@ -173,13 +173,20 @@ watch([page, pageSize], () => {
|
||||
})
|
||||
|
||||
function handleSearch(): void {
|
||||
page.value = 1
|
||||
fetchList()
|
||||
resetAndFetch()
|
||||
}
|
||||
|
||||
function handleReset(): void {
|
||||
page.value = 1
|
||||
fetchList()
|
||||
resetAndFetch()
|
||||
}
|
||||
|
||||
/** 重置到第一页并查询:page 已是 1 时 watch 不触发,需手动查询,避免重复请求。 */
|
||||
function resetAndFetch(): void {
|
||||
if (page.value === 1) {
|
||||
fetchList()
|
||||
} else {
|
||||
page.value = 1
|
||||
}
|
||||
}
|
||||
|
||||
async function openCreate(): Promise<void> {
|
||||
|
||||
@ -47,10 +47,12 @@ export function useRequest<TData, TParams extends unknown[]>(
|
||||
|
||||
let lastParams: TParams = defaultParams
|
||||
let controller: AbortController | null = null
|
||||
let seq = 0
|
||||
|
||||
async function run(...args: TParams): Promise<TData | undefined> {
|
||||
controller?.abort()
|
||||
controller = new AbortController()
|
||||
const mySeq = ++seq
|
||||
lastParams = args.length > 0 ? args : lastParams
|
||||
|
||||
loading.value = true
|
||||
@ -59,16 +61,18 @@ export function useRequest<TData, TParams extends unknown[]>(
|
||||
|
||||
try {
|
||||
const result = await fetcher(...lastParams, controller.signal)
|
||||
if (mySeq !== seq) return undefined // 已被更新的请求取代
|
||||
data.value = result
|
||||
options.onSuccess?.(result, lastParams)
|
||||
return result
|
||||
} catch (e) {
|
||||
if (isCanceled(e)) return undefined
|
||||
if (mySeq !== seq) return undefined
|
||||
error.value = e
|
||||
options.onError?.(e, lastParams)
|
||||
return undefined
|
||||
} finally {
|
||||
loading.value = false
|
||||
if (mySeq === seq) loading.value = false
|
||||
options.onFinally?.(lastParams)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user