Add promiseAnimationFrame function (#752)

This commit is contained in:
toasted-nutbread 2020-08-23 12:43:53 -04:00 committed by GitHub
parent 1ab853a4d5
commit 934355dd09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -108,6 +108,7 @@
"deferPromise": "readonly",
"clone": "readonly",
"generateId": "readonly",
"promiseAnimationFrame": "readonly",
"DynamicProperty": "readonly",
"EventDispatcher": "readonly",
"EventListenerCollection": "readonly"

View File

@ -258,6 +258,34 @@ function promiseTimeout(delay, resolveValue) {
return promise;
}
function promiseAnimationFrame(timeout=null) {
return new Promise((resolve) => {
let timer = null;
let frameRequest = null;
const onFrame = (time) => {
frameRequest = null;
if (timer !== null) {
clearTimeout(timer);
timer = null;
}
resolve({time, timeout: false});
};
const onTimeout = () => {
timer = null;
if (frameRequest !== null) {
cancelAnimationFrame(frameRequest);
frameRequest = null;
}
resolve({time: timeout, timeout: true});
};
frameRequest = requestAnimationFrame(onFrame);
if (typeof timeout === 'number') {
timer = setTimeout(onTimeout, timeout);
}
});
}
/*
* Common classes