All files / utils/src requireScript.ts

0% Statements 0/35
0% Branches 0/14
0% Functions 0/8
0% Lines 0/35

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                                                                                   
const _importedScript: { [src: string]: true } = {};
 
/**
 * load dependency by css tag
 */
export function requireCss(src: string): Promise<void> {
  const headElement = document && (document.head || document.getElementsByTagName('head')[0]);
 
  return new Promise((resolve, reject) => {
    if (!document || src in _importedScript) {
      resolve();
      return;
    }
    const script = document.createElement('link');
    script.type = 'text/css';
    script.rel = 'stylesheet';
    script.href = src;
    script.onerror = (err) => {
      headElement!.removeChild(script);
      reject(new URIError(`The css ${src} is no accessible.`));
    };
    script.onload = () => {
      _importedScript[src] = true;
      resolve();
    };
    headElement!.appendChild(script);
  });
}
 
/**
 * load dependency by script tag
 */
export function requireScript(src: string): Promise<void> {
  const headElement = document && (document.head || document.getElementsByTagName('head')[0]);
 
  return new Promise((resolve, reject) => {
    if (!document || src in _importedScript) {
      resolve();
      return;
    }
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.id = '_react_baidu_map';
    script.src = src;
    script.async = true;
    script.defer = true;
    script.onerror = (err) => {
      headElement!.removeChild(script);
      reject(new URIError(`The Script ${src} is no accessible.`));
    };
    script.onload = () => {
      _importedScript[src] = true;
      resolve();
    };
    headElement!.appendChild(script);
  });
}