mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-15 23:20:02 -04:00
add probing for active switcher when not found
This commit is contained in:
parent
84827c9541
commit
80368ba682
2 changed files with 19 additions and 5 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@mercuryworkshop/bare-mux",
|
"name": "@mercuryworkshop/bare-mux",
|
||||||
"version": "1.0.9",
|
"version": "1.1.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -29,13 +29,15 @@ function initTransport(name: string, config: any) {
|
||||||
return cl;
|
return cl;
|
||||||
}
|
}
|
||||||
class Switcher {
|
class Switcher {
|
||||||
active: BareTransport | null = null;
|
active: BareTransport | null = null
|
||||||
|
|
||||||
channel = new BroadcastChannel("bare-mux");
|
channel = new BroadcastChannel("bare-mux");
|
||||||
|
|
||||||
|
data: Object | null = null
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.channel.addEventListener("message", ({ data: { type, data } }) => {
|
this.channel.addEventListener("message", ({ data: { type, data } }) => {
|
||||||
console.log(type, data, "ServiceWorker" in globalThis);
|
console.log(`bare-mux: ${type}`, data, `${"ServiceWorker" in globalThis}`);
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "setremote":
|
case "setremote":
|
||||||
this.active = new RemoteTransport
|
this.active = new RemoteTransport
|
||||||
|
@ -44,15 +46,24 @@ class Switcher {
|
||||||
const { name, config } = data;
|
const { name, config } = data;
|
||||||
this.active = initTransport(name, config);
|
this.active = initTransport(name, config);
|
||||||
break;
|
break;
|
||||||
|
case "find":
|
||||||
|
if (this.data) {
|
||||||
|
this.channel.postMessage(this.data)
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findSwitcher(): Switcher {
|
export function findSwitcher(): Switcher {
|
||||||
|
if ("ServiceWorkerGlobalScope" in globalThis && globalThis.gSwitcher && !globalThis.gSwitcher.active) {
|
||||||
|
globalThis.gSwitcher.channel.postMessage({ type: "find" })
|
||||||
|
}
|
||||||
if (globalThis.gSwitcher) return globalThis.gSwitcher;
|
if (globalThis.gSwitcher) return globalThis.gSwitcher;
|
||||||
if ("ServiceWorkerGlobalScope" in globalThis) {
|
if ("ServiceWorkerGlobalScope" in globalThis) {
|
||||||
globalThis.gSwitcher = new Switcher;
|
globalThis.gSwitcher = new Switcher;
|
||||||
|
globalThis.gSwitcher.channel.postMessage({ type: "find" })
|
||||||
return globalThis.gSwitcher;
|
return globalThis.gSwitcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,6 +83,7 @@ export function findSwitcher(): Switcher {
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
globalThis.gSwitcher = new Switcher;
|
globalThis.gSwitcher = new Switcher;
|
||||||
|
globalThis.gSwitcher.channel.postMessage({ type: "find" })
|
||||||
return globalThis.gSwitcher;
|
return globalThis.gSwitcher;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,12 +95,14 @@ findSwitcher();
|
||||||
export function SetTransport(name: string, ...config: any[]) {
|
export function SetTransport(name: string, ...config: any[]) {
|
||||||
let switcher = findSwitcher();
|
let switcher = findSwitcher();
|
||||||
switcher.active = initTransport(name, config);
|
switcher.active = initTransport(name, config);
|
||||||
switcher.channel.postMessage({ type: "set", data: { name, config } });
|
switcher.data = { type: "set", data: { name, config } }
|
||||||
|
switcher.channel.postMessage(switcher.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function SetSingletonTransport(client: BareTransport) {
|
export async function SetSingletonTransport(client: BareTransport) {
|
||||||
let switcher = findSwitcher();
|
let switcher = findSwitcher();
|
||||||
await client.init();
|
await client.init();
|
||||||
switcher.active = client;
|
switcher.active = client;
|
||||||
switcher.channel.postMessage({ type: "setremote" });
|
switcher.data = { type: "setremote", data: { name: client.constructor.name }}
|
||||||
|
switcher.channel.postMessage(switcher.data);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue