|
| 1 | +import * as G from './globals' |
| 2 | +import { raf } from "./rafz"; |
| 3 | +import { InferProps, InferState, RunAsyncProps, RunAsyncState } from "./runAsync"; |
| 4 | +import { AnimationResolver, AnimationTarget, AsyncResult, callProp, is, matchProp, MatchProp, Timeout } from "./utils"; |
| 5 | + |
| 6 | +// The `scheduleProps` function only handles these defaults. |
| 7 | +type DefaultProps<T> = { cancel?: MatchProp<T>; pause?: MatchProp<T> } |
| 8 | + |
| 9 | +interface ScheduledProps<T extends AnimationTarget> { |
| 10 | + key?: string |
| 11 | + props: InferProps<T> |
| 12 | + defaultProps?: DefaultProps<InferState<T>> |
| 13 | + state: RunAsyncState<T> |
| 14 | + actions: { |
| 15 | + pause: () => void |
| 16 | + resume: () => void |
| 17 | + start: (props: RunAsyncProps<T>, resolve: AnimationResolver<T>) => void |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * This function sets a timeout if both the `delay` prop exists and |
| 23 | + * the `cancel` prop is not `true`. |
| 24 | + * |
| 25 | + * The `actions.start` function must handle the `cancel` prop itself, |
| 26 | + * but the `pause` prop is taken care of. |
| 27 | + */ |
| 28 | +export function scheduleProps<T extends AnimationTarget>( |
| 29 | + callId: number, |
| 30 | + { key, props, defaultProps, state, actions }: ScheduledProps<T> |
| 31 | +): AsyncResult<T> { |
| 32 | + return new Promise((resolve, reject) => { |
| 33 | + let delay: number |
| 34 | + let timeout: Timeout |
| 35 | + |
| 36 | + let cancel = matchProp(props.cancel ?? defaultProps?.cancel, key) |
| 37 | + if (cancel) { |
| 38 | + onStart() |
| 39 | + } else { |
| 40 | + // The `pause` prop updates the paused flag. |
| 41 | + if (!is.und(props.pause)) { |
| 42 | + state.paused = matchProp(props.pause, key) |
| 43 | + } |
| 44 | + // The default `pause` takes precedence when true, |
| 45 | + // which allows `SpringContext` to work as expected. |
| 46 | + let pause = defaultProps?.pause |
| 47 | + if (pause !== true) { |
| 48 | + pause = state.paused || matchProp(pause, key) |
| 49 | + } |
| 50 | + |
| 51 | + delay = callProp(props.delay || 0, key) |
| 52 | + if (pause) { |
| 53 | + state.resumeQueue.add(onResume) |
| 54 | + actions.pause() |
| 55 | + } else { |
| 56 | + actions.resume() |
| 57 | + onResume() |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + function onPause() { |
| 62 | + state.resumeQueue.add(onResume) |
| 63 | + state.timeouts.delete(timeout) |
| 64 | + timeout.cancel() |
| 65 | + // Cache the remaining delay. |
| 66 | + delay = timeout.time - raf.now() |
| 67 | + } |
| 68 | + |
| 69 | + function onResume() { |
| 70 | + if (delay > 0 && !G.skipAnimation) { |
| 71 | + state.delayed = true |
| 72 | + timeout = raf.setTimeout(onStart, delay) |
| 73 | + state.pauseQueue.add(onPause) |
| 74 | + state.timeouts.add(timeout) |
| 75 | + } else { |
| 76 | + onStart() |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + function onStart() { |
| 81 | + if (state.delayed) { |
| 82 | + state.delayed = false |
| 83 | + } |
| 84 | + |
| 85 | + state.pauseQueue.delete(onPause) |
| 86 | + state.timeouts.delete(timeout) |
| 87 | + |
| 88 | + // Maybe cancelled during its delay. |
| 89 | + if (callId <= (state.cancelId || 0)) { |
| 90 | + cancel = true |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + actions.start({ ...props, callId, cancel }, resolve) |
| 95 | + } catch (err) { |
| 96 | + reject(err) |
| 97 | + } |
| 98 | + } |
| 99 | + }) |
| 100 | +} |
0 commit comments