@mercuryworkshop/bare-mux
Advanced tools
Comparing version 1.0.9 to 1.1.0
@@ -101,3 +101,3 @@ import { v4 } from 'uuid'; | ||
//@ts-expect-error not installing node types for this one thing | ||
self.BCC_VERSION = "1.0.9"; | ||
self.BCC_VERSION = "1.1.0"; | ||
console.debug("BARE_MUX_VERSION: " + self.BCC_VERSION); | ||
@@ -112,5 +112,6 @@ function initTransport(name, config) { | ||
channel = new BroadcastChannel("bare-mux"); | ||
data = null; | ||
constructor() { | ||
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) { | ||
@@ -124,2 +125,7 @@ case "setremote": | ||
break; | ||
case "find": | ||
if (this.data) { | ||
this.channel.postMessage(this.data); | ||
} | ||
break; | ||
} | ||
@@ -130,2 +136,5 @@ }); | ||
function findSwitcher() { | ||
if ("ServiceWorkerGlobalScope" in globalThis && globalThis.gSwitcher && !globalThis.gSwitcher.active) { | ||
globalThis.gSwitcher.channel.postMessage({ type: "find" }); | ||
} | ||
if (globalThis.gSwitcher) | ||
@@ -135,2 +144,3 @@ return globalThis.gSwitcher; | ||
globalThis.gSwitcher = new Switcher; | ||
globalThis.gSwitcher.channel.postMessage({ type: "find" }); | ||
return globalThis.gSwitcher; | ||
@@ -154,2 +164,3 @@ } | ||
globalThis.gSwitcher = new Switcher; | ||
globalThis.gSwitcher.channel.postMessage({ type: "find" }); | ||
return globalThis.gSwitcher; | ||
@@ -164,3 +175,4 @@ } | ||
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); | ||
} | ||
@@ -171,3 +183,4 @@ async function SetSingletonTransport(client) { | ||
switcher.active = client; | ||
switcher.channel.postMessage({ type: "setremote" }); | ||
switcher.data = { type: "setremote", data: { name: client.constructor.name } }; | ||
switcher.channel.postMessage(switcher.data); | ||
} | ||
@@ -344,2 +357,7 @@ | ||
let switcher = findSwitcher(); | ||
if (!switcher.active) { | ||
// in race conditions we trust | ||
await new Promise(r => setTimeout(r, 1000)); | ||
switcher = findSwitcher(); | ||
} | ||
if (!switcher.active) | ||
@@ -346,0 +364,0 @@ throw "there are no bare clients"; |
@@ -22,2 +22,3 @@ import { BareTransport } from "./BareTypes"; | ||
channel: BroadcastChannel; | ||
data: Object | null; | ||
constructor(); | ||
@@ -24,0 +25,0 @@ } |
{ | ||
"name": "@mercuryworkshop/bare-mux", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
112676
975