Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
fix: make inline workers work from inside workers
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewSteel committed Feb 10, 2021
1 parent edca167 commit a7619bf
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/runtime/inline.js
Expand Up @@ -2,20 +2,21 @@
/* eslint-disable no-undef, no-use-before-define, new-cap */

module.exports = (content, workerConstructor, workerOptions, url) => {
const globalScope = (self || window);
try {
try {
let blob;

try {
// New API
blob = new window.Blob([content]);
blob = new globalScope.Blob([content]);
} catch (e) {
// BlobBuilder = Deprecated, but widely implemented
const BlobBuilder =
window.BlobBuilder ||
window.WebKitBlobBuilder ||
window.MozBlobBuilder ||
window.MSBlobBuilder;
globalScope.BlobBuilder ||
globalScope.WebKitBlobBuilder ||
globalScope.MozBlobBuilder ||
globalScope.MSBlobBuilder;

blob = new BlobBuilder();

Expand All @@ -24,15 +25,18 @@ module.exports = (content, workerConstructor, workerOptions, url) => {
blob = blob.getBlob();
}

const URL = window.URL || window.webkitURL;
const URL = globalScope.URL || globalScope.webkitURL;
const objectURL = URL.createObjectURL(blob);
const worker = new window[workerConstructor](objectURL, workerOptions);
const worker = new globalScope[workerConstructor](
objectURL,
workerOptions
);

URL.revokeObjectURL(objectURL);

return worker;
} catch (e) {
return new window[workerConstructor](
return new globalScope[workerConstructor](
`data:application/javascript,${encodeURIComponent(content)}`,
workerOptions
);
Expand All @@ -42,6 +46,6 @@ module.exports = (content, workerConstructor, workerOptions, url) => {
throw Error("Inline worker is not supported");
}

return new window[workerConstructor](url, workerOptions);
return new globalScope[workerConstructor](url, workerOptions);
}
};

0 comments on commit a7619bf

Please sign in to comment.