File size: 3,353 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
import { raf } from '@react-spring/rafz'
import * as G from './globals'

export interface OpaqueAnimation {
  idle: boolean
  priority: number
  advance(dt: number): void
}

// Animations starting on the next frame
const startQueue = new Set<OpaqueAnimation>()

// The animations being updated in the current frame, sorted by lowest
// priority first. These two arrays are swapped at the end of each frame.
let currentFrame: OpaqueAnimation[] = []
let prevFrame: OpaqueAnimation[] = []

// The priority of the currently advancing animation.
// To protect against a race condition whenever a frame is being processed,
// where the filtering of `animations` is corrupted with a shifting index,
// causing animations to potentially advance 2x faster than intended.
let priority = 0

/**
 * The frameloop executes its animations in order of lowest priority first.
 * Animations are retained until idle.
 */
export const frameLoop = {
  get idle() {
    return !startQueue.size && !currentFrame.length
  },

  /** Advance the given animation on every frame until idle. */
  start(animation: OpaqueAnimation) {
    // An animation can be added while a frame is being processed,
    // unless its priority is lower than the animation last updated.
    if (priority > animation.priority) {
      startQueue.add(animation)
      raf.onStart(flushStartQueue)
    } else {
      startSafely(animation)
      raf(advance)
    }
  },

  /** Advance all animations by the given time. */
  advance,

  /** Call this when an animation's priority changes. */
  sort(animation: OpaqueAnimation) {
    if (priority) {
      raf.onFrame(() => frameLoop.sort(animation))
    } else {
      const prevIndex = currentFrame.indexOf(animation)
      if (~prevIndex) {
        currentFrame.splice(prevIndex, 1)
        startUnsafely(animation)
      }
    }
  },

  /**
   * Clear all animations. For testing purposes.
   *
   * ☠️ Never call this from within the frameloop.
   */
  clear() {
    currentFrame = []
    startQueue.clear()
  },
}

function flushStartQueue() {
  startQueue.forEach(startSafely)
  startQueue.clear()
  raf(advance)
}

function startSafely(animation: OpaqueAnimation) {
  if (!currentFrame.includes(animation)) startUnsafely(animation)
}

function startUnsafely(animation: OpaqueAnimation) {
  currentFrame.splice(
    findIndex(currentFrame, other => other.priority > animation.priority),
    0,
    animation
  )
}

function advance(dt: number) {
  const nextFrame = prevFrame

  for (let i = 0; i < currentFrame.length; i++) {
    const animation = currentFrame[i]
    priority = animation.priority

    // Animations may go idle before advancing.
    if (!animation.idle) {
      G.willAdvance(animation)
      animation.advance(dt)
      if (!animation.idle) {
        nextFrame.push(animation)
      }
    }
  }
  priority = 0

  // Reuse the `currentFrame` array to avoid garbage collection.
  prevFrame = currentFrame
  prevFrame.length = 0

  // Set `currentFrame` for next frame, so the `start` function
  // adds new animations to the proper array.
  currentFrame = nextFrame

  return currentFrame.length > 0
}

/** Like `Array.prototype.findIndex` but returns `arr.length` instead of `-1` */
function findIndex<T>(arr: T[], test: (value: T) => boolean) {
  const index = arr.findIndex(test)
  return index < 0 ? arr.length : index
}