Skip to content

Commit 1993df1

Browse files
committed
fix(Controller): handle the cancel prop in runAsync start callback
Remove `cancel` handling from the `runAsync` function, forcing callers to implement cancellation manually.
1 parent 2202328 commit 1993df1

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

packages/core/src/Controller.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { is, each, OneOrMore, toArray, UnknownProps, noop } from 'shared'
22
import * as G from 'shared/globals'
33

44
import { Lookup, Falsy } from './types/common'
5-
import { inferTo, flush } from './helpers'
5+
import { inferTo, flush, isDefaultProp } from './helpers'
66
import { FrameValue } from './FrameValue'
77
import { SpringPhase, CREATED, ACTIVE, IDLE } from './SpringPhase'
88
import { SpringValue, createLoopUpdate, createUpdate } from './SpringValue'
@@ -303,7 +303,14 @@ export function flushUpdate(
303303
pause: noop,
304304
start(props, resolve) {
305305
props.onRest = onRest as any
306-
resolve(runAsync(asyncTo, props, state, ctrl))
306+
if (!props.cancel) {
307+
resolve(runAsync(asyncTo, props, state, ctrl))
308+
}
309+
// Prevent `cancel: true` from ending the current `runAsync` call,
310+
// except when the default `cancel` prop is being set.
311+
else if (isDefaultProp(props, 'cancel')) {
312+
cancelAsync(state, props.callId)
313+
}
307314
},
308315
},
309316
})

packages/core/src/helpers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ export const getProps = <T, Arg = never>(
4949
props &&
5050
(is.fun(props) ? props(i, arg) : is.arr(props) ? props[i] : { ...props })
5151

52+
/** Returns `true` if the given prop is having its default value set. */
53+
export const isDefaultProp = <T extends Lookup>(props: T, key: keyof T) =>
54+
!is.und(
55+
props.default === true ? props[key] : props.default && props.default[key]
56+
)
57+
5258
export const mergeDefaultProps = (
5359
defaultProps: Lookup,
5460
props: Lookup & { default?: boolean | Lookup }

packages/core/src/runAsync.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ export async function runAsync<T>(
5353
state: RunAsyncState<T>,
5454
target: AnimationTarget<T>
5555
): AsyncResult<T> {
56-
if (props.cancel) {
57-
// Stop the active `asyncTo` only on "default cancel".
58-
if (props.default) {
59-
state.asyncTo = undefined
60-
}
61-
return getCancelledResult(target)
62-
}
6356
if (props.pause) {
6457
await new Promise(resume => {
6558
state.resumeQueue.add(resume)

0 commit comments

Comments
 (0)