File size: 1,396 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
import { defineHidden } from '@react-spring/shared'
import { AnimatedValue } from './AnimatedValue'

const $node: any = Symbol.for('Animated:node')

export const isAnimated = <T = any>(value: any): value is Animated<T> =>
  !!value && value[$node] === value

/** Get the owner's `Animated` node. */
export const getAnimated = <T = any>(owner: any): Animated<T> | undefined =>
  owner && owner[$node]

/** Set the owner's `Animated` node. */
export const setAnimated = (owner: any, node: Animated) =>
  defineHidden(owner, $node, node)

/** Get every `AnimatedValue` in the owner's `Animated` node. */
export const getPayload = (owner: any): AnimatedValue[] | undefined =>
  owner && owner[$node] && owner[$node].getPayload()

export abstract class Animated<T = any> {
  /** The cache of animated values */
  protected payload?: Payload

  constructor() {
    // This makes "isAnimated" return true.
    setAnimated(this, this)
  }

  /** Get the current value. Pass `true` for only animated values. */
  abstract getValue(animated?: boolean): T

  /** Set the current value. Returns `true` if the value changed. */
  abstract setValue(value: T): boolean | void

  /** Reset any animation state. */
  abstract reset(goal?: T): void

  /** Get every `AnimatedValue` used by this node. */
  getPayload(): Payload {
    return this.payload || []
  }
}

export type Payload = readonly AnimatedValue[]