File size: 3,024 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
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
/**
* A kind of `FluidValue` that manages an `AnimatedValue` node.
*
* Its underlying value can be accessed and even observed.
*/
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 the current value */
get(): T {
const node = getAnimated(this)
return node && node.getValue()
}
/** Create a spring that maps our value to another value */
to<Out>(...args: InterpolatorArgs<T, Out>) {
return G.to(this, args) as Interpolation<T, Out>
}
/** @deprecated Use the `to` method instead. */
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()
}
/** @internal */
abstract advance(dt: number): void
/** @internal */
abstract eventObserved(_event: FrameValue.Event): void
/** Called when the first child is added. */
protected _attach() {}
/** Called when the last child is removed. */
protected _detach() {}
/** Tell our children about our new value */
protected _onChange(value: T, idle = false) {
callFluidObservers(this, {
type: 'change',
parent: this,
value,
idle,
})
}
/** Tell our children about our new priority */
protected _onPriorityChange(priority: number) {
if (!this.idle) {
frameLoop.sort(this)
}
callFluidObservers(this, {
type: 'priority',
parent: this,
priority,
})
}
}
// eslint-disable-next-line @typescript-eslint/no-namespace
export declare namespace FrameValue {
/** A parent changed its value */
interface ChangeEvent<T = any> {
parent: FrameValue<T>
type: 'change'
value: T
idle: boolean
}
/** A parent changed its priority */
interface PriorityEvent<T = any> {
parent: FrameValue<T>
type: 'priority'
priority: number
}
/** A parent is done animating */
interface IdleEvent<T = any> {
parent: FrameValue<T>
type: 'idle'
}
/** Events sent to children of `FrameValue` objects */
export type Event<T = any> = ChangeEvent<T> | PriorityEvent<T> | IdleEvent<T>
}
|