fix: console error "reading then" "#409
Massimo Melina committed
Dec 5, 2023 at 23:36 UTC
9f2c09d016a36af12496e2d2103bea0f8fafcd6b
1 file changed
+15
-14
src/debounceAsync.ts
+15
-14
@@ -11,15 +11,15 @@ export function debounceAsync<Cancelable extends boolean = false, A extends unkn
11
const { leading=false, maxWait=Infinity, cancelable=false, retain=0, retainFailure } = options
12
let started = 0 // latest callback invocation
13
let runningCallback: Promise<R> | undefined // latest callback invocation result
14
- let runningDebouncer: Promise<MaybeR | R> // latest wrapper invocation
14
+ let latestDebouncer: Promise<MaybeR | R> // latest wrapper invocation
15
let waitingSince = 0 // we are delaying invocation since
16
let whoIsWaiting: undefined | A // args object identifies the pending instance, and incidentally stores args
17
- let last: typeof runningCallback
18
- let lastFailed = false
19
- let lastSince = 0
20
- const interceptingWrapper = (...args: A) => runningDebouncer = debouncer(...args)
17
+ let latestCallback: typeof runningCallback
18
+ let latestHasFailed = false
19
+ let latestTimestamp = 0
20
+ const interceptingWrapper = (...args: A) => latestDebouncer = debouncer(...args)
21
return Object.assign(interceptingWrapper, {
22
- clearRetain: () => last = undefined,
22
+ clearRetain: () => latestCallback = undefined,
23
flush: () => runningCallback ?? exec(),
24
isWorking: () => runningCallback,
25
...cancelable && {
@@ -34,8 +34,8 @@ export function debounceAsync<Cancelable extends boolean = false, A extends unkn
34
if (runningCallback)
35
return runningCallback as MaybeR
36
const now = Date.now()
37
- if (last && now - lastSince < (lastFailed ? retainFailure ?? retain : retain))
38
- return await last
37
+ if (latestCallback && now - latestTimestamp < (latestHasFailed ? retainFailure ?? retain : retain))
38
+ return await latestCallback
39
whoIsWaiting = args
40
waitingSince ||= now
41
const waitingCap = maxWait - (now - (waitingSince || started))
@@ -45,7 +45,7 @@ export function debounceAsync<Cancelable extends boolean = false, A extends unkn
45
if (!whoIsWaiting) // canceled
46
return void(waitingSince = 0) as MaybeR
47
if (whoIsWaiting !== args) // another fresher call is waiting
48
- return runningDebouncer
48
+ return latestDebouncer
49
return exec()
50
}
51
@@ -54,14 +54,15 @@ export function debounceAsync<Cancelable extends boolean = false, A extends unkn
54
waitingSince = 0
55
started = Date.now()
56
try {
57
- runningCallback = callback(...whoIsWaiting)
57
+ const args = whoIsWaiting
58
+ whoIsWaiting = undefined
59
+ runningCallback = callback(...args)
60
+ runningCallback.then(() => latestHasFailed = false, () => latestHasFailed = true)
61
return await runningCallback as MaybeUndefined<R> // await necessary to go-finally at the right time and even on exceptions
62
}
63
finally {
61
- last = runningCallback
62
- last!.then(() => lastFailed = false, () => lastFailed = true)
63
- lastSince = Date.now()
64
- whoIsWaiting = undefined
64
+ latestCallback = runningCallback
65
+ latestTimestamp = Date.now()
66
runningCallback = undefined
67
}
68
}