Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions wled00/data/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ function tooltip(cont=null) {
});
});
};
// sequential loading of external resources (JS or CSS) with retry, calls init() when done
function loadResources(files, init) {
let i = 0;
const loadNext = () => {
if (i >= files.length) {
if (init) {
d.documentElement.style.visibility = 'visible'; // make page visible after all files are laoded if it was hidden (prevent ugly display)
d.readyState === 'complete' ? init() : window.addEventListener('load', init);
}
return;
}
const file = files[i++];
const isCSS = file.endsWith('.css');
const el = d.createElement(isCSS ? 'link' : 'script');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose to implement JS loading as that already have a function for that, loadJS().

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my function loads JS and CSS, I did not understand the purpose of the already implemented one. maybe they can be combined?

if (isCSS) {
el.rel = 'stylesheet';
el.href = file;
} else {
el.src = file;
}
el.onload = () => { setTimeout(loadNext, 0);};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason to "schedule" next load without timeout? Wouldn't a simpler loadNext()be enough?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was suggested to me by claude.ai and the purpose is, if I understand correctly, to execute a loaded script if it needs to be executed. I am on thin ice on this one ;)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just call loadNext() which will do the same.
Almost. If you call loadNext() it will be executed within onload callback, if you schedule it, it will run separately in its own context.

el.onerror = () => {
i--; // load this file again
setTimeout(loadNext, 100);
};
d.head.appendChild(el);
};
loadNext();
}
// https://www.educative.io/edpresso/how-to-dynamically-load-a-js-file-in-javascript
function loadJS(FILE_URL, async = true, preGetV = undefined, postGetV = undefined) {
let scE = d.createElement("script");
Expand Down
Loading