mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-16 07:30:01 -04:00
gyatt
This commit is contained in:
parent
86abdca21e
commit
5a5a5debdb
13 changed files with 314 additions and 222 deletions
|
@ -1,13 +1,8 @@
|
|||
import { BareTransport } from "./BareTypes";
|
||||
|
||||
self.BCC_VERSION = "2.1.3";
|
||||
self.BCC_VERSION = "3.0.2";
|
||||
console.warn("BCC_VERSION: " + self.BCC_VERSION);
|
||||
|
||||
if (!("gTransports" in globalThis)) {
|
||||
globalThis.gTransports = {};
|
||||
}
|
||||
|
||||
|
||||
declare global {
|
||||
interface ServiceWorkerGlobalScope {
|
||||
gSwitcher: Switcher;
|
||||
|
@ -27,23 +22,48 @@ declare global {
|
|||
}
|
||||
|
||||
class Switcher {
|
||||
transports: Record<string, BareTransport> = {};
|
||||
active: BareTransport | null = null;
|
||||
|
||||
channel = new BroadcastChannel("bare-mux");
|
||||
|
||||
constructor() {
|
||||
this.channel.addEventListener("message", ({ data: { type, data } }) => {
|
||||
console.log(type, data, "ServiceWorker" in globalThis);
|
||||
switch (type) {
|
||||
case "setremote":
|
||||
// this.active = new RemoteClient
|
||||
break;
|
||||
case "set":
|
||||
const { name, config } = data;
|
||||
this.active = new ((0, eval)(name))(...config);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function findSwitcher(): Switcher {
|
||||
if (globalThis.gSwitcher) return globalThis.gSwitcher;
|
||||
if ("ServiceWorkerGlobalScope" in globalThis) {
|
||||
globalThis.gSwitcher = new Switcher;
|
||||
return globalThis.gSwitcher;
|
||||
}
|
||||
|
||||
let _parent: any = window;
|
||||
for (let i = 0; i < 20; i++) {
|
||||
try {
|
||||
parent = parent.parent;
|
||||
if (parent && parent["gSwitcher"]) {
|
||||
if (_parent == _parent.parent) {
|
||||
globalThis.gSwitcher = new Switcher;
|
||||
return globalThis.gSwitcher;
|
||||
}
|
||||
_parent = _parent.parent;
|
||||
|
||||
if (_parent && _parent["gSwitcher"]) {
|
||||
console.warn("found implementation on parent");
|
||||
globalThis.gSwitcher = parent["gSwitcher"];
|
||||
return parent["gSwitcher"];
|
||||
globalThis.gSwitcher = _parent["gSwitcher"];
|
||||
return _parent["gSwitcher"];
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
globalThis.gSwitcher = new Switcher;
|
||||
return globalThis.gSwitcher;
|
||||
}
|
||||
|
@ -51,17 +71,16 @@ export function findSwitcher(): Switcher {
|
|||
|
||||
throw "unreachable";
|
||||
}
|
||||
findSwitcher();
|
||||
|
||||
export function AddTransport(name: string, client: BareTransport) {
|
||||
|
||||
export function SetTransport(name: string, ...config: any[]) {
|
||||
let switcher = findSwitcher();
|
||||
|
||||
switcher.transports[name] = client;
|
||||
if (!switcher.active)
|
||||
switcher.active = switcher.transports[name];
|
||||
switcher.active = new ((0, eval)(name))(...config);
|
||||
switcher.channel.postMessage({ type: "set", data: { name, config } });
|
||||
}
|
||||
|
||||
export function SetTransport(name: string) {
|
||||
export function SetSingletonTransport(client: BareTransport) {
|
||||
let switcher = findSwitcher();
|
||||
switcher.active = switcher.transports[name];
|
||||
switcher.active = client;
|
||||
switcher.channel.postMessage({ type: "setremote" });
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue