-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
36 lines (30 loc) · 922 Bytes
/
index.js
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
export default function requestAnimationFrames() {
const hasRaf = typeof globalThis.requestAnimationFrame === 'function';
const requestFrame = hasRaf
? globalThis.requestAnimationFrame
: callback => setTimeout(() => callback(performance.now()), 16);
const cancelFrame = hasRaf
? globalThis.cancelAnimationFrame
: id => clearTimeout(id);
return {
async * [Symbol.asyncIterator]() {
let requestId;
try {
yield performance.now();
// Ensure the RAF timestamp comes after our custom one.
// In some cases without this, the `performance.now()`
// call above would return the same timestamp as RAF.
await new Promise(resolve => {
setTimeout(resolve, 1);
});
while (true) {
yield await new Promise(resolve => { // eslint-disable-line no-await-in-loop
requestId = requestFrame(resolve);
});
}
} finally {
cancelFrame(requestId);
}
},
};
}