| | import { |
| | is, |
| | raf, |
| | flush, |
| | eachProp, |
| | Timeout, |
| | Globals as G, |
| | } from '@react-spring/shared' |
| | import { Falsy } from '@react-spring/types' |
| |
|
| | import { getDefaultProps } from './helpers' |
| | import { AnimationTarget, InferState, InferProps } from './types/internal' |
| | import { AnimationResult, AsyncResult, SpringChain, SpringToFn } from './types' |
| | import { getCancelledResult, getFinishedResult } from './AnimationResult' |
| |
|
| | type AsyncTo<T> = SpringChain<T> | SpringToFn<T> |
| |
|
| | |
| | export type RunAsyncProps<T extends AnimationTarget = any> = InferProps<T> & { |
| | callId: number |
| | parentId?: number |
| | cancel: boolean |
| | to?: any |
| | } |
| |
|
| | |
| | export interface RunAsyncState<T extends AnimationTarget = any> { |
| | paused: boolean |
| | pauseQueue: Set<() => void> |
| | resumeQueue: Set<() => void> |
| | timeouts: Set<Timeout> |
| | delayed?: boolean |
| | asyncId?: number |
| | asyncTo?: AsyncTo<InferState<T>> |
| | promise?: AsyncResult<T> |
| | cancelId?: number |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export function runAsync<T extends AnimationTarget>( |
| | to: AsyncTo<InferState<T>>, |
| | props: RunAsyncProps<T>, |
| | state: RunAsyncState<T>, |
| | target: T |
| | ): AsyncResult<T> { |
| | const { callId, parentId, onRest } = props |
| | const { asyncTo: prevTo, promise: prevPromise } = state |
| |
|
| | if (!parentId && to === prevTo && !props.reset) { |
| | return prevPromise! |
| | } |
| |
|
| | return (state.promise = (async () => { |
| | state.asyncId = callId |
| | state.asyncTo = to |
| |
|
| | |
| | const defaultProps = getDefaultProps<InferProps<T>>(props, (value, key) => |
| | |
| | key === 'onRest' ? undefined : value |
| | ) |
| |
|
| | let preventBail!: () => void |
| | let bail: (error: any) => void |
| |
|
| | |
| | const bailPromise = new Promise<void>( |
| | (resolve, reject) => ((preventBail = resolve), (bail = reject)) |
| | ) |
| |
|
| | const bailIfEnded = (bailSignal: BailSignal) => { |
| | const bailResult = |
| | |
| | (callId <= (state.cancelId || 0) && getCancelledResult(target)) || |
| | |
| | (callId !== state.asyncId && getFinishedResult(target, false)) |
| |
|
| | if (bailResult) { |
| | bailSignal.result = bailResult |
| |
|
| | |
| | |
| | bail(bailSignal) |
| | throw bailSignal |
| | } |
| | } |
| |
|
| | const animate: any = (arg1: any, arg2?: any) => { |
| | |
| | |
| | const bailSignal = new BailSignal() |
| | const skipAnimationSignal = new SkipAnimationSignal() |
| |
|
| | return (async () => { |
| | if (G.skipAnimation) { |
| | |
| | |
| | |
| | |
| | |
| | stopAsync(state) |
| |
|
| | |
| | skipAnimationSignal.result = getFinishedResult(target, false) |
| | bail(skipAnimationSignal) |
| | throw skipAnimationSignal |
| | } |
| |
|
| | bailIfEnded(bailSignal) |
| |
|
| | const props: any = is.obj(arg1) ? { ...arg1 } : { ...arg2, to: arg1 } |
| | props.parentId = callId |
| |
|
| | eachProp(defaultProps, (value, key) => { |
| | if (is.und(props[key])) { |
| | props[key] = value |
| | } |
| | }) |
| |
|
| | const result = await target.start(props) |
| | bailIfEnded(bailSignal) |
| |
|
| | if (state.paused) { |
| | await new Promise<void>(resume => { |
| | state.resumeQueue.add(resume) |
| | }) |
| | } |
| |
|
| | return result |
| | })() |
| | } |
| |
|
| | let result!: AnimationResult<T> |
| |
|
| | if (G.skipAnimation) { |
| | |
| | |
| | |
| | |
| | stopAsync(state) |
| | return getFinishedResult(target, false) |
| | } |
| |
|
| | try { |
| | let animating!: Promise<void> |
| |
|
| | |
| | if (is.arr(to)) { |
| | animating = (async (queue: any[]) => { |
| | for (const props of queue) { |
| | await animate(props) |
| | } |
| | })(to) |
| | } |
| |
|
| | |
| | else { |
| | animating = Promise.resolve(to(animate, target.stop.bind(target))) |
| | } |
| |
|
| | await Promise.all([animating.then(preventBail), bailPromise]) |
| | result = getFinishedResult(target.get(), true, false) |
| |
|
| | |
| | } catch (err) { |
| | if (err instanceof BailSignal) { |
| | result = err.result |
| | } else if (err instanceof SkipAnimationSignal) { |
| | result = err.result |
| | } else { |
| | throw err |
| | } |
| |
|
| | |
| | } finally { |
| | if (callId == state.asyncId) { |
| | state.asyncId = parentId |
| | state.asyncTo = parentId ? prevTo : undefined |
| | state.promise = parentId ? prevPromise : undefined |
| | } |
| | } |
| |
|
| | if (is.fun(onRest)) { |
| | raf.batchedUpdates(() => { |
| | onRest(result, target, target.item) |
| | }) |
| | } |
| |
|
| | return result |
| | })()) |
| | } |
| |
|
| | |
| | export function stopAsync(state: RunAsyncState, cancelId?: number | Falsy) { |
| | flush(state.timeouts, t => t.cancel()) |
| | state.pauseQueue.clear() |
| | state.resumeQueue.clear() |
| | state.asyncId = state.asyncTo = state.promise = undefined |
| | if (cancelId) state.cancelId = cancelId |
| | } |
| |
|
| | |
| | export class BailSignal extends Error { |
| | result!: AnimationResult |
| | constructor() { |
| | super( |
| | 'An async animation has been interrupted. You see this error because you ' + |
| | 'forgot to use `await` or `.catch(...)` on its returned promise.' |
| | ) |
| | } |
| | } |
| |
|
| | export class SkipAnimationSignal extends Error { |
| | result!: AnimationResult |
| |
|
| | constructor() { |
| | super('SkipAnimationSignal') |
| | } |
| | } |
| |
|