This repository was archived by the owner on Jul 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathframe-rate.js
More file actions
40 lines (33 loc) · 1.21 KB
/
frame-rate.js
File metadata and controls
40 lines (33 loc) · 1.21 KB
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
import { getDeferred } from './promises.js';
export async function checkFrameRate({ signal, timeout } = {}) {
const { promise, resolve, reject } = getDeferred();
if (! (globalThis.requestIdleCallback instanceof Function)) {
reject(new DOMException('`requestIdleCallback()` not supported.'));
} if (signal instanceof globalThis.AbortSignal && signal.aborted) {
reject(signal.reason || new DOMException('Operation aborted.'));
} else {
let idle, first, last;
const abort = ({ target: {
reason = new DOMException('Operation aborted.'),
}}) => {
reject(reason);
if (typeof idle === 'number') cancelIdleCallback(idle);
if (typeof first === 'number') cancelAnimationFrame(first);
if (typeof last === 'number') cancelAnimationFrame(last);
};
if (signal instanceof globalThis.AbortSignal) {
signal.addEventListener('abort', abort, { once: true });
}
idle = requestIdleCallback(() => {
first = requestAnimationFrame(start => {
last = requestAnimationFrame(end => {
resolve(parseInt(1000 / (end - start)));
if (signal instanceof globalThis.AbortSignal) {
signal.removeEventListener('abort', abort, { once: true });
}
});
});
}, { timeout });
}
return promise;
}