|
|
import { Arrify, InterpolatorArgs, InterpolatorFn } from '@react-spring/types' |
|
|
import { |
|
|
is, |
|
|
raf, |
|
|
each, |
|
|
isEqual, |
|
|
toArray, |
|
|
frameLoop, |
|
|
FluidValue, |
|
|
getFluidValue, |
|
|
createInterpolator, |
|
|
Globals as G, |
|
|
callFluidObservers, |
|
|
addFluidObserver, |
|
|
removeFluidObserver, |
|
|
hasFluidValue, |
|
|
} from '@react-spring/shared' |
|
|
|
|
|
import { FrameValue, isFrameValue } from './FrameValue' |
|
|
import { |
|
|
getAnimated, |
|
|
setAnimated, |
|
|
getAnimatedType, |
|
|
getPayload, |
|
|
} from '@react-spring/animated' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class Interpolation< |
|
|
Input = any, |
|
|
Output = any, |
|
|
> extends FrameValue<Output> { |
|
|
|
|
|
key?: string |
|
|
|
|
|
|
|
|
idle = true |
|
|
|
|
|
|
|
|
readonly calc: InterpolatorFn<Input, Output> |
|
|
|
|
|
|
|
|
protected _active = new Set<FluidValue>() |
|
|
|
|
|
constructor( |
|
|
|
|
|
readonly source: unknown, |
|
|
args: InterpolatorArgs<Input, Output> |
|
|
) { |
|
|
super() |
|
|
this.calc = createInterpolator(...args) |
|
|
|
|
|
const value = this._get() |
|
|
const nodeType = getAnimatedType(value) |
|
|
|
|
|
|
|
|
setAnimated(this, nodeType.create(value)) |
|
|
} |
|
|
|
|
|
advance(_dt?: number) { |
|
|
const value = this._get() |
|
|
const oldValue = this.get() |
|
|
if (!isEqual(value, oldValue)) { |
|
|
getAnimated(this)!.setValue(value) |
|
|
this._onChange(value, this.idle) |
|
|
} |
|
|
|
|
|
if (!this.idle && checkIdle(this._active)) { |
|
|
becomeIdle(this) |
|
|
} |
|
|
} |
|
|
|
|
|
protected _get() { |
|
|
const inputs: Arrify<Input> = is.arr(this.source) |
|
|
? this.source.map(getFluidValue) |
|
|
: (toArray(getFluidValue(this.source)) as any) |
|
|
|
|
|
return this.calc(...inputs) |
|
|
} |
|
|
|
|
|
protected _start() { |
|
|
if (this.idle && !checkIdle(this._active)) { |
|
|
this.idle = false |
|
|
|
|
|
each(getPayload(this)!, node => { |
|
|
node.done = false |
|
|
}) |
|
|
|
|
|
if (G.skipAnimation) { |
|
|
raf.batchedUpdates(() => this.advance()) |
|
|
becomeIdle(this) |
|
|
} else { |
|
|
frameLoop.start(this) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected _attach() { |
|
|
let priority = 1 |
|
|
each(toArray(this.source), source => { |
|
|
if (hasFluidValue(source)) { |
|
|
addFluidObserver(source, this) |
|
|
} |
|
|
if (isFrameValue(source)) { |
|
|
if (!source.idle) { |
|
|
this._active.add(source) |
|
|
} |
|
|
priority = Math.max(priority, source.priority + 1) |
|
|
} |
|
|
}) |
|
|
this.priority = priority |
|
|
this._start() |
|
|
} |
|
|
|
|
|
|
|
|
protected _detach() { |
|
|
each(toArray(this.source), source => { |
|
|
if (hasFluidValue(source)) { |
|
|
removeFluidObserver(source, this) |
|
|
} |
|
|
}) |
|
|
this._active.clear() |
|
|
becomeIdle(this) |
|
|
} |
|
|
|
|
|
|
|
|
eventObserved(event: FrameValue.Event) { |
|
|
|
|
|
|
|
|
if (event.type == 'change') { |
|
|
if (event.idle) { |
|
|
this.advance() |
|
|
} else { |
|
|
this._active.add(event.parent) |
|
|
this._start() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
else if (event.type == 'idle') { |
|
|
this._active.delete(event.parent) |
|
|
} |
|
|
|
|
|
|
|
|
else if (event.type == 'priority') { |
|
|
this.priority = toArray(this.source).reduce( |
|
|
(highest: number, parent) => |
|
|
Math.max(highest, (isFrameValue(parent) ? parent.priority : 0) + 1), |
|
|
0 |
|
|
) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function isIdle(source: any) { |
|
|
return source.idle !== false |
|
|
} |
|
|
|
|
|
|
|
|
function checkIdle(active: Set<FluidValue>) { |
|
|
|
|
|
|
|
|
return !active.size || Array.from(active).every(isIdle) |
|
|
} |
|
|
|
|
|
|
|
|
function becomeIdle(self: Interpolation) { |
|
|
if (!self.idle) { |
|
|
self.idle = true |
|
|
|
|
|
each(getPayload(self)!, node => { |
|
|
node.done = true |
|
|
}) |
|
|
|
|
|
callFluidObservers(self, { |
|
|
type: 'idle', |
|
|
parent: self, |
|
|
}) |
|
|
} |
|
|
} |
|
|
|