UNPKG

threads

Version:

Web workers & worker threads as simple as a function call

74 lines (73 loc) 3.33 kB
"use strict"; // tslint:disable max-classes-per-file Object.defineProperty(exports, "__esModule", { value: true }); exports.isWorkerRuntime = exports.getWorkerImplementation = exports.defaultPoolSize = void 0; const get_bundle_url_browser_1 = require("./get-bundle-url.browser"); exports.defaultPoolSize = typeof navigator !== "undefined" && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4; const isAbsoluteURL = (value) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(value); function createSourceBlobURL(code) { const blob = new Blob([code], { type: "application/javascript" }); return URL.createObjectURL(blob); } function selectWorkerImplementation() { if (typeof Worker === "undefined") { // Might happen on Safari, for instance // The idea is to only fail if the constructor is actually used return class NoWebWorker { constructor() { throw Error("No web worker implementation available. You might have tried to spawn a worker within a worker in a browser that doesn't support workers in workers."); } }; } class WebWorker extends Worker { constructor(url, options) { var _a, _b; if (typeof url === "string" && options && options._baseURL) { url = new URL(url, options._baseURL); } else if (typeof url === "string" && !isAbsoluteURL(url) && get_bundle_url_browser_1.getBundleURL().match(/^file:\/\//i)) { url = new URL(url, get_bundle_url_browser_1.getBundleURL().replace(/\/[^\/]+$/, "/")); if ((_a = options === null || options === void 0 ? void 0 : options.CORSWorkaround) !== null && _a !== void 0 ? _a : true) { url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`); } } if (typeof url === "string" && isAbsoluteURL(url)) { // Create source code blob loading JS file via `importScripts()` // to circumvent worker CORS restrictions if ((_b = options === null || options === void 0 ? void 0 : options.CORSWorkaround) !== null && _b !== void 0 ? _b : true) { url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`); } } super(url, options); } } class BlobWorker extends WebWorker { constructor(blob, options) { const url = window.URL.createObjectURL(blob); super(url, options); } static fromText(source, options) { const blob = new window.Blob([source], { type: "text/javascript" }); return new BlobWorker(blob, options); } } return { blob: BlobWorker, default: WebWorker }; } let implementation; function getWorkerImplementation() { if (!implementation) { implementation = selectWorkerImplementation(); } return implementation; } exports.getWorkerImplementation = getWorkerImplementation; function isWorkerRuntime() { const isWindowContext = typeof self !== "undefined" && typeof Window !== "undefined" && self instanceof Window; return typeof self !== "undefined" && self.postMessage && !isWindowContext ? true : false; } exports.isWorkerRuntime = isWorkerRuntime;