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

refactor: webpack SharedWorker #178

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
},
"publicPath": {
"type": "string"
},
"isSharedWorker": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down
16 changes: 12 additions & 4 deletions src/workers/InlineWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

var URL = window.URL || window.webkitURL;

module.exports = function (content, url) {
function CreateWorker(url, isSharedWorker) {
if (isSharedWorker) {
return new isSharedWorker(url);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new isSharedWorker(url);
return new SharedWorker(url);

} else {
return Worker(url);
}
}

module.exports = function (content, url, options) {
try {
try {
var blob;
Expand All @@ -24,15 +32,15 @@ module.exports = function (content, url) {
blob = new Blob([content]);
}

return new Worker(URL.createObjectURL(blob));
return CreateWorker(URL.createObjectURL(blob), options.isSharedWorker);
} catch (e) {
return new Worker('data:application/javascript,' + encodeURIComponent(content));
return CreateWorker('data:application/javascript,' + encodeURIComponent(content), options.isSharedWorker);
}
} catch (e) {
if (!url) {
throw Error('Inline worker is not supported');
}

return new Worker(url);
return CreateWorker(url, options.isSharedWorker);
}
};
5 changes: 3 additions & 2 deletions src/workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ const getWorker = (file, content, options) => {

return `require(${InlineWorkerPath})(${JSON.stringify(
content
)}, ${fallbackWorkerPath})`;
)}, ${fallbackWorkerPath}, ${options})`;
}

return `new Worker(${publicWorkerPath})`;
const Worker = options.isSharedWorker ? 'SharedWorker' : 'Worker';
return `new ${Worker}(${publicWorkerPath})`;
};

export default getWorker;