Skip to content

Commit 30a336a

Browse files
authored
trying to tackle issues
1 parent 2306354 commit 30a336a

1 file changed

Lines changed: 108 additions & 102 deletions

File tree

ResourceLoader.js

Lines changed: 108 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -161,40 +161,48 @@ const ResourceLoader = (() => {
161161
let timedOut = false;
162162
let startedLoading = false;
163163

164-
const loadScriptWhenReady = (resolve, reject) => {
165-
const existingElement =
166-
document.head.querySelector(
167-
`[src="${finalUrl}"], [href="${finalUrl}"]`
168-
) ||
169-
document.body.querySelector(
170-
`[src="${finalUrl}"], [href="${finalUrl}"]`
171-
);
172-
if (existingElement) {
173-
log(`Resource already loaded: ${finalUrl}`, "verbose");
174-
resourceStates[url] = "loaded";
175-
resolve();
176-
return;
177-
}
164+
return (resourceLoadedPromises[url] = {
165+
promise: new Promise((resolve, reject) => {
166+
const loadScriptWhenReady = () => {
167+
const existingElement =
168+
document.head.querySelector(
169+
`[src="${finalUrl}"], [href="${finalUrl}"]`
170+
) ||
171+
document.body.querySelector(
172+
`[src="${finalUrl}"], [href="${finalUrl}"]`
173+
);
174+
if (existingElement) {
175+
log(`Resource already loaded: ${finalUrl}`, "verbose");
176+
resourceStates[url] = "loaded";
177+
resolve();
178+
return;
179+
}
178180

179-
let element;
180-
let timeoutId;
181-
182-
const handleTimeout = () => {
183-
timedOut = true;
184-
const error = new Error(`Timeout while loading: ${finalUrl}`);
185-
const categorizedError = categorizeError(error, fileType, finalUrl);
186-
reject(categorizedError);
187-
resourceStates[url] = "unloaded";
188-
if (onError) onError(categorizedError);
189-
if (element && startedLoading) {
190-
element.remove();
191-
log(`Resource load aborted due to timeout: ${finalUrl}`, "warn");
192-
}
193-
if (retryCount < retries) {
194-
log(`Retrying to load resource: ${finalUrl}`, "warn");
195-
setTimeout(() => loadResource(url, retryCount + 1), retryDelay);
196-
}
197-
};
181+
let element;
182+
let timeoutId;
183+
184+
const handleTimeout = () => {
185+
timedOut = true;
186+
const error = new Error(`Timeout while loading: ${finalUrl}`);
187+
const categorizedError = categorizeError(
188+
error,
189+
fileType,
190+
finalUrl
191+
);
192+
reject(categorizedError);
193+
resourceStates[url] = "unloaded";
194+
if (element && startedLoading) {
195+
element.remove();
196+
log(
197+
`Resource load aborted due to timeout: ${finalUrl}`,
198+
"warn"
199+
);
200+
}
201+
if (retryCount < retries) {
202+
log(`Retrying to load resource: ${finalUrl}`, "warn");
203+
setTimeout(() => loadResource(url, retryCount + 1), retryDelay);
204+
}
205+
};
198206

199207
switch (fileType) {
200208
case "js":
@@ -326,82 +334,80 @@ const ResourceLoader = (() => {
326334
return;
327335
}
328336

329-
applyAttributes(element, attributes, fileType); // Updated to pass fileType
330-
331-
startedLoading = true;
332-
333-
timeoutId = setTimeout(handleTimeout, timeout);
334-
335-
element.onload = () => {
336-
if (!timedOut) {
337-
clearTimeout(timeoutId);
338-
log(`Resource loaded from: ${finalUrl}`, "verbose");
339-
resourceStates[url] = "loaded";
340-
resolve(); // Ensure resolve is properly called here
341-
}
342-
};
343-
344-
element.onerror = () => {
345-
clearTimeout(timeoutId);
346-
const loadError = new Error(
347-
`Failed to load resource from: ${finalUrl}`
348-
);
349-
const categorizedError = categorizeError(
350-
loadError,
351-
fileType,
352-
finalUrl
353-
);
354-
reject(categorizedError); // Ensure reject is called on error
355-
log(`Failed to load resource from: ${finalUrl}`, "warn");
356-
resourceStates[url] = "unloaded";
357-
if (retryCount < retries) {
358-
log(`Retrying to load resource: ${finalUrl}`, "warn");
359-
setTimeout(() => loadResource(url, retryCount + 1), retryDelay);
360-
}
361-
};
337+
applyAttributes(element, attributes, fileType);
338+
startedLoading = true;
339+
timeoutId = setTimeout(handleTimeout, timeout);
340+
341+
element.onload = () => {
342+
if (!timedOut) {
343+
clearTimeout(timeoutId);
344+
log(`Resource loaded from: ${finalUrl}`, "verbose");
345+
resourceStates[url] = "loaded";
346+
resolve(); // Make sure resolve is correctly called here
347+
}
348+
};
349+
350+
element.onerror = () => {
351+
clearTimeout(timeoutId);
352+
const loadError = new Error(
353+
`Failed to load resource from: ${finalUrl}`
354+
);
355+
const categorizedError = categorizeError(
356+
loadError,
357+
fileType,
358+
finalUrl
359+
);
360+
reject(categorizedError); // Ensure reject is called on error
361+
log(`Failed to load resource from: ${finalUrl}`, "warn");
362+
resourceStates[url] = "unloaded";
363+
if (retryCount < retries) {
364+
log(`Retrying to load resource: ${finalUrl}`, "warn");
365+
setTimeout(() => loadResource(url, retryCount + 1), retryDelay);
366+
}
367+
};
368+
369+
if (element.tagName) {
370+
if (appendToBody && fileType === "js") {
371+
document.body.appendChild(element);
372+
} else {
373+
document.head.appendChild(element);
374+
}
375+
}
362376

363-
if (element.tagName) {
364-
if (appendToBody && fileType === "js") {
365-
document.body.appendChild(element);
377+
cancel = () => {
378+
clearTimeout(timeoutId);
379+
if (element && element.parentNode) {
380+
element.parentNode.removeChild(element);
381+
log(
382+
`Loading cancelled and element removed: ${finalUrl}`,
383+
"warn"
384+
);
385+
resourceStates[url] = "unloaded";
386+
}
387+
};
388+
};
389+
390+
if (
391+
fileType === "js" &&
392+
deferScriptsUntilReady &&
393+
document.readyState !== "complete"
394+
) {
395+
window.addEventListener("DOMContentLoaded", () => {
396+
log(
397+
`Deferring script load until DOM ready: ${finalUrl}`,
398+
"verbose"
399+
);
400+
loadScriptWhenReady();
401+
});
366402
} else {
367-
document.head.appendChild(element);
403+
loadScriptWhenReady();
368404
}
369-
}
370-
371-
cancel = () => {
372-
clearTimeout(timeoutId);
373-
if (element && element.parentNode) {
374-
element.parentNode.removeChild(element);
375-
log(`Loading cancelled and element removed: ${finalUrl}`, "warn");
376-
resourceStates[url] = "unloaded";
377-
}
378-
};
379-
};
380-
381-
if (
382-
fileType === "js" &&
383-
deferScriptsUntilReady &&
384-
document.readyState !== "complete"
385-
) {
386-
window.addEventListener("DOMContentLoaded", () => {
387-
log(`Deferring script load until DOM ready: ${finalUrl}`, "verbose");
388-
loadScriptWhenReady();
389-
});
390-
} else {
391-
loadScriptWhenReady();
392-
}
393-
394-
resourceLoadedPromises[url] = {
395-
promise: new Promise((resolve, reject) => {
396-
loadScriptWhenReady(resolve, reject);
397405
}).catch((err) => {
398406
log(`Error loading resource: ${url}`, "warn");
399407
return Promise.resolve();
400408
}),
401409
cancel,
402-
};
403-
404-
return resourceLoadedPromises[url].promise;
410+
}).promise;
405411
};
406412

407413
const loadWithConcurrencyLimit = async (
@@ -426,7 +432,7 @@ const ResourceLoader = (() => {
426432
await processNext();
427433
};
428434

429-
await loadWithConcurrencyLimit(sortedUrls, loadResource, maxConcurrency);
435+
await loadWithConcurrencyLimit(urls, loadResource, maxConcurrency);
430436
}
431437

432438
function unloadResource(url) {

0 commit comments

Comments
 (0)