Skip to content

Commit 2b0f861

Browse files
committed
fix: call "cancel" function before "delay" timeout
1 parent cd0c71c commit 2b0f861

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

packages/core/src/runAsync.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,16 @@ export function scheduleProps<T>(
179179
{ key, props, state, action }: ScheduledProps<T>
180180
): AsyncResult<T> {
181181
return new Promise((resolve, reject) => {
182-
const pause = matchProp(props.pause, key)
182+
let delay = 0
183+
let pause = false
183184

184-
let delay = Math.max(0, callProp(props.delay || 0, key))
185+
let cancel = matchProp(props.cancel, key)
186+
if (cancel) {
187+
state.cancelId = callId
188+
return next()
189+
}
190+
191+
delay = Math.max(0, callProp(props.delay || 0, key))
185192
if (delay > 0) {
186193
let timeout: Timeout
187194
const onPause = () => {
@@ -195,7 +202,7 @@ export function scheduleProps<T>(
195202
state.pause = concatFn(state.pause, onPause)
196203
timeout = G.frameLoop.setTimeout(next, delay)
197204
}
198-
if (pause) {
205+
if ((pause = matchProp(props.pause, key))) {
199206
state.unpause = concatFn(state.unpause, onResume)
200207
} else {
201208
timeout = G.frameLoop.setTimeout(next, delay)
@@ -209,18 +216,12 @@ export function scheduleProps<T>(
209216
if (delay > 0) {
210217
state.pause = void 0
211218
}
212-
let { cancel, reset } = props
219+
// Maybe cancelled during its delay.
220+
if (callId <= (state.cancelId || 0)) {
221+
cancel = true
222+
}
213223
try {
214-
// Might have been cancelled during its delay.
215-
if (callId <= (state.cancelId || 0)) {
216-
cancel = true
217-
} else {
218-
cancel = matchProp(cancel, key)
219-
if (cancel) {
220-
state.cancelId = callId
221-
}
222-
}
223-
reset = !cancel && matchProp(reset, key)
224+
const reset = !cancel && matchProp(props.reset, key)
224225
action({ ...props, callId, delay, cancel, pause, reset }, resolve)
225226
} catch (err) {
226227
reject(err)

0 commit comments

Comments
 (0)