| | import { OneOrMore, UnknownProps, Lookup, Falsy } from '@react-spring/types' |
| | import { |
| | is, |
| | raf, |
| | each, |
| | noop, |
| | flush, |
| | toArray, |
| | eachProp, |
| | flushCalls, |
| | addFluidObserver, |
| | FluidObserver, |
| | } from '@react-spring/shared' |
| |
|
| | import { getDefaultProp } from './helpers' |
| | import { FrameValue } from './FrameValue' |
| | import type { SpringRef } from './SpringRef' |
| | import { SpringValue, createLoopUpdate, createUpdate } from './SpringValue' |
| | import { getCancelledResult, getCombinedResult } from './AnimationResult' |
| | import { runAsync, RunAsyncState, stopAsync } from './runAsync' |
| | import { scheduleProps } from './scheduleProps' |
| | import { |
| | AnimationResult, |
| | AsyncResult, |
| | ControllerFlushFn, |
| | ControllerUpdate, |
| | OnChange, |
| | OnRest, |
| | OnStart, |
| | SpringChain, |
| | SpringToFn, |
| | SpringValues, |
| | } from './types' |
| |
|
| | |
| | const BATCHED_EVENTS = ['onStart', 'onChange', 'onRest'] as const |
| |
|
| | let nextId = 1 |
| |
|
| | |
| | export interface ControllerQueue<State extends Lookup = Lookup> |
| | extends Array< |
| | ControllerUpdate<State, any> & { |
| | |
| | keys: string[] | null |
| | } |
| | > {} |
| |
|
| | export class Controller<State extends Lookup = Lookup> { |
| | readonly id = nextId++ |
| |
|
| | |
| | springs: SpringValues<State> = {} as any |
| |
|
| | |
| | queue: ControllerQueue<State> = [] |
| |
|
| | |
| | |
| | |
| | |
| | ref?: SpringRef<State> |
| |
|
| | |
| | protected _flush?: ControllerFlushFn<this> |
| |
|
| | |
| | protected _initialProps?: Lookup |
| |
|
| | |
| | protected _lastAsyncId = 0 |
| |
|
| | |
| | protected _active = new Set<FrameValue>() |
| |
|
| | |
| | protected _changed = new Set<FrameValue>() |
| |
|
| | |
| | protected _started = false |
| |
|
| | private _item?: any |
| |
|
| | |
| | protected _state: RunAsyncState<this> = { |
| | paused: false, |
| | pauseQueue: new Set(), |
| | resumeQueue: new Set(), |
| | timeouts: new Set(), |
| | } |
| |
|
| | |
| | protected _events = { |
| | onStart: new Map< |
| | OnStart<SpringValue<State>, Controller<State>, any>, |
| | AnimationResult |
| | >(), |
| | onChange: new Map< |
| | OnChange<SpringValue<State>, Controller<State>, any>, |
| | AnimationResult |
| | >(), |
| | onRest: new Map< |
| | OnRest<SpringValue<State>, Controller<State>, any>, |
| | AnimationResult |
| | >(), |
| | } |
| |
|
| | constructor( |
| | props?: ControllerUpdate<State> | null, |
| | flush?: ControllerFlushFn<any> |
| | ) { |
| | this._onFrame = this._onFrame.bind(this) |
| | if (flush) { |
| | this._flush = flush |
| | } |
| | if (props) { |
| | this.start({ default: true, ...props }) |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | get idle() { |
| | return ( |
| | !this._state.asyncTo && |
| | Object.values(this.springs as Lookup<SpringValue>).every(spring => { |
| | return spring.idle && !spring.isDelayed && !spring.isPaused |
| | }) |
| | ) |
| | } |
| |
|
| | get item() { |
| | return this._item |
| | } |
| |
|
| | set item(item) { |
| | this._item = item |
| | } |
| |
|
| | |
| | get(): State & UnknownProps { |
| | const values: any = {} |
| | this.each((spring, key) => (values[key] = spring.get())) |
| | return values |
| | } |
| |
|
| | |
| | set(values: Partial<State>) { |
| | for (const key in values) { |
| | const value = values[key] |
| | if (!is.und(value)) { |
| | this.springs[key].set(value) |
| | } |
| | } |
| | } |
| |
|
| | |
| | update(props: ControllerUpdate<State> | Falsy) { |
| | if (props) { |
| | this.queue.push(createUpdate(props)) |
| | } |
| | return this |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | start(props?: OneOrMore<ControllerUpdate<State>> | null): AsyncResult<this> { |
| | let { queue } = this as any |
| | if (props) { |
| | queue = toArray<any>(props).map(createUpdate) |
| | } else { |
| | this.queue = [] |
| | } |
| |
|
| | if (this._flush) { |
| | return this._flush(this, queue) |
| | } |
| |
|
| | prepareKeys(this, queue) |
| | return flushUpdateQueue(this, queue) |
| | } |
| |
|
| | |
| | stop(): this |
| | |
| | stop(keys: OneOrMore<string>): this |
| | |
| | stop(cancel: boolean): this |
| | |
| | stop(cancel: boolean, keys: OneOrMore<string>): this |
| | |
| | stop(keys?: OneOrMore<string>): this |
| | |
| | stop(cancel: boolean, keys?: OneOrMore<string>): this |
| | |
| | stop(arg?: boolean | OneOrMore<string>, keys?: OneOrMore<string>) { |
| | if (arg !== !!arg) { |
| | keys = arg as OneOrMore<string> |
| | } |
| | if (keys) { |
| | const springs = this.springs as Lookup<SpringValue> |
| | each(toArray(keys) as string[], key => springs[key].stop(!!arg)) |
| | } else { |
| | stopAsync(this._state, this._lastAsyncId) |
| | this.each(spring => spring.stop(!!arg)) |
| | } |
| | return this |
| | } |
| |
|
| | |
| | pause(keys?: OneOrMore<string>) { |
| | if (is.und(keys)) { |
| | this.start({ pause: true }) |
| | } else { |
| | const springs = this.springs as Lookup<SpringValue> |
| | each(toArray(keys) as string[], key => springs[key].pause()) |
| | } |
| | return this |
| | } |
| |
|
| | |
| | resume(keys?: OneOrMore<string>) { |
| | if (is.und(keys)) { |
| | this.start({ pause: false }) |
| | } else { |
| | const springs = this.springs as Lookup<SpringValue> |
| | each(toArray(keys) as string[], key => springs[key].resume()) |
| | } |
| | return this |
| | } |
| |
|
| | |
| | each(iterator: (spring: SpringValue, key: string) => void) { |
| | eachProp(this.springs, iterator as any) |
| | } |
| |
|
| | |
| | protected _onFrame() { |
| | const { onStart, onChange, onRest } = this._events |
| |
|
| | const active = this._active.size > 0 |
| | const changed = this._changed.size > 0 |
| |
|
| | if ((active && !this._started) || (changed && !this._started)) { |
| | this._started = true |
| | flush(onStart, ([onStart, result]) => { |
| | result.value = this.get() |
| | onStart(result, this, this._item) |
| | }) |
| | } |
| |
|
| | const idle = !active && this._started |
| | const values = changed || (idle && onRest.size) ? this.get() : null |
| |
|
| | if (changed && onChange.size) { |
| | flush(onChange, ([onChange, result]) => { |
| | result.value = values |
| | onChange(result, this, this._item) |
| | }) |
| | } |
| |
|
| | |
| | if (idle) { |
| | this._started = false |
| | flush(onRest, ([onRest, result]) => { |
| | result.value = values |
| | onRest(result, this, this._item) |
| | }) |
| | } |
| | } |
| |
|
| | |
| | eventObserved(event: FrameValue.Event) { |
| | if (event.type == 'change') { |
| | this._changed.add(event.parent) |
| | if (!event.idle) { |
| | this._active.add(event.parent) |
| | } |
| | } else if (event.type == 'idle') { |
| | this._active.delete(event.parent) |
| | } |
| | |
| | else return |
| | raf.onFrame(this._onFrame) |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | export function flushUpdateQueue( |
| | ctrl: Controller<any>, |
| | queue: ControllerQueue |
| | ) { |
| | return Promise.all(queue.map(props => flushUpdate(ctrl, props))).then( |
| | results => getCombinedResult(ctrl, results) |
| | ) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export async function flushUpdate( |
| | ctrl: Controller<any>, |
| | props: ControllerQueue[number], |
| | isLoop?: boolean |
| | ): AsyncResult { |
| | const { keys, to, from, loop, onRest, onResolve } = props |
| | const defaults = is.obj(props.default) && props.default |
| |
|
| | |
| | |
| | if (loop) { |
| | props.loop = false |
| | } |
| |
|
| | |
| | if (to === false) props.to = null |
| | if (from === false) props.from = null |
| |
|
| | const asyncTo = is.arr(to) || is.fun(to) ? to : undefined |
| | if (asyncTo) { |
| | props.to = undefined |
| | props.onRest = undefined |
| | if (defaults) { |
| | defaults.onRest = undefined |
| | } |
| | } |
| | |
| | |
| | |
| | else { |
| | each(BATCHED_EVENTS, key => { |
| | const handler: any = props[key] |
| | if (is.fun(handler)) { |
| | const queue = ctrl['_events'][key] |
| | props[key] = (({ finished, cancelled }: AnimationResult) => { |
| | const result = queue.get(handler) |
| | if (result) { |
| | if (!finished) result.finished = false |
| | if (cancelled) result.cancelled = true |
| | } else { |
| | |
| | queue.set(handler, { |
| | value: null, |
| | finished: finished || false, |
| | cancelled: cancelled || false, |
| | }) |
| | } |
| | }) as any |
| |
|
| | |
| | if (defaults) { |
| | defaults[key] = props[key] as any |
| | } |
| | } |
| | }) |
| | } |
| |
|
| | const state = ctrl['_state'] |
| |
|
| | |
| | if (props.pause === !state.paused) { |
| | state.paused = props.pause |
| | flushCalls(props.pause ? state.pauseQueue : state.resumeQueue) |
| | } |
| | |
| | else if (state.paused) { |
| | props.pause = true |
| | } |
| |
|
| | const promises: AsyncResult[] = (keys || Object.keys(ctrl.springs)).map(key => |
| | ctrl.springs[key]!.start(props as any) |
| | ) |
| |
|
| | const cancel = |
| | props.cancel === true || getDefaultProp(props, 'cancel') === true |
| |
|
| | if (asyncTo || (cancel && state.asyncId)) { |
| | promises.push( |
| | scheduleProps(++ctrl['_lastAsyncId'], { |
| | props, |
| | state, |
| | actions: { |
| | pause: noop, |
| | resume: noop, |
| | start(props, resolve) { |
| | if (cancel) { |
| | stopAsync(state, ctrl['_lastAsyncId']) |
| | resolve(getCancelledResult(ctrl)) |
| | } else { |
| | props.onRest = onRest |
| | resolve( |
| | runAsync( |
| | asyncTo as SpringChain | SpringToFn, |
| | props, |
| | state, |
| | ctrl |
| | ) |
| | ) |
| | } |
| | }, |
| | }, |
| | }) |
| | ) |
| | } |
| |
|
| | |
| | |
| | if (state.paused) { |
| | |
| | |
| | await new Promise<void>(resume => { |
| | state.resumeQueue.add(resume) |
| | }) |
| | } |
| |
|
| | const result = getCombinedResult<any>(ctrl, await Promise.all(promises)) |
| | if (loop && result.finished && !(isLoop && result.noop)) { |
| | const nextProps = createLoopUpdate(props, loop, to) |
| | if (nextProps) { |
| | prepareKeys(ctrl, [nextProps]) |
| | return flushUpdate(ctrl, nextProps, true) |
| | } |
| | } |
| | if (onResolve) { |
| | raf.batchedUpdates(() => onResolve(result, ctrl, ctrl.item)) |
| | } |
| | return result |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | export function getSprings<State extends Lookup>( |
| | ctrl: Controller<Lookup<any>>, |
| | props?: OneOrMore<ControllerUpdate<State>> |
| | ) { |
| | const springs = { ...ctrl.springs } |
| | if (props) { |
| | each(toArray(props), (props: any) => { |
| | if (is.und(props.keys)) { |
| | props = createUpdate(props) |
| | } |
| | if (!is.obj(props.to)) { |
| | |
| | props = { ...props, to: undefined } |
| | } |
| | prepareSprings(springs as any, props, key => { |
| | return createSpring(key) |
| | }) |
| | }) |
| | } |
| | setSprings(ctrl, springs) |
| | return springs |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function setSprings( |
| | ctrl: Controller<Lookup<any>>, |
| | springs: SpringValues<UnknownProps> |
| | ) { |
| | eachProp(springs, (spring, key) => { |
| | if (!ctrl.springs[key]) { |
| | ctrl.springs[key] = spring |
| | addFluidObserver(spring, ctrl) |
| | } |
| | }) |
| | } |
| |
|
| | function createSpring(key: string, observer?: FluidObserver<FrameValue.Event>) { |
| | const spring = new SpringValue() |
| | spring.key = key |
| | if (observer) { |
| | addFluidObserver(spring, observer) |
| | } |
| | return spring |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | function prepareSprings( |
| | springs: SpringValues, |
| | props: ControllerQueue[number], |
| | create: (key: string) => SpringValue |
| | ) { |
| | if (props.keys) { |
| | each(props.keys, key => { |
| | const spring = springs[key] || (springs[key] = create(key)) |
| | spring['_prepareNode'](props) |
| | }) |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | function prepareKeys(ctrl: Controller<any>, queue: ControllerQueue[number][]) { |
| | each(queue, props => { |
| | prepareSprings(ctrl.springs, props, key => { |
| | return createSpring(key, ctrl) |
| | }) |
| | }) |
| | } |
| |
|