gunter-kgd 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,49 @@
1
+ const callbackList = []
2
+
3
+ const find = (list, predicate) => {
4
+ for (let index = 0; index < list.length; index++) {
5
+ if (predicate(list[index], index, list)) {
6
+ return list[index]
7
+ }
8
+ }
9
+ return null
10
+ }
11
+ const findIndex = (list, predicate) => {
12
+ for (let index = 0; index < list.length; index++) {
13
+ if (predicate(list[index], index, list)) {
14
+ return index
15
+ }
16
+ }
17
+ return -1
18
+ }
19
+
20
+ function raf(callback) {
21
+ const entry = find(callbackList, item => item.callback === callback)
22
+ if (entry) {
23
+ return entry.requestId
24
+ }
25
+ else {
26
+ const requestId = requestAnimationFrame(ts => {
27
+ const index = findIndex(callbackList, item => item.callback === callback)
28
+ callbackList.splice(index, 1)
29
+ callback(ts)
30
+ })
31
+ callbackList.push({
32
+ callback,
33
+ requestId,
34
+ })
35
+ return requestId
36
+ }
37
+ }
38
+ function caf(requestId) {
39
+ const index = findIndex(callbackList, item => item.requestId === requestId)
40
+ if (~index) {
41
+ callbackList.splice(index, 1)
42
+ }
43
+ cancelAnimationFrame(requestId)
44
+ }
45
+
46
+ export {
47
+ raf as requestAnimationFrame,
48
+ caf as cancelAnimationFrame,
49
+ }