{"version":3,"sources":["share.js"],"sourcesContent":["import initC from \"x/initC.js\"\n\nconst W = \"/W.js\"\n\nexport const [hook, send] = initC(\n\tglobalThis.SharedWorker ? new SharedWorker(W).port : new Worker(W),\n)\n\n// TODO : check worker port is available\n// https://stackoverflow.com/questions/13662089/javascript-how-to-know-if-a-connection-with-a-shared-worker-is-still-alive/71499332#71499332\n//\n// class Port {\n// \tconstructor(port, onMessage, onDisconnect, onError) {\n// \t\tthis.onDisconnect = onDisconnect;\n// \t\tthis.disconnected = false;\n// \t\tthis.weakRef = new WeakRef(port);\n// \t\tport.onmessage = (e) => onMessage(e.data);\n// \t\tif (onError) {\n// \t\t\tport.onmessageerror = (e) => onError(e);\n// \t\t}\n// \t\tif (\"start\" in port) {\n// \t\t\tport.start();\n// \t\t}\n// \t}\n// \tisAlive() {\n// \t\tif (this.disconnected) {\n// \t\t\t// May occur, if the port was given away while alive, but response to it came after it \"died\"\n// \t\t\treturn false;\n// \t\t} else if (!this.weakRef.deref()) {\n// \t\t\t// If port is no longer accessible, call destructor\n// \t\t\tthis.onDisconnect();\n// \t\t\tthis.disconnected = true;\n// \t\t\treturn false;\n// \t\t}\n// \t\treturn true;\n// \t}\n// \tpostMessage(message, options) {\n// \t\ttry {\n// \t\t\tconst port = this.weakRef.deref();\n// \t\t\tif (!port) {\n// \t\t\t\tthrow new TypeError(`Port is no longer reference-able`);\n// \t\t\t}\n// \t\t\t// In some browsers, if the other side of the port is no longer available, it will throw an error\n// \t\t\tport.postMessage(message, options);\n// \t\t} catch (_a) {\n// \t\t\tthis.onDisconnect();\n// \t\t\tthis.disconnected = true;\n// \t\t}\n// \t}\n// \tclose() {\n// \t\tvar _a;\n// \t\tthis.onDisconnect();\n// \t\tthis.disconnected = true;\n// \t\t(_a = this.weakRef.deref()) === null || _a === void 0 ? void 0 : _a.close();\n// \t}\n// }\n//\n// export class PortAwareSharedWorker {\n// \tconstructor(port, messageHandle, disconnectHandle) {\n// \t\tthis.messageHandle = messageHandle;\n// \t\tthis.disconnectHandle = disconnectHandle;\n// \t\tthis.portsSet = new Set();\n// \t\tthis.initializePort(port);\n// \t\t// Poll based check to delete\n// \t\tsetInterval(() => {\n// \t\t\tfor (const port of this.portsSet) {\n// \t\t\t\tport.isAlive();\n// \t\t\t}\n// \t\t}, 100);\n// \t}\n// \tinitializePort(port) {\n// \t\tconst portWrapper = new Port(\n// \t\t\tport,\n// \t\t\t(data) => this.messageHandle(portWrapper, data, this.getOpenPorts()),\n// \t\t\t() => this.disconnectHandle(portWrapper, this.getOpenPorts()),\n// \t\t);\n// \t\tthis.portsSet.add(portWrapper);\n// \t}\n// \t/**\n// \t * Gets all currently opened ports. May also return some ports that are no longer active,\n// \t * but over time all inactive ports will be gone.\n// \t *\n// \t * @see(https://html.spec.whatwg.org/multipage/web-messaging.html#ports-and-garbage-collection)\n// \t */\n// \tgetOpenPorts() {\n// \t\tconst remainingPorts = [];\n// \t\tfor (const port of this.portsSet) {\n// \t\t\tif (port.isAlive()) {\n// \t\t\t\tremainingPorts.push(port);\n// \t\t\t} else {\n// \t\t\t\tthis.portsSet.delete(port);\n// \t\t\t}\n// \t\t}\n// \t\treturn remainingPorts;\n// \t}\n// \tstatic initializeProxy(\n// \t\t/** Pass `self` to this. The `self` can be from a `Worker` or `ServiceWorker` for convince */\n// \t\tglobal,\n// \t\tmessageHandle,\n// \t\tdisconnectHandle,\n// \t) {\n// \t\treturn new Promise((resolve) => {\n// \t\t\tif (PortAwareSharedWorker.instance) {\n// \t\t\t\treturn resolve(PortAwareSharedWorker.instance);\n// \t\t\t}\n// \t\t\tglobal.onconnect = function sharedConnectCallback(e) {\n// \t\t\t\tif (PortAwareSharedWorker.instance) {\n// \t\t\t\t\tPortAwareSharedWorker.instance.initializePort(e.ports[0]);\n// \t\t\t\t\treturn;\n// \t\t\t\t}\n// \t\t\t\tresolve(\n// \t\t\t\t\t(PortAwareSharedWorker.instance = new PortAwareSharedWorker(\n// \t\t\t\t\t\te.ports[0],\n// \t\t\t\t\t\tmessageHandle,\n// \t\t\t\t\t\tdisconnectHandle,\n// \t\t\t\t\t)),\n// \t\t\t\t);\n// \t\t\t};\n// \t\t\t// This is the fallback, just in case the browser doesn't support SharedWorkers\n// \t\t\tif (!(\"SharedWorkerGlobalScope\" in global)) {\n// \t\t\t\tresolve(\n// \t\t\t\t\t(PortAwareSharedWorker.instance = new PortAwareSharedWorker(\n// \t\t\t\t\t\tglobal,\n// \t\t\t\t\t\tmessageHandle,\n// \t\t\t\t\t\tdisconnectHandle,\n// \t\t\t\t\t)),\n// \t\t\t\t);\n// \t\t\t}\n// \t\t});\n// \t}\n// }\n"],"names":["initC","W","hook","send","globalThis","SharedWorker","port","Worker"],"rangeMappings":"","mappings":"AAAA,OAAOA,MAAW,YAAY,CAE9B,IAAMC,EAAI,OAEV,QAAO,KAAM,CAACC,KAAMC,KAAK,CAAGH,EAC3BI,WAAWC,YAAY,CAAG,IAAIA,aAAaJ,GAAGK,IAAI,CAAG,IAAIC,OAAON,GAChE"}