Skip to content

Commit

Permalink
add host prop await method
Browse files Browse the repository at this point in the history
  • Loading branch information
spelkey-ucd committed Aug 5, 2024
1 parent 27f7871 commit 857ff2f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ucd-lib/theme-elements",
"version": "2.1.3",
"version": "2.1.4",
"description": "Custom elements for the UCD brand theme",
"main": "index.js",
"scripts": {
Expand Down
39 changes: 35 additions & 4 deletions elements/utils/controllers/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class WaitController{
* @method wait
* @description Wait for the specified amount of time
* @param {Number} time - Time to wait (ms)
* @returns
* @returns
*/
async wait(time){
return new Promise(resolve => {
Expand All @@ -20,7 +20,7 @@ export class WaitController{
* @method waitForUpdate
* @description Requests and waits for Lit update.
*/
async waitForUpdate(){
async waitForUpdate(){
this.host.requestUpdate();
await this.host.updateComplete;
}
Expand All @@ -30,13 +30,44 @@ export class WaitController{
* @description Wait for specified number of animation frames
* @param {Number} ct Number of frames
*/
async waitForFrames(ct=1) {
async waitForFrames(ct=1) {
for (let i = 0; i < ct; i++) {
await new Promise(resolve => {
requestAnimationFrame(resolve);
});
}
}

/**
* @description Wait for the host to have a property with a specific value
* @param {String} prop - Host property to wait for
* @param {*} value - Host property value to wait for
* @param {Number} timeout - Timeout in ms
* @returns {Object} - Object with the following properties:
* - prop: Property name
* - targetValue: Value to wait for
* - timeoutValue: Timeout value
* - propValue: Current value of the property
* - wasTimeout: Was the wait timed out
*/
async waitForHostPropertyValue(prop, value, timeout=5000){
const out = {
prop,
targetValue: value,
timeoutValue: timeout,
propValue: this.host[prop],
wasTimeout: false
};
let start = Date.now();
while (this.host[prop] !== value) {
if (Date.now() - start > timeout) {
out.wasTimeout = true;
return out;
}
await this.wait(10);
}
return out;
}


}
}

0 comments on commit 857ff2f

Please sign in to comment.