fix: can not save when switch to skill

This commit is contained in:
Joel
2026-01-21 13:56:02 +08:00
parent c3333006cf
commit 5657bf52f0
3 changed files with 79 additions and 25 deletions

View File

@ -1,5 +1,6 @@
import {
useCallback,
useEffect,
useRef,
} from 'react'
@ -8,15 +9,25 @@ export const useSerialAsyncCallback = <Args extends any[], Result = void>(
shouldSkip?: () => boolean,
) => {
const queueRef = useRef<Promise<unknown>>(Promise.resolve())
const fnRef = useRef(fn)
const shouldSkipRef = useRef(shouldSkip)
useEffect(() => {
fnRef.current = fn
}, [fn])
useEffect(() => {
shouldSkipRef.current = shouldSkip
}, [shouldSkip])
return useCallback((...args: Args) => {
if (shouldSkip?.())
if (shouldSkipRef.current?.())
return Promise.resolve(undefined as Result)
const lastPromise = queueRef.current.catch(() => undefined)
const nextPromise = lastPromise.then(() => fn(...args))
const nextPromise = lastPromise.then(() => fnRef.current(...args))
queueRef.current = nextPromise
return nextPromise
}, [fn, shouldSkip])
}, [])
}