|
|
import { |
|
|
is, |
|
|
raf, |
|
|
each, |
|
|
isEqual, |
|
|
toArray, |
|
|
eachProp, |
|
|
frameLoop, |
|
|
flushCalls, |
|
|
getFluidValue, |
|
|
isAnimatedString, |
|
|
FluidValue, |
|
|
Globals as G, |
|
|
callFluidObservers, |
|
|
hasFluidValue, |
|
|
addFluidObserver, |
|
|
removeFluidObserver, |
|
|
getFluidObservers, |
|
|
} from '@react-spring/shared' |
|
|
import { |
|
|
Animated, |
|
|
AnimatedValue, |
|
|
AnimatedString, |
|
|
getPayload, |
|
|
getAnimated, |
|
|
setAnimated, |
|
|
getAnimatedType, |
|
|
} from '@react-spring/animated' |
|
|
import { Lookup } from '@react-spring/types' |
|
|
|
|
|
import { Animation } from './Animation' |
|
|
import { mergeConfig } from './AnimationConfig' |
|
|
import { scheduleProps } from './scheduleProps' |
|
|
import { runAsync, RunAsyncState, RunAsyncProps, stopAsync } from './runAsync' |
|
|
import { |
|
|
callProp, |
|
|
computeGoal, |
|
|
matchProp, |
|
|
inferTo, |
|
|
getDefaultProps, |
|
|
getDefaultProp, |
|
|
isAsyncTo, |
|
|
resolveProp, |
|
|
} from './helpers' |
|
|
import { FrameValue, isFrameValue } from './FrameValue' |
|
|
import { |
|
|
isAnimating, |
|
|
isPaused, |
|
|
setPausedBit, |
|
|
hasAnimated, |
|
|
setActiveBit, |
|
|
} from './SpringPhase' |
|
|
import { |
|
|
AnimationRange, |
|
|
AnimationResolver, |
|
|
EventKey, |
|
|
PickEventFns, |
|
|
} from './types/internal' |
|
|
import { AsyncResult, SpringUpdate, VelocityProp, SpringProps } from './types' |
|
|
import { |
|
|
getCombinedResult, |
|
|
getCancelledResult, |
|
|
getFinishedResult, |
|
|
getNoopResult, |
|
|
} from './AnimationResult' |
|
|
|
|
|
declare const console: any |
|
|
|
|
|
interface DefaultSpringProps<T> |
|
|
extends Pick<SpringProps<T>, 'pause' | 'cancel' | 'immediate' | 'config'>, |
|
|
PickEventFns<SpringProps<T>> {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class SpringValue<T = any> extends FrameValue<T> { |
|
|
|
|
|
key?: string |
|
|
|
|
|
|
|
|
animation = new Animation<T>() |
|
|
|
|
|
|
|
|
queue?: SpringUpdate<T>[] |
|
|
|
|
|
|
|
|
defaultProps: DefaultSpringProps<T> = {} |
|
|
|
|
|
|
|
|
protected _state: RunAsyncState<SpringValue<T>> = { |
|
|
paused: false, |
|
|
delayed: false, |
|
|
pauseQueue: new Set(), |
|
|
resumeQueue: new Set(), |
|
|
timeouts: new Set(), |
|
|
} |
|
|
|
|
|
|
|
|
protected _pendingCalls = new Set<AnimationResolver<this>>() |
|
|
|
|
|
|
|
|
protected _lastCallId = 0 |
|
|
|
|
|
|
|
|
protected _lastToId = 0 |
|
|
|
|
|
protected _memoizedDuration = 0 |
|
|
|
|
|
constructor(from: Exclude<T, object>, props?: SpringUpdate<T>) |
|
|
constructor(props?: SpringUpdate<T>) |
|
|
constructor(arg1?: any, arg2?: any) { |
|
|
super() |
|
|
if (!is.und(arg1) || !is.und(arg2)) { |
|
|
const props = is.obj(arg1) ? { ...arg1 } : { ...arg2, from: arg1 } |
|
|
if (is.und(props.default)) { |
|
|
props.default = true |
|
|
} |
|
|
this.start(props) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
get idle() { |
|
|
return !(isAnimating(this) || this._state.asyncTo) || isPaused(this) |
|
|
} |
|
|
|
|
|
get goal() { |
|
|
return getFluidValue(this.animation.to) as T |
|
|
} |
|
|
|
|
|
get velocity(): VelocityProp<T> { |
|
|
const node = getAnimated(this)! |
|
|
return ( |
|
|
node instanceof AnimatedValue |
|
|
? node.lastVelocity || 0 |
|
|
: node.getPayload().map(node => node.lastVelocity || 0) |
|
|
) as any |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get hasAnimated() { |
|
|
return hasAnimated(this) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get isAnimating() { |
|
|
return isAnimating(this) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get isPaused() { |
|
|
return isPaused(this) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get isDelayed() { |
|
|
return this._state.delayed |
|
|
} |
|
|
|
|
|
|
|
|
advance(dt: number) { |
|
|
let idle = true |
|
|
let changed = false |
|
|
|
|
|
const anim = this.animation |
|
|
let { toValues } = anim |
|
|
const { config } = anim |
|
|
|
|
|
const payload = getPayload(anim.to) |
|
|
if (!payload && hasFluidValue(anim.to)) { |
|
|
toValues = toArray(getFluidValue(anim.to)) as any |
|
|
} |
|
|
|
|
|
anim.values.forEach((node, i) => { |
|
|
if (node.done) return |
|
|
|
|
|
const to = |
|
|
|
|
|
node.constructor == AnimatedString |
|
|
? 1 |
|
|
: payload |
|
|
? payload[i].lastPosition |
|
|
: toValues![i] |
|
|
|
|
|
let finished = anim.immediate |
|
|
let position = to |
|
|
|
|
|
if (!finished) { |
|
|
position = node.lastPosition |
|
|
|
|
|
|
|
|
if (config.tension <= 0) { |
|
|
node.done = true |
|
|
return |
|
|
} |
|
|
|
|
|
let elapsed = (node.elapsedTime += dt) |
|
|
const from = anim.fromValues[i] |
|
|
|
|
|
const v0 = |
|
|
node.v0 != null |
|
|
? node.v0 |
|
|
: (node.v0 = is.arr(config.velocity) |
|
|
? config.velocity[i] |
|
|
: config.velocity) |
|
|
|
|
|
let velocity: number |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const precision = |
|
|
config.precision || |
|
|
(from == to ? 0.005 : Math.min(1, Math.abs(to - from) * 0.001)) |
|
|
|
|
|
|
|
|
if (!is.und(config.duration)) { |
|
|
let p = 1 |
|
|
if (config.duration > 0) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this._memoizedDuration !== config.duration) { |
|
|
|
|
|
this._memoizedDuration = config.duration |
|
|
|
|
|
|
|
|
if (node.durationProgress > 0) { |
|
|
|
|
|
node.elapsedTime = config.duration * node.durationProgress |
|
|
|
|
|
elapsed = node.elapsedTime += dt |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
p = (config.progress || 0) + elapsed / this._memoizedDuration |
|
|
|
|
|
p = p > 1 ? 1 : p < 0 ? 0 : p |
|
|
|
|
|
node.durationProgress = p |
|
|
} |
|
|
|
|
|
position = from + config.easing(p) * (to - from) |
|
|
velocity = (position - node.lastPosition) / dt |
|
|
|
|
|
finished = p == 1 |
|
|
} |
|
|
|
|
|
|
|
|
else if (config.decay) { |
|
|
const decay = config.decay === true ? 0.998 : config.decay |
|
|
const e = Math.exp(-(1 - decay) * elapsed) |
|
|
|
|
|
position = from + (v0 / (1 - decay)) * (1 - e) |
|
|
finished = Math.abs(node.lastPosition - position) <= precision |
|
|
|
|
|
|
|
|
velocity = v0 * e |
|
|
} |
|
|
|
|
|
|
|
|
else { |
|
|
velocity = node.lastVelocity == null ? v0 : node.lastVelocity |
|
|
|
|
|
|
|
|
const restVelocity = config.restVelocity || precision / 10 |
|
|
|
|
|
|
|
|
const bounceFactor = config.clamp ? 0 : config.bounce! |
|
|
const canBounce = !is.und(bounceFactor) |
|
|
|
|
|
|
|
|
const isGrowing = from == to ? node.v0 > 0 : from < to |
|
|
|
|
|
|
|
|
let isMoving!: boolean |
|
|
|
|
|
|
|
|
let isBouncing = false |
|
|
|
|
|
const step = 1 |
|
|
const numSteps = Math.ceil(dt / step) |
|
|
for (let n = 0; n < numSteps; ++n) { |
|
|
isMoving = Math.abs(velocity) > restVelocity |
|
|
|
|
|
if (!isMoving) { |
|
|
finished = Math.abs(to - position) <= precision |
|
|
if (finished) { |
|
|
break |
|
|
} |
|
|
} |
|
|
|
|
|
if (canBounce) { |
|
|
isBouncing = position == to || position > to == isGrowing |
|
|
|
|
|
|
|
|
if (isBouncing) { |
|
|
velocity = -velocity * bounceFactor |
|
|
position = to |
|
|
} |
|
|
} |
|
|
|
|
|
const springForce = -config.tension * 0.000001 * (position - to) |
|
|
const dampingForce = -config.friction * 0.001 * velocity |
|
|
const acceleration = (springForce + dampingForce) / config.mass |
|
|
|
|
|
velocity = velocity + acceleration * step |
|
|
position = position + velocity * step |
|
|
} |
|
|
} |
|
|
|
|
|
node.lastVelocity = velocity |
|
|
|
|
|
if (Number.isNaN(position)) { |
|
|
console.warn(`Got NaN while animating:`, this) |
|
|
finished = true |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (payload && !payload[i].done) { |
|
|
finished = false |
|
|
} |
|
|
|
|
|
if (finished) { |
|
|
node.done = true |
|
|
} else { |
|
|
idle = false |
|
|
} |
|
|
|
|
|
if (node.setValue(position, config.round)) { |
|
|
changed = true |
|
|
} |
|
|
}) |
|
|
|
|
|
const node = getAnimated(this)! |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const currVal = node.getValue() |
|
|
if (idle) { |
|
|
|
|
|
const finalVal = getFluidValue(anim.to) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((currVal !== finalVal || changed) && !config.decay) { |
|
|
|
|
|
node.setValue(finalVal) |
|
|
this._onChange(finalVal) |
|
|
} else if (changed && config.decay) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._onChange(currVal) |
|
|
} |
|
|
|
|
|
this._stop() |
|
|
} else if (changed) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._onChange(currVal) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
set(value: T | FluidValue<T>) { |
|
|
raf.batchedUpdates(() => { |
|
|
this._stop() |
|
|
|
|
|
|
|
|
|
|
|
this._focus(value) |
|
|
this._set(value) |
|
|
}) |
|
|
return this |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pause() { |
|
|
this._update({ pause: true }) |
|
|
} |
|
|
|
|
|
|
|
|
resume() { |
|
|
this._update({ pause: false }) |
|
|
} |
|
|
|
|
|
|
|
|
finish() { |
|
|
if (isAnimating(this)) { |
|
|
const { to, config } = this.animation |
|
|
raf.batchedUpdates(() => { |
|
|
|
|
|
this._onStart() |
|
|
|
|
|
|
|
|
|
|
|
if (!config.decay) { |
|
|
this._set(to, false) |
|
|
} |
|
|
|
|
|
this._stop() |
|
|
}) |
|
|
} |
|
|
return this |
|
|
} |
|
|
|
|
|
|
|
|
update(props: SpringUpdate<T>) { |
|
|
const queue = this.queue || (this.queue = []) |
|
|
queue.push(props) |
|
|
return this |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
start(): AsyncResult<this> |
|
|
|
|
|
start(props: SpringUpdate<T>): AsyncResult<this> |
|
|
|
|
|
start(to: T, props?: SpringProps<T>): AsyncResult<this> |
|
|
|
|
|
start(to?: any, arg2?: any) { |
|
|
let queue: SpringUpdate<T>[] |
|
|
if (!is.und(to)) { |
|
|
queue = [is.obj(to) ? to : { ...arg2, to }] |
|
|
} else { |
|
|
queue = this.queue || [] |
|
|
this.queue = [] |
|
|
} |
|
|
|
|
|
return Promise.all( |
|
|
queue.map(props => { |
|
|
const up = this._update(props) |
|
|
return up |
|
|
}) |
|
|
).then(results => getCombinedResult(this, results)) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stop(cancel?: boolean) { |
|
|
const { to } = this.animation |
|
|
|
|
|
|
|
|
this._focus(this.get()) |
|
|
|
|
|
stopAsync(this._state, cancel && this._lastCallId) |
|
|
raf.batchedUpdates(() => this._stop(to, cancel)) |
|
|
|
|
|
return this |
|
|
} |
|
|
|
|
|
|
|
|
reset() { |
|
|
this._update({ reset: true }) |
|
|
} |
|
|
|
|
|
|
|
|
eventObserved(event: FrameValue.Event) { |
|
|
if (event.type == 'change') { |
|
|
this._start() |
|
|
} else if (event.type == 'priority') { |
|
|
this.priority = event.priority + 1 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected _prepareNode(props: { |
|
|
to?: any |
|
|
from?: any |
|
|
reverse?: boolean |
|
|
default?: any |
|
|
}) { |
|
|
const key = this.key || '' |
|
|
|
|
|
let { to, from } = props |
|
|
|
|
|
to = is.obj(to) ? to[key] : to |
|
|
if (to == null || isAsyncTo(to)) { |
|
|
to = undefined |
|
|
} |
|
|
|
|
|
from = is.obj(from) ? from[key] : from |
|
|
if (from == null) { |
|
|
from = undefined |
|
|
} |
|
|
|
|
|
|
|
|
const range = { to, from } |
|
|
|
|
|
|
|
|
|
|
|
if (!hasAnimated(this)) { |
|
|
if (props.reverse) [to, from] = [from, to] |
|
|
|
|
|
from = getFluidValue(from) |
|
|
if (!is.und(from)) { |
|
|
this._set(from) |
|
|
} |
|
|
|
|
|
else if (!getAnimated(this)) { |
|
|
this._set(to) |
|
|
} |
|
|
} |
|
|
|
|
|
return range |
|
|
} |
|
|
|
|
|
|
|
|
protected _update( |
|
|
{ ...props }: SpringProps<T>, |
|
|
isLoop?: boolean |
|
|
): AsyncResult<SpringValue<T>> { |
|
|
const { key, defaultProps } = this |
|
|
|
|
|
|
|
|
if (props.default) |
|
|
Object.assign( |
|
|
defaultProps, |
|
|
getDefaultProps(props, (value, prop) => |
|
|
/^on/.test(prop) ? resolveProp(value, key) : value |
|
|
) |
|
|
) |
|
|
|
|
|
mergeActiveFn(this, props, 'onProps') |
|
|
sendEvent(this, 'onProps', props, this) |
|
|
|
|
|
|
|
|
const range = this._prepareNode(props) |
|
|
|
|
|
if (Object.isFrozen(this)) { |
|
|
throw Error( |
|
|
'Cannot animate a `SpringValue` object that is frozen. ' + |
|
|
'Did you forget to pass your component to `animated(...)` before animating its props?' |
|
|
) |
|
|
} |
|
|
|
|
|
const state = this._state |
|
|
|
|
|
return scheduleProps(++this._lastCallId, { |
|
|
key, |
|
|
props, |
|
|
defaultProps, |
|
|
state, |
|
|
actions: { |
|
|
pause: () => { |
|
|
if (!isPaused(this)) { |
|
|
setPausedBit(this, true) |
|
|
flushCalls(state.pauseQueue) |
|
|
sendEvent( |
|
|
this, |
|
|
'onPause', |
|
|
getFinishedResult(this, checkFinished(this, this.animation.to)), |
|
|
this |
|
|
) |
|
|
} |
|
|
}, |
|
|
resume: () => { |
|
|
if (isPaused(this)) { |
|
|
setPausedBit(this, false) |
|
|
if (isAnimating(this)) { |
|
|
this._resume() |
|
|
} |
|
|
flushCalls(state.resumeQueue) |
|
|
sendEvent( |
|
|
this, |
|
|
'onResume', |
|
|
getFinishedResult(this, checkFinished(this, this.animation.to)), |
|
|
this |
|
|
) |
|
|
} |
|
|
}, |
|
|
start: this._merge.bind(this, range), |
|
|
}, |
|
|
}).then(result => { |
|
|
if (props.loop && result.finished && !(isLoop && result.noop)) { |
|
|
const nextProps = createLoopUpdate(props) |
|
|
if (nextProps) { |
|
|
return this._update(nextProps, true) |
|
|
} |
|
|
} |
|
|
return result |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
protected _merge( |
|
|
range: AnimationRange<T>, |
|
|
props: RunAsyncProps<SpringValue<T>>, |
|
|
resolve: AnimationResolver<SpringValue<T>> |
|
|
): void { |
|
|
|
|
|
|
|
|
if (props.cancel) { |
|
|
this.stop(true) |
|
|
return resolve(getCancelledResult(this)) |
|
|
} |
|
|
|
|
|
|
|
|
const hasToProp = !is.und(range.to) |
|
|
|
|
|
|
|
|
const hasFromProp = !is.und(range.from) |
|
|
|
|
|
|
|
|
|
|
|
if (hasToProp || hasFromProp) { |
|
|
if (props.callId > this._lastToId) { |
|
|
this._lastToId = props.callId |
|
|
} else { |
|
|
return resolve(getCancelledResult(this)) |
|
|
} |
|
|
} |
|
|
|
|
|
const { key, defaultProps, animation: anim } = this |
|
|
const { to: prevTo, from: prevFrom } = anim |
|
|
let { to = prevTo, from = prevFrom } = range |
|
|
|
|
|
|
|
|
|
|
|
if (hasFromProp && !hasToProp && (!props.default || is.und(to))) { |
|
|
to = from |
|
|
} |
|
|
|
|
|
|
|
|
if (props.reverse) [to, from] = [from, to] |
|
|
|
|
|
|
|
|
const hasFromChanged = !isEqual(from, prevFrom) |
|
|
|
|
|
if (hasFromChanged) { |
|
|
anim.from = from |
|
|
} |
|
|
|
|
|
|
|
|
from = getFluidValue(from) |
|
|
|
|
|
|
|
|
const hasToChanged = !isEqual(to, prevTo) |
|
|
|
|
|
if (hasToChanged) { |
|
|
this._focus(to) |
|
|
} |
|
|
|
|
|
|
|
|
const hasAsyncTo = isAsyncTo(props.to) |
|
|
|
|
|
const { config } = anim |
|
|
const { decay, velocity } = config |
|
|
|
|
|
|
|
|
if (hasToProp || hasFromProp) { |
|
|
config.velocity = 0 |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (props.config && !hasAsyncTo) { |
|
|
mergeConfig( |
|
|
config, |
|
|
callProp(props.config, key!), |
|
|
|
|
|
props.config !== defaultProps.config |
|
|
? callProp(defaultProps.config, key!) |
|
|
: void 0 |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let node = getAnimated(this) |
|
|
if (!node || is.und(to)) { |
|
|
return resolve(getFinishedResult(this, true)) |
|
|
} |
|
|
|
|
|
|
|
|
const reset = |
|
|
|
|
|
|
|
|
|
|
|
is.und(props.reset) |
|
|
? hasFromProp && !props.default |
|
|
: !is.und(from) && matchProp(props.reset, key) |
|
|
|
|
|
|
|
|
const value = reset ? (from as T) : this.get() |
|
|
|
|
|
|
|
|
const goal = computeGoal<any>(to) |
|
|
|
|
|
|
|
|
const isAnimatable = is.num(goal) || is.arr(goal) || isAnimatedString(goal) |
|
|
|
|
|
|
|
|
const immediate = |
|
|
!hasAsyncTo && |
|
|
(!isAnimatable || |
|
|
matchProp(defaultProps.immediate || props.immediate, key)) |
|
|
|
|
|
if (hasToChanged) { |
|
|
const nodeType = getAnimatedType(to) |
|
|
if (nodeType !== node.constructor) { |
|
|
if (immediate) { |
|
|
node = this._set(goal)! |
|
|
} else |
|
|
throw Error( |
|
|
`Cannot animate between ${node.constructor.name} and ${nodeType.name}, as the "to" prop suggests` |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const goalType = node.constructor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let started = hasFluidValue(to) |
|
|
let finished = false |
|
|
|
|
|
if (!started) { |
|
|
|
|
|
const hasValueChanged = reset || (!hasAnimated(this) && hasFromChanged) |
|
|
|
|
|
|
|
|
|
|
|
if (hasToChanged || hasValueChanged) { |
|
|
finished = isEqual(computeGoal(value), goal) |
|
|
started = !finished |
|
|
} |
|
|
|
|
|
|
|
|
if ( |
|
|
(!isEqual(anim.immediate, immediate) && !immediate) || |
|
|
!isEqual(config.decay, decay) || |
|
|
!isEqual(config.velocity, velocity) |
|
|
) { |
|
|
started = true |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (finished && isAnimating(this)) { |
|
|
|
|
|
|
|
|
if (anim.changed && !reset) { |
|
|
started = true |
|
|
} |
|
|
|
|
|
else if (!started) { |
|
|
this._stop(prevTo) |
|
|
} |
|
|
} |
|
|
|
|
|
if (!hasAsyncTo) { |
|
|
|
|
|
|
|
|
if (started || hasFluidValue(prevTo)) { |
|
|
anim.values = node.getPayload() |
|
|
anim.toValues = hasFluidValue(to) |
|
|
? null |
|
|
: goalType == AnimatedString |
|
|
? [1] |
|
|
: toArray(goal) |
|
|
} |
|
|
|
|
|
if (anim.immediate != immediate) { |
|
|
anim.immediate = immediate |
|
|
|
|
|
|
|
|
if (!immediate && !reset) { |
|
|
this._set(prevTo) |
|
|
} |
|
|
} |
|
|
|
|
|
if (started) { |
|
|
const { onRest } = anim |
|
|
|
|
|
|
|
|
each(ACTIVE_EVENTS, type => mergeActiveFn(this, props, type)) |
|
|
|
|
|
const result = getFinishedResult(this, checkFinished(this, prevTo)) |
|
|
flushCalls(this._pendingCalls, result) |
|
|
this._pendingCalls.add(resolve) |
|
|
|
|
|
if (anim.changed) |
|
|
raf.batchedUpdates(() => { |
|
|
|
|
|
anim.changed = !reset |
|
|
|
|
|
|
|
|
onRest?.(result, this) |
|
|
|
|
|
|
|
|
|
|
|
if (reset) { |
|
|
callProp(defaultProps.onRest, result) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else { |
|
|
anim.onStart?.(result, this) |
|
|
} |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
if (reset) { |
|
|
this._set(value) |
|
|
} |
|
|
|
|
|
if (hasAsyncTo) { |
|
|
resolve(runAsync(props.to, props, this._state, this)) |
|
|
} |
|
|
|
|
|
|
|
|
else if (started) { |
|
|
this._start() |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
else if (isAnimating(this) && !hasToChanged) { |
|
|
this._pendingCalls.add(resolve) |
|
|
} |
|
|
|
|
|
|
|
|
else { |
|
|
resolve(getNoopResult(value)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected _focus(value: T | FluidValue<T>) { |
|
|
const anim = this.animation |
|
|
if (value !== anim.to) { |
|
|
if (getFluidObservers(this)) { |
|
|
this._detach() |
|
|
} |
|
|
anim.to = value |
|
|
if (getFluidObservers(this)) { |
|
|
this._attach() |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
protected _attach() { |
|
|
let priority = 0 |
|
|
|
|
|
const { to } = this.animation |
|
|
if (hasFluidValue(to)) { |
|
|
addFluidObserver(to, this) |
|
|
if (isFrameValue(to)) { |
|
|
priority = to.priority + 1 |
|
|
} |
|
|
} |
|
|
|
|
|
this.priority = priority |
|
|
} |
|
|
|
|
|
protected _detach() { |
|
|
const { to } = this.animation |
|
|
if (hasFluidValue(to)) { |
|
|
removeFluidObserver(to, this) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected _set(arg: T | FluidValue<T>, idle = true): Animated | undefined { |
|
|
const value = getFluidValue(arg) |
|
|
if (!is.und(value)) { |
|
|
const oldNode = getAnimated(this) |
|
|
if (!oldNode || !isEqual(value, oldNode.getValue())) { |
|
|
|
|
|
const nodeType = getAnimatedType(value) |
|
|
if (!oldNode || oldNode.constructor != nodeType) { |
|
|
setAnimated(this, nodeType.create(value)) |
|
|
} else { |
|
|
oldNode.setValue(value) |
|
|
} |
|
|
|
|
|
if (oldNode) { |
|
|
raf.batchedUpdates(() => { |
|
|
this._onChange(value, idle) |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
return getAnimated(this) |
|
|
} |
|
|
|
|
|
protected _onStart() { |
|
|
const anim = this.animation |
|
|
if (!anim.changed) { |
|
|
anim.changed = true |
|
|
sendEvent( |
|
|
this, |
|
|
'onStart', |
|
|
getFinishedResult(this, checkFinished(this, anim.to)), |
|
|
this |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
protected _onChange(value: T, idle?: boolean) { |
|
|
if (!idle) { |
|
|
this._onStart() |
|
|
callProp(this.animation.onChange, value, this) |
|
|
} |
|
|
callProp(this.defaultProps.onChange, value, this) |
|
|
super._onChange(value, idle) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected _start() { |
|
|
const anim = this.animation |
|
|
|
|
|
|
|
|
getAnimated(this)!.reset(getFluidValue(anim.to)) |
|
|
|
|
|
|
|
|
if (!anim.immediate) { |
|
|
anim.fromValues = anim.values.map(node => node.lastPosition) |
|
|
} |
|
|
|
|
|
if (!isAnimating(this)) { |
|
|
setActiveBit(this, true) |
|
|
if (!isPaused(this)) { |
|
|
this._resume() |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
protected _resume() { |
|
|
|
|
|
if (G.skipAnimation) { |
|
|
this.finish() |
|
|
} else { |
|
|
frameLoop.start(this) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected _stop(goal?: any, cancel?: boolean) { |
|
|
if (isAnimating(this)) { |
|
|
setActiveBit(this, false) |
|
|
|
|
|
const anim = this.animation |
|
|
each(anim.values, node => { |
|
|
node.done = true |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (anim.toValues) { |
|
|
anim.onChange = anim.onPause = anim.onResume = undefined |
|
|
} |
|
|
|
|
|
callFluidObservers(this, { |
|
|
type: 'idle', |
|
|
parent: this, |
|
|
}) |
|
|
|
|
|
const result = cancel |
|
|
? getCancelledResult(this.get()) |
|
|
: getFinishedResult(this.get(), checkFinished(this, goal ?? anim.to)) |
|
|
|
|
|
flushCalls(this._pendingCalls, result) |
|
|
if (anim.changed) { |
|
|
anim.changed = false |
|
|
sendEvent(this, 'onRest', result, this) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function checkFinished<T>(target: SpringValue<T>, to: T | FluidValue<T>) { |
|
|
const goal = computeGoal(to) |
|
|
const value = computeGoal(target.get()) |
|
|
return isEqual(value, goal) |
|
|
} |
|
|
|
|
|
export function createLoopUpdate<T>( |
|
|
props: T & { loop?: any; to?: any; from?: any; reverse?: any }, |
|
|
loop = props.loop, |
|
|
to = props.to |
|
|
): T | undefined { |
|
|
const loopRet = callProp(loop) |
|
|
if (loopRet) { |
|
|
const overrides = loopRet !== true && inferTo(loopRet) |
|
|
const reverse = (overrides || props).reverse |
|
|
const reset = !overrides || overrides.reset |
|
|
return createUpdate({ |
|
|
...props, |
|
|
loop, |
|
|
|
|
|
|
|
|
default: false, |
|
|
|
|
|
|
|
|
pause: undefined, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
to: !reverse || isAsyncTo(to) ? to : undefined, |
|
|
|
|
|
|
|
|
from: reset ? props.from : undefined, |
|
|
reset, |
|
|
|
|
|
|
|
|
|
|
|
...overrides, |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function createUpdate(props: any) { |
|
|
const { to, from } = (props = inferTo(props)) |
|
|
|
|
|
|
|
|
const keys = new Set<string>() |
|
|
|
|
|
if (is.obj(to)) findDefined(to, keys) |
|
|
if (is.obj(from)) findDefined(from, keys) |
|
|
|
|
|
|
|
|
props.keys = keys.size ? Array.from(keys) : null |
|
|
|
|
|
return props |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function declareUpdate(props: any) { |
|
|
const update = createUpdate(props) |
|
|
if (is.und(update.default)) { |
|
|
update.default = getDefaultProps(update) |
|
|
} |
|
|
return update |
|
|
} |
|
|
|
|
|
|
|
|
function findDefined(values: Lookup, keys: Set<string>) { |
|
|
eachProp(values, (value, key) => value != null && keys.add(key as any)) |
|
|
} |
|
|
|
|
|
|
|
|
const ACTIVE_EVENTS = [ |
|
|
'onStart', |
|
|
'onRest', |
|
|
'onChange', |
|
|
'onPause', |
|
|
'onResume', |
|
|
] as const |
|
|
|
|
|
function mergeActiveFn<T, P extends EventKey>( |
|
|
target: SpringValue<T>, |
|
|
props: SpringProps<T>, |
|
|
type: P |
|
|
) { |
|
|
target.animation[type] = |
|
|
props[type] !== getDefaultProp(props, type) |
|
|
? resolveProp<any>(props[type], target.key) |
|
|
: undefined |
|
|
} |
|
|
|
|
|
type EventArgs<T, P extends EventKey> = Parameters< |
|
|
Extract<SpringProps<T>[P], Function> |
|
|
> |
|
|
|
|
|
|
|
|
function sendEvent<T, P extends EventKey>( |
|
|
target: SpringValue<T>, |
|
|
type: P, |
|
|
...args: EventArgs<T, P> |
|
|
) { |
|
|
target.animation[type]?.(...(args as [any, any])) |
|
|
target.defaultProps[type]?.(...(args as [any, any])) |
|
|
} |
|
|
|