Split service worker post message handling out to function

pull/27326/head
Travis Ralston 2024-04-23 13:46:20 -06:00
parent 80dd41508b
commit 0395ee450e
1 changed files with 17 additions and 15 deletions

View File

@ -58,27 +58,29 @@ export default class WebPlatform extends VectorBasePlatform {
return r;
})
.then((r) => {
navigator.serviceWorker.addEventListener("message", (e) => {
try {
if (e.data?.["type"] === "userinfo" && e.data?.["responseKey"]) {
const userId = localStorage.getItem("mx_user_id");
const deviceId = localStorage.getItem("mx_device_id");
r.active!.postMessage({
responseKey: e.data["responseKey"],
userId,
deviceId,
});
}
} catch (e) {
console.error("Error responding to service worker: ", e);
}
});
navigator.serviceWorker.addEventListener("message", this.onServiceWorkerPostMessage.bind(this));
})
.catch((e) => console.error("Error registering/updating service worker:", e));
}
}
}
private onServiceWorkerPostMessage(event: MessageEvent): void {
try {
if (event.data?.["type"] === "userinfo" && event.data?.["responseKey"]) {
const userId = localStorage.getItem("mx_user_id");
const deviceId = localStorage.getItem("mx_device_id");
event.source!.postMessage({
responseKey: event.data["responseKey"],
userId,
deviceId,
});
}
} catch (e) {
console.error("Error responding to service worker: ", e);
}
}
public getHumanReadableName(): string {
return "Web Platform"; // no translation required: only used for analytics
}