mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 14:50:03 -04:00
refactor out sharedworker construction, add logging
This commit is contained in:
parent
9d7e6f3c9c
commit
3fd8d4e293
2 changed files with 30 additions and 26 deletions
|
@ -46,6 +46,23 @@ type BroadcastMessage = {
|
||||||
path?: string,
|
path?: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createPort(path: string, channel: BroadcastChannel): MessagePort {
|
||||||
|
const worker = new SharedWorker(path, "bare-mux-worker");
|
||||||
|
navigator.serviceWorker.addEventListener("message", event => {
|
||||||
|
if (event.data.type === "getPort" && event.data.port) {
|
||||||
|
console.debug("bare-mux: recieved request for port from sw");
|
||||||
|
event.data.port.postMessage(worker.port, [worker.port]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
channel.onmessage = (event: MessageEvent) => {
|
||||||
|
if (event.data.type === "getPath") {
|
||||||
|
console.debug("bare-mux: recieved request for worker path from broadcast channel");
|
||||||
|
channel.postMessage(<BroadcastMessage>{ type: "path", path: path });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return worker.port;
|
||||||
|
}
|
||||||
|
|
||||||
export class WorkerConnection {
|
export class WorkerConnection {
|
||||||
channel: BroadcastChannel;
|
channel: BroadcastChannel;
|
||||||
port: MessagePort | Promise<MessagePort>;
|
port: MessagePort | Promise<MessagePort>;
|
||||||
|
@ -62,34 +79,14 @@ export class WorkerConnection {
|
||||||
} else if (workerPath && SharedWorker) {
|
} else if (workerPath && SharedWorker) {
|
||||||
// running in a window, was passed a workerPath
|
// running in a window, was passed a workerPath
|
||||||
// create the SharedWorker and help other bare-mux clients get the workerPath
|
// create the SharedWorker and help other bare-mux clients get the workerPath
|
||||||
navigator.serviceWorker.addEventListener("message", event => {
|
this.port = createPort(workerPath, this.channel);
|
||||||
if (event.data.type === "getPort" && event.data.port) {
|
|
||||||
const worker = new SharedWorker(workerPath, "bare-mux-worker");
|
|
||||||
event.data.port.postMessage(worker.port, [worker.port]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.channel.onmessage = (event: MessageEvent) => {
|
|
||||||
if (event.data.type === "getPath") {
|
|
||||||
this.channel.postMessage(<BroadcastMessage>{ type: "path", path: workerPath });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const worker = new SharedWorker(workerPath, "bare-mux-worker");
|
|
||||||
this.port = worker.port;
|
|
||||||
} else if (SharedWorker) {
|
} else if (SharedWorker) {
|
||||||
// running in a window, was not passed a workerPath
|
// running in a window, was not passed a workerPath
|
||||||
// ask other bare-mux clients for the workerPath
|
// ask other bare-mux clients for the workerPath
|
||||||
this.port = new Promise(resolve => {
|
this.port = new Promise(resolve => {
|
||||||
this.channel.onmessage = (event: MessageEvent) => {
|
this.channel.onmessage = (event: MessageEvent) => {
|
||||||
if (event.data.type === "path") {
|
if (event.data.type === "path") {
|
||||||
const worker = new SharedWorker(event.data.path, "bare-mux-worker");
|
resolve(createPort(event.data.path, this.channel));
|
||||||
this.channel.onmessage = (event: MessageEvent) => {
|
|
||||||
if (event.data.type === "getPath") {
|
|
||||||
this.channel.postMessage(<BroadcastMessage>{ type: "path", path: event.data.path });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resolve(worker.port);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.channel.postMessage(<BroadcastMessage>{ type: "getPath" });
|
this.channel.postMessage(<BroadcastMessage>{ type: "getPath" });
|
||||||
|
|
|
@ -3,6 +3,13 @@ import { WorkerMessage, WorkerResponse } from "./connection"
|
||||||
|
|
||||||
let currentTransport: BareTransport | null = null;
|
let currentTransport: BareTransport | null = null;
|
||||||
|
|
||||||
|
function noClients(): Error {
|
||||||
|
// @ts-expect-error mdn error constructor: new Error(message, options)
|
||||||
|
return new Error("there are no bare clients", {
|
||||||
|
cause: "No BareTransport was set. Try creating a BareMuxConnection and calling setTransport() or setManualTransport() on it before using BareClient."
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function handleConnection(port: MessagePort) {
|
function handleConnection(port: MessagePort) {
|
||||||
port.onmessage = async (event: MessageEvent) => {
|
port.onmessage = async (event: MessageEvent) => {
|
||||||
const port = event.data.port;
|
const port = event.data.port;
|
||||||
|
@ -10,7 +17,7 @@ function handleConnection(port: MessagePort) {
|
||||||
|
|
||||||
if (message.type === "set") {
|
if (message.type === "set") {
|
||||||
try {
|
try {
|
||||||
const AsyncFunction = (async function () {}).constructor;
|
const AsyncFunction = (async function() { }).constructor;
|
||||||
|
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
const func = new AsyncFunction(message.client);
|
const func = new AsyncFunction(message.client);
|
||||||
|
@ -18,12 +25,12 @@ function handleConnection(port: MessagePort) {
|
||||||
console.log("set transport to ", currentTransport);
|
console.log("set transport to ", currentTransport);
|
||||||
|
|
||||||
port.postMessage(<WorkerResponse>{ type: "set" });
|
port.postMessage(<WorkerResponse>{ type: "set" });
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
port.postMessage(<WorkerResponse>{ type: "error", error: err });
|
port.postMessage(<WorkerResponse>{ type: "error", error: err });
|
||||||
}
|
}
|
||||||
} else if (message.type === "fetch") {
|
} else if (message.type === "fetch") {
|
||||||
try {
|
try {
|
||||||
if (!currentTransport) throw new Error("No BareTransport was set. Try creating a BareMuxConnection and calling set() on it.");
|
if (!currentTransport) throw noClients();
|
||||||
if (!currentTransport.ready) await currentTransport.init();
|
if (!currentTransport.ready) await currentTransport.init();
|
||||||
|
|
||||||
const resp = await currentTransport.request(
|
const resp = await currentTransport.request(
|
||||||
|
@ -44,7 +51,7 @@ function handleConnection(port: MessagePort) {
|
||||||
}
|
}
|
||||||
} else if (message.type === "websocket") {
|
} else if (message.type === "websocket") {
|
||||||
try {
|
try {
|
||||||
if (!currentTransport) throw new Error("No BareTransport was set. Try creating a BareMuxConnection and calling set() on it.");
|
if (!currentTransport) throw noClients();
|
||||||
if (!currentTransport.ready) await currentTransport.init();
|
if (!currentTransport.ready) await currentTransport.init();
|
||||||
|
|
||||||
const onopen = (protocol: string) => {
|
const onopen = (protocol: string) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue