Add a 'noWebWorker=true' option to <replay-web-page> embed to disable loading in separate web worker (#102)

* add a 'noWebWorker' option to <replay-web-page> embed to disable the default webworker-based loading, and load via the service worker directly. this feature was previously available, but turned-off, as its less efficient (uses sw for what can be done in a separate web worker thread). however, service-worker based loading may be needed for certain edge cases (see #97), and this option enables this loading method to be turned back on for embedding.
* docs: update guide to include 'noWebWorker', also use include attribute for boolean attributes
* update CHANGES.md
* bump to 1.5.12
This commit is contained in:
Ilya Kreymer
2022-06-15 11:38:04 -07:00
committed by GitHub
parent 789e5ba2b1
commit 36da7fa2c6
10 changed files with 234 additions and 198 deletions
+7
View File
@@ -379,6 +379,13 @@ class ReplayWebApp extends LitElement
this.loadInfo.customColl = this.pageParams.get("customColl");
}
if (this.pageParams.get("noWebWorker") === "1") {
if (!this.loadInfo) {
this.loadInfo = {};
}
this.loadInfo.noWebWorker = true;
}
if (IS_APP && this.sourceUrl.startsWith("file://")) {
// eslint-disable-next-line no-undef
const url = new URL(__APP_FILE_SERVE_PREFIX__);
+10 -2
View File
@@ -29,6 +29,7 @@ class Embed extends LitElement
this.embed = null;
this.reloadCount = 0;
this.noSandbox = false;
this.noWebWorker = false;
}
static get properties() {
@@ -56,6 +57,7 @@ class Embed extends LitElement
deepLink: { type: Boolean },
noSandbox: { type: Boolean },
noWebWorker: { type: Boolean },
errorMessage: { type: String }
};
@@ -126,13 +128,19 @@ class Embed extends LitElement
const source = new URL(this.source, document.baseURI);
this.paramString = new URLSearchParams({
const params = {
source,
customColl: this.coll,
config: this.config,
basePageUrl: window.location.href.split("#")[0],
embed: this.embed,
}).toString();
};
if (this.noWebWorker) {
params.noWebWorker = "1";
}
this.paramString = new URLSearchParams(params).toString();
this.hashString = new URLSearchParams({
url: this.url,
+52 -24
View File
@@ -27,7 +27,9 @@ class Loader extends LitElement
this.errorAllowRetry = false;
this.pingInterval = "";
this.pingInterval = 0;
this.noWebWorker = false;
}
static get properties() {
@@ -56,11 +58,18 @@ class Loader extends LitElement
}
initMessages() {
// if (!navigator.serviceWorker) {
// return;
// }
// eslint-disable-next-line no-undef
this.worker = new Worker(__SW_NAME__);
this.noWebWorker = this.loadInfo && this.loadInfo.noWebWorker;
if (!this.noWebWorker) {
// eslint-disable-next-line no-undef
this.worker = new Worker(__SW_NAME__);
} else {
if (!navigator.serviceWorker) {
return;
}
this.worker = navigator.serviceWorker;
}
this.worker.addEventListener("message", (event) => {
switch (event.data.msg_type) {
@@ -94,11 +103,16 @@ class Loader extends LitElement
}
this.progress = this.total;
this.percent = 100;
if (this.pingInterval) {
clearInterval(this.pingInterval);
}
this.dispatchEvent(new CustomEvent("coll-loaded", {detail: event.data}));
this.worker.terminate();
if (!this.noWebWorker) {
this.worker.terminate();
} else {
if (this.pingInterval) {
clearInterval(this.pingInterval);
}
}
this.worker = null;
}
break;
}
@@ -187,15 +201,19 @@ You can select a file to upload from the main page by clicking the 'Choose File.
});
}
this.worker.postMessage(msg);
if (this.worker) {
if (!this.noWebWorker) {
this.worker.postMessage(msg);
} else {
navigator.serviceWorker.controller.postMessage(msg);
//navigator.serviceWorker.controller.postMessage(msg);
// ping service worker with messages to avoid shutdown while loading
// (mostly for Firefox)
// this.pingInterval = setInterval(() => {
// navigator.serviceWorker.controller.postMessage({"msg_type": "ping"});
// }, 15000);
// ping service worker with messages to avoid shutdown while loading
// (mostly for Firefox)
this.pingInterval = setInterval(() => {
navigator.serviceWorker.controller.postMessage({"msg_type": "ping"});
}, 15000);
}
}
}
googledriveInit() {
@@ -213,12 +231,22 @@ You can select a file to upload from the main page by clicking the 'Choose File.
}
onCancel() {
//if (navigator.serviceWorker && navigator.serviceWorker.controller) {
if (this.worker) {
this.worker.postMessage({"msg_type": "cancelLoad", "name": this.coll});
// if (this.pingInterval) {
// clearInterval(this.pingInterval);
// }
if (!this.worker) {
return;
}
const msg = {"msg_type": "cancelLoad", "name": this.coll};
if (!this.noWebWorker) {
this.worker.postMessage(msg);
return;
}
if (navigator.serviceWorker && navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.postMessage(msg);
if (this.pingInterval) {
clearInterval(this.pingInterval);
}
}
}
+1 -12
View File
@@ -34,17 +34,6 @@ Please try a different browser.
return p;
}
// ===========================================================================
let dbworker = null;
function initDBWorker() {
if (dbworker === null) {
// eslint-disable-next-line no-undef
dbworker = new Worker(__SW_NAME__);
}
return dbworker;
}
// ===========================================================================
async function digestMessage(message, hashtype) {
@@ -148,4 +137,4 @@ function parseURLSchemeHostPath(url) {
export { digestMessage, tsToDate, getTS, getPageDateTS, getReplayLink, sourceToId, parseURLSchemeHostPath,
registerSW, initDBWorker };
registerSW };