File size: 1,629 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 |
import { Lookup } from '@react-spring/types'
import {
each,
eachProp,
getFluidValue,
hasFluidValue,
} from '@react-spring/shared'
import { Animated, isAnimated, getPayload } from './Animated'
import { AnimatedValue } from './AnimatedValue'
import { TreeContext } from './context'
/** An object containing `Animated` nodes */
export class AnimatedObject extends Animated {
constructor(protected source: Lookup) {
super()
this.setValue(source)
}
getValue(animated?: boolean) {
const values: Lookup = {}
eachProp(this.source, (source, key) => {
if (isAnimated(source)) {
values[key] = source.getValue(animated)
} else if (hasFluidValue(source)) {
values[key] = getFluidValue(source)
} else if (!animated) {
values[key] = source
}
})
return values
}
/** Replace the raw object data */
setValue(source: Lookup) {
this.source = source
this.payload = this._makePayload(source)
}
reset() {
if (this.payload) {
each(this.payload, node => node.reset())
}
}
/** Create a payload set. */
protected _makePayload(source: Lookup) {
if (source) {
const payload = new Set<AnimatedValue>()
eachProp(source, this._addToPayload, payload)
return Array.from(payload)
}
}
/** Add to a payload set. */
protected _addToPayload(this: Set<AnimatedValue>, source: any) {
if (TreeContext.dependencies && hasFluidValue(source)) {
TreeContext.dependencies.add(source)
}
const payload = getPayload(source)
if (payload) {
each(payload, node => this.add(node))
}
}
}
|