|
|
import { Timeout, is, raf, Globals as G } from '@react-spring/shared' |
|
|
import { matchProp, callProp } from './helpers' |
|
|
import { AsyncResult, MatchProp } from './types' |
|
|
import { RunAsyncState, RunAsyncProps } from './runAsync' |
|
|
import { |
|
|
AnimationResolver, |
|
|
AnimationTarget, |
|
|
InferProps, |
|
|
InferState, |
|
|
} from './types/internal' |
|
|
|
|
|
|
|
|
type DefaultProps<T> = { cancel?: MatchProp<T>; pause?: MatchProp<T> } |
|
|
|
|
|
interface ScheduledProps<T extends AnimationTarget> { |
|
|
key?: string |
|
|
props: InferProps<T> |
|
|
defaultProps?: DefaultProps<InferState<T>> |
|
|
state: RunAsyncState<T> |
|
|
actions: { |
|
|
pause: () => void |
|
|
resume: () => void |
|
|
start: (props: RunAsyncProps<T>, resolve: AnimationResolver<T>) => void |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function scheduleProps<T extends AnimationTarget>( |
|
|
callId: number, |
|
|
{ key, props, defaultProps, state, actions }: ScheduledProps<T> |
|
|
): AsyncResult<T> { |
|
|
return new Promise((resolve, reject) => { |
|
|
let delay: number |
|
|
let timeout: Timeout |
|
|
|
|
|
let cancel = matchProp(props.cancel ?? defaultProps?.cancel, key) |
|
|
if (cancel) { |
|
|
onStart() |
|
|
} else { |
|
|
|
|
|
if (!is.und(props.pause)) { |
|
|
state.paused = matchProp(props.pause, key) |
|
|
} |
|
|
|
|
|
|
|
|
let pause = defaultProps?.pause |
|
|
if (pause !== true) { |
|
|
pause = state.paused || matchProp(pause, key) |
|
|
} |
|
|
|
|
|
delay = callProp(props.delay || 0, key) |
|
|
if (pause) { |
|
|
state.resumeQueue.add(onResume) |
|
|
actions.pause() |
|
|
} else { |
|
|
actions.resume() |
|
|
onResume() |
|
|
} |
|
|
} |
|
|
|
|
|
function onPause() { |
|
|
state.resumeQueue.add(onResume) |
|
|
state.timeouts.delete(timeout) |
|
|
timeout.cancel() |
|
|
|
|
|
delay = timeout.time - raf.now() |
|
|
} |
|
|
|
|
|
function onResume() { |
|
|
if (delay > 0 && !G.skipAnimation) { |
|
|
state.delayed = true |
|
|
timeout = raf.setTimeout(onStart, delay) |
|
|
state.pauseQueue.add(onPause) |
|
|
state.timeouts.add(timeout) |
|
|
} else { |
|
|
onStart() |
|
|
} |
|
|
} |
|
|
|
|
|
function onStart() { |
|
|
if (state.delayed) { |
|
|
state.delayed = false |
|
|
} |
|
|
|
|
|
state.pauseQueue.delete(onPause) |
|
|
state.timeouts.delete(timeout) |
|
|
|
|
|
|
|
|
if (callId <= (state.cancelId || 0)) { |
|
|
cancel = true |
|
|
} |
|
|
|
|
|
try { |
|
|
actions.start({ ...props, callId, cancel }, resolve) |
|
|
} catch (err) { |
|
|
reject(err) |
|
|
} |
|
|
} |
|
|
}) |
|
|
} |
|
|
|