mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 14:50:03 -04:00
tell the sw to refresh its port when a new shared worker gets made
This commit is contained in:
parent
fb81b33714
commit
fde90f3ec1
2 changed files with 12 additions and 4 deletions
|
@ -32,8 +32,8 @@ export type WorkerResponse = {
|
||||||
error?: Error,
|
error?: Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
type BroadcastMessage = {
|
export type BroadcastMessage = {
|
||||||
type: "getPath" | "path",
|
type: "getPath" | "path" | "refreshPort",
|
||||||
path?: string,
|
path?: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,8 +89,13 @@ export class WorkerConnection {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
if (self.clients) {
|
if (self.clients) {
|
||||||
// running in a ServiceWorker
|
// running in a ServiceWorker
|
||||||
// ask a window for the worker port
|
// ask a window for the worker port, register for refreshPort
|
||||||
this.port = searchForPort();
|
this.port = searchForPort();
|
||||||
|
this.channel.onmessage = (event: MessageEvent) => {
|
||||||
|
if (event.data.type === "refreshPort") {
|
||||||
|
this.port = searchForPort();
|
||||||
|
}
|
||||||
|
}
|
||||||
} 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
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
import { BareTransport } from "./baretypes";
|
import { BareTransport } from "./baretypes";
|
||||||
import { WorkerMessage, WorkerResponse } from "./connection"
|
import { BroadcastMessage, WorkerMessage, WorkerResponse } from "./connection"
|
||||||
|
|
||||||
let currentTransport: BareTransport | null = null;
|
let currentTransport: BareTransport | null = null;
|
||||||
let currentTransportName: string = "";
|
let currentTransportName: string = "";
|
||||||
|
|
||||||
|
const channel = new BroadcastChannel("bare-mux");
|
||||||
|
channel.postMessage(<BroadcastMessage>{ type: "refreshPort" });
|
||||||
|
|
||||||
function noClients(): Error {
|
function noClients(): Error {
|
||||||
// @ts-expect-error mdn error constructor: new Error(message, options)
|
// @ts-expect-error mdn error constructor: new Error(message, options)
|
||||||
return new Error("there are no bare clients", {
|
return new Error("there are no bare clients", {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue