|
|
import { |
|
|
deprecateInterpolate, |
|
|
frameLoop, |
|
|
FluidValue, |
|
|
Globals as G, |
|
|
callFluidObservers, |
|
|
} from '@react-spring/shared' |
|
|
import { InterpolatorArgs } from '@react-spring/types' |
|
|
import { getAnimated } from '@react-spring/animated' |
|
|
|
|
|
import { Interpolation } from './Interpolation' |
|
|
|
|
|
export const isFrameValue = (value: any): value is FrameValue => |
|
|
value instanceof FrameValue |
|
|
|
|
|
let nextId = 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export abstract class FrameValue<T = any> extends FluidValue< |
|
|
T, |
|
|
FrameValue.Event<T> |
|
|
> { |
|
|
readonly id = nextId++ |
|
|
|
|
|
abstract key?: string |
|
|
abstract get idle(): boolean |
|
|
|
|
|
protected _priority = 0 |
|
|
|
|
|
get priority() { |
|
|
return this._priority |
|
|
} |
|
|
set priority(priority: number) { |
|
|
if (this._priority != priority) { |
|
|
this._priority = priority |
|
|
this._onPriorityChange(priority) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
get(): T { |
|
|
const node = getAnimated(this) |
|
|
return node && node.getValue() |
|
|
} |
|
|
|
|
|
|
|
|
to<Out>(...args: InterpolatorArgs<T, Out>) { |
|
|
return G.to(this, args) as Interpolation<T, Out> |
|
|
} |
|
|
|
|
|
|
|
|
interpolate<Out>(...args: InterpolatorArgs<T, Out>) { |
|
|
deprecateInterpolate() |
|
|
return G.to(this, args) as Interpolation<T, Out> |
|
|
} |
|
|
|
|
|
toJSON() { |
|
|
return this.get() |
|
|
} |
|
|
|
|
|
protected observerAdded(count: number) { |
|
|
if (count == 1) this._attach() |
|
|
} |
|
|
|
|
|
protected observerRemoved(count: number) { |
|
|
if (count == 0) this._detach() |
|
|
} |
|
|
|
|
|
|
|
|
abstract advance(dt: number): void |
|
|
|
|
|
|
|
|
abstract eventObserved(_event: FrameValue.Event): void |
|
|
|
|
|
|
|
|
protected _attach() {} |
|
|
|
|
|
|
|
|
protected _detach() {} |
|
|
|
|
|
|
|
|
protected _onChange(value: T, idle = false) { |
|
|
callFluidObservers(this, { |
|
|
type: 'change', |
|
|
parent: this, |
|
|
value, |
|
|
idle, |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
protected _onPriorityChange(priority: number) { |
|
|
if (!this.idle) { |
|
|
frameLoop.sort(this) |
|
|
} |
|
|
callFluidObservers(this, { |
|
|
type: 'priority', |
|
|
parent: this, |
|
|
priority, |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export declare namespace FrameValue { |
|
|
|
|
|
interface ChangeEvent<T = any> { |
|
|
parent: FrameValue<T> |
|
|
type: 'change' |
|
|
value: T |
|
|
idle: boolean |
|
|
} |
|
|
|
|
|
|
|
|
interface PriorityEvent<T = any> { |
|
|
parent: FrameValue<T> |
|
|
type: 'priority' |
|
|
priority: number |
|
|
} |
|
|
|
|
|
|
|
|
interface IdleEvent<T = any> { |
|
|
parent: FrameValue<T> |
|
|
type: 'idle' |
|
|
} |
|
|
|
|
|
|
|
|
export type Event<T = any> = ChangeEvent<T> | PriorityEvent<T> | IdleEvent<T> |
|
|
} |
|
|
|