-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Use sequential loading and requests for all UI resources #5013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
8fc6cac
31618c2
3778b25
e0155d4
86b9556
523f944
ebdddfa
6351ea0
3a003db
633d661
75413ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,38 @@ 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'); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took a look at it and the two functions do server quite different purposes, unifying them does not really make sense and will just make the code more complex, at least that is how it looks to me. |
||
| if (isCSS) { | ||
| el.rel = 'stylesheet'; | ||
| el.href = file; | ||
| const st = d.head.querySelector('style'); | ||
| if (st) d.head.insertBefore(el, st); // insert before any <style> to allow overrides | ||
| else d.head.appendChild(el); | ||
| } else { | ||
| el.src = file; | ||
| d.head.appendChild(el); | ||
| } | ||
| el.onload = () => { setTimeout(loadNext, 0);}; | ||
DedeHai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| el.onerror = () => { | ||
| i--; // load this file again | ||
| setTimeout(loadNext, 100); | ||
| }; | ||
| }; | ||
| 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"); | ||
|
|
@@ -116,21 +148,22 @@ function uploadFile(fileObj, name) { | |
| fileObj.value = ''; | ||
| return false; | ||
| } | ||
| // connect to WebSocket, use parent WS or open new | ||
| // connect to WebSocket, use parent WS or open new, callback function gets passed the new WS object | ||
| function connectWs(onOpen) { | ||
| try { | ||
| if (top.window.ws && top.window.ws.readyState === WebSocket.OPEN) { | ||
| if (onOpen) onOpen(); | ||
| return top.window.ws; | ||
| } | ||
| } catch (e) {} | ||
|
|
||
| getLoc(); // ensure globals (loc, locip, locproto) are up to date | ||
| let url = loc ? getURL('/ws').replace("http","ws") : "ws://"+window.location.hostname+"/ws"; | ||
| let ws = new WebSocket(url); | ||
| ws.binaryType = "arraybuffer"; | ||
| if (onOpen) { ws.onopen = onOpen; } | ||
| try { top.window.ws = ws; } catch (e) {} // store in parent for reuse | ||
| let ws; | ||
| try { ws = top.window.ws;} catch (e) {} | ||
| // reuse if open | ||
| if (ws && ws.readyState === WebSocket.OPEN) { | ||
| if (onOpen) onOpen(ws); | ||
| } else { | ||
| // create new ws connection | ||
| getLoc(); // ensure globals are up to date | ||
| let url = loc ? getURL('/ws').replace("http", "ws") | ||
| : "ws://" + window.location.hostname + "/ws"; | ||
| ws = new WebSocket(url); | ||
| if (onOpen) ws.onopen = onOpen(ws); | ||
DedeHai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ws.binaryType = "arraybuffer"; | ||
| } | ||
| return ws; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.