Skip to content

Commit 25b3b21

Browse files
Cập nhật chỉnh sửa hiệu năng load liveview và liveview2D
Giải pháp: wled#5013
1 parent cf2fb86 commit 25b3b21

15 files changed

+423
-366
lines changed

wled00/data/common.js

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,38 @@ function tooltip(cont=null) {
5151
});
5252
});
5353
};
54+
// sequential loading of external resources (JS or CSS) with retry, calls init() when done
55+
function loadResources(files, init) {
56+
let i = 0;
57+
const loadNext = () => {
58+
if (i >= files.length) {
59+
if (init) {
60+
d.documentElement.style.visibility = 'visible'; // make page visible after all files are loaded if it was hidden (prevent ugly display)
61+
d.readyState === 'complete' ? init() : window.addEventListener('load', init);
62+
}
63+
return;
64+
}
65+
const file = files[i++];
66+
const isCSS = file.endsWith('.css');
67+
const el = d.createElement(isCSS ? 'link' : 'script');
68+
if (isCSS) {
69+
el.rel = 'stylesheet';
70+
el.href = file;
71+
const st = d.head.querySelector('style');
72+
if (st) d.head.insertBefore(el, st); // insert before any <style> to allow overrides
73+
else d.head.appendChild(el);
74+
} else {
75+
el.src = file;
76+
d.head.appendChild(el);
77+
}
78+
el.onload = () => { loadNext(); };
79+
el.onerror = () => {
80+
i--; // load this file again
81+
setTimeout(loadNext, 100);
82+
};
83+
};
84+
loadNext();
85+
}
5486
// https://www.educative.io/edpresso/how-to-dynamically-load-a-js-file-in-javascript
5587
function loadJS(FILE_URL, async = true, preGetV = undefined, postGetV = undefined) {
5688
let scE = d.createElement("script");
@@ -116,21 +148,22 @@ function uploadFile(fileObj, name) {
116148
fileObj.value = '';
117149
return false;
118150
}
119-
// connect to WebSocket, use parent WS or open new
151+
// connect to WebSocket, use parent WS or open new, callback function gets passed the new WS object
120152
function connectWs(onOpen) {
121-
try {
122-
if (top.window.ws && top.window.ws.readyState === WebSocket.OPEN) {
123-
if (onOpen) onOpen();
124-
return top.window.ws;
125-
}
126-
} catch (e) {}
127-
128-
getLoc(); // ensure globals (loc, locip, locproto) are up to date
129-
let url = loc ? getURL('/ws').replace("http","ws") : "ws://"+window.location.hostname+"/ws";
130-
let ws = new WebSocket(url);
131-
ws.binaryType = "arraybuffer";
132-
if (onOpen) { ws.onopen = onOpen; }
133-
try { top.window.ws = ws; } catch (e) {} // store in parent for reuse
153+
let ws;
154+
try { ws = top.window.ws;} catch (e) {}
155+
// reuse if open
156+
if (ws && ws.readyState === WebSocket.OPEN) {
157+
if (onOpen) onOpen(ws);
158+
} else {
159+
// create new ws connection
160+
getLoc(); // ensure globals are up to date
161+
let url = loc ? getURL('/ws').replace("http", "ws")
162+
: "ws://" + window.location.hostname + "/ws";
163+
ws = new WebSocket(url);
164+
if (onOpen) ws.onopen = () => onOpen(ws);
165+
ws.binaryType = "arraybuffer";
166+
}
134167
return ws;
135168
}
136169

@@ -174,4 +207,4 @@ function sendDDP(ws, start, len, colors) {
174207
}
175208
}
176209
return true;
177-
}
210+
}

0 commit comments

Comments
 (0)