File size: 589 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let pendingCallbacks: Array<(time: number) => void> = []

/**
 * requestAnimationFrame that also works on non-browser environments like Node.
 */
export const raf = typeof requestAnimationFrame === 'function'
  ? requestAnimationFrame
  : (fn: (time: number) => void) => {
      if (!pendingCallbacks.length) {
        setImmediate(() => {
          const now = performance.now()
          const cbs = pendingCallbacks
          // in case cbs add new callbacks
          pendingCallbacks = []
          cbs.forEach(cb => cb(now))
        })
      }

      pendingCallbacks.push(fn)
    }