All files / main/umd browser-worker-factory.ts

100% Statements 5/5
100% Branches 8/8
100% Functions 2/2
100% Lines 5/5

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 243x     2x       3x       3x               2x        
import { workerFactory } from '../worker-interface';
 
function isHtmlScriptElement(scriptElement: HTMLOrSVGScriptElement): scriptElement is HTMLScriptElement {
  return 'src' in scriptElement;
}
 
const THIS_SCRIPT_URL: string | undefined =
    (document && document.currentScript && isHtmlScriptElement(document.currentScript) && document.currentScript.src)
        ? document.currentScript.src
        : undefined;
 
workerFactory.createWorker = () => {
  // Notes:
  // 1. The expectation is that the current script is loaded through a <script> element with a context that sets
  //    document.currentScript. This excludes newer contexts such as running as a module script or running in a shadow
  //    tree. https://html.spec.whatwg.org/multipage/dom.html#dom-document-currentscript
  //    If you are seeing this error, either change the <script> element to a more traditional context, or use the ES6
  //    module distribution from dist/es6 and use a bundler for your web app.
  // 2. Current browsers do not support {type: 'module'} as `options` argument yet.
  return THIS_SCRIPT_URL === undefined
      ? 'Failed to create web worker because the URL of the current script could not be determined.'
      : new Worker(THIS_SCRIPT_URL);
};