add a ping message to make sure worker port is alive

This commit is contained in:
Toshit Chawda 2024-07-08 12:59:37 -07:00
parent fde90f3ec1
commit 6e86375a69
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
2 changed files with 57 additions and 26 deletions

View file

@ -18,8 +18,9 @@ function handleConnection(port: MessagePort) {
port.onmessage = async (event: MessageEvent) => {
const port = event.data.port;
const message: WorkerMessage = event.data.message;
if (message.type === "set") {
if (message.type === "ping") {
port.postMessage(<WorkerResponse>{ type: "pong" });
} else if (message.type === "set") {
try {
const AsyncFunction = (async function() { }).constructor;
@ -32,6 +33,7 @@ function handleConnection(port: MessagePort) {
port.postMessage(<WorkerResponse>{ type: "set" });
} catch (err) {
console.error(err);
port.postMessage(<WorkerResponse>{ type: "error", error: err });
}
} else if (message.type === "get") {
@ -55,6 +57,7 @@ function handleConnection(port: MessagePort) {
port.postMessage(<WorkerResponse>{ type: "fetch", fetch: resp });
}
} catch (err) {
console.error(err);
port.postMessage(<WorkerResponse>{ type: "error", error: err });
}
} else if (message.type === "websocket") {
@ -98,6 +101,7 @@ function handleConnection(port: MessagePort) {
port.postMessage(<WorkerResponse>{ type: "websocket" });
} catch (err) {
console.error(err);
port.postMessage(<WorkerResponse>{ type: "error", error: err });
}
}