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 (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewSteel authored Feb 11, 2021
1 parent edca167 commit 2abd129
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"webpack": "^4.0.0 || ^5.0.0"
},
"dependencies": {
"schema-utils": "^3.0.0",
"loader-utils": "^2.0.0"
"loader-utils": "^2.0.0",
"schema-utils": "^3.0.0"
},
"devDependencies": {
"@babel/cli": "^7.12.8",
Expand Down
22 changes: 13 additions & 9 deletions src/runtime/inline.js
Original file line number Diff line number Diff line change
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 2abd129

Please sign in to comment.