File size: 2,853 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
import { Timeout, is, raf, Globals as G } from '@react-spring/shared'
import { matchProp, callProp } from './helpers'
import { AsyncResult, MatchProp } from './types'
import { RunAsyncState, RunAsyncProps } from './runAsync'
import {
  AnimationResolver,
  AnimationTarget,
  InferProps,
  InferState,
} from './types/internal'

// The `scheduleProps` function only handles these defaults.
type DefaultProps<T> = { cancel?: MatchProp<T>; pause?: MatchProp<T> }

interface ScheduledProps<T extends AnimationTarget> {
  key?: string
  props: InferProps<T>
  defaultProps?: DefaultProps<InferState<T>>
  state: RunAsyncState<T>
  actions: {
    pause: () => void
    resume: () => void
    start: (props: RunAsyncProps<T>, resolve: AnimationResolver<T>) => void
  }
}

/**
 * This function sets a timeout if both the `delay` prop exists and
 * the `cancel` prop is not `true`.
 *
 * The `actions.start` function must handle the `cancel` prop itself,
 * but the `pause` prop is taken care of.
 */
export function scheduleProps<T extends AnimationTarget>(
  callId: number,
  { key, props, defaultProps, state, actions }: ScheduledProps<T>
): AsyncResult<T> {
  return new Promise((resolve, reject) => {
    let delay: number
    let timeout: Timeout

    let cancel = matchProp(props.cancel ?? defaultProps?.cancel, key)
    if (cancel) {
      onStart()
    } else {
      // The `pause` prop updates the paused flag.
      if (!is.und(props.pause)) {
        state.paused = matchProp(props.pause, key)
      }
      // The default `pause` takes precedence when true,
      // which allows `SpringContext` to work as expected.
      let pause = defaultProps?.pause
      if (pause !== true) {
        pause = state.paused || matchProp(pause, key)
      }

      delay = callProp(props.delay || 0, key)
      if (pause) {
        state.resumeQueue.add(onResume)
        actions.pause()
      } else {
        actions.resume()
        onResume()
      }
    }

    function onPause() {
      state.resumeQueue.add(onResume)
      state.timeouts.delete(timeout)
      timeout.cancel()
      // Cache the remaining delay.
      delay = timeout.time - raf.now()
    }

    function onResume() {
      if (delay > 0 && !G.skipAnimation) {
        state.delayed = true
        timeout = raf.setTimeout(onStart, delay)
        state.pauseQueue.add(onPause)
        state.timeouts.add(timeout)
      } else {
        onStart()
      }
    }

    function onStart() {
      if (state.delayed) {
        state.delayed = false
      }

      state.pauseQueue.delete(onPause)
      state.timeouts.delete(timeout)

      // Maybe cancelled during its delay.
      if (callId <= (state.cancelId || 0)) {
        cancel = true
      }

      try {
        actions.start({ ...props, callId, cancel }, resolve)
      } catch (err) {
        reject(err)
      }
    }
  })
}