All files / Sources/IO/Core/ResourceLoader index.js

4.76% Statements 1/21
0% Branches 0/4
0% Functions 0/4
4.76% Lines 1/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 411x                                                                                
const LOADED_URLS = [];
 
export function loadScript(url) {
  return new Promise((resolve, reject) => {
    if (LOADED_URLS.indexOf(url) === -1) {
      LOADED_URLS.push(url);
      const newScriptTag = document.createElement('script');
      newScriptTag.type = 'text/javascript';
      newScriptTag.src = url;
      newScriptTag.onload = resolve;
      newScriptTag.onerror = reject;
      document.body.appendChild(newScriptTag);
    } else {
      resolve(false);
    }
  });
}
 
// <link href=/css/app.44f7bc81.css rel=preload as=style>
export function loadCSS(url) {
  return new Promise((resolve, reject) => {
    if (LOADED_URLS.indexOf(url) === -1) {
      LOADED_URLS.push(url);
      const newScriptTag = document.createElement('link');
      newScriptTag.rel = 'stylesheet';
      newScriptTag.href = url;
      newScriptTag.onload = resolve;
      newScriptTag.onerror = reject;
      document.head.appendChild(newScriptTag);
    } else {
      resolve(false);
    }
  });
}
 
export default {
  loadScript,
  loadCSS,
  LOADED_URLS,
};