{"version":3,"sources":["thread-worker.ts"],"sourcesContent":["import type { MessageToWorker } from \"./threads.ts\";\nimport type { Datex as DatexType } from \"../mod.ts\";\nimport type { EndpointConfigData } from \"../runtime/endpoint_config.ts\";\n\nconst isServiceWorker = 'registration' in globalThis && (globalThis as any).registration instanceof ServiceWorkerRegistration;\n\nconsole.log(\"spawned new thread worker\")\n\nif (isServiceWorker) {\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim\n\tself.addEventListener(\"activate\", (event) => {\n\t\tconsole.log(\"service worker activated\")\n\t\t// @ts-ignore\n\t\tevent.waitUntil(clients.claim());\n\t});\n}\n\n\n// for import maps (TODO: fix import shim)\ndeclare const importShim: any;\n\nlet Datex: typeof DatexType;\nlet generateTSModuleForRemoteAccess: typeof import(\"../utils/interface-generator.ts\").generateTSModuleForRemoteAccess;\n\n\nasync function initDatex(url: string, datexConfig?: EndpointConfigData) {\n\t({Datex} = await import(url));\n\tconst {endpoint_config} = await import(new URL(\"./runtime/endpoint_config.ts\", url).toString());\n\tif (datexConfig) {\n\t\tDatex.Runtime.OPTIONS.USE_DX_CONFIG = false;\n\t\tDatex.Runtime.OPTIONS.USE_PUBLIC_NODES = false;\n\t\tendpoint_config.set(datexConfig)\n\t}\n\tawait Datex.Supranet.start();\n}\n\nasync function initTsInterfaceGenerator(url: string) {\n\t({generateTSModuleForRemoteAccess} = await import(url));\n}\n\nasync function getWorkerComInterface(url: string) {\n\treturn await import(url) as typeof import(\"../network/communication-interfaces/worker-interface.ts\");\n}\n\nasync function getCommunicationHub(url: string) {\n\treturn await import(url) as typeof import(\"../network/communication-hub.ts\");\n}\n\n\nasync function loadModule(url: string) {\n\tconst module = await import(url)\n\tconst remoteModule = await generateTSModuleForRemoteAccess(new URL(url), undefined, false)\n\tDatex.Runtime.endpoint_entrypoint = module;\n\treturn remoteModule;\n}\n\nlet messageTarget:{postMessage:(data:any)=>void} = self\n\naddEventListener(\"message\", async function (event) {\n\n\tconst data = event.data as MessageToWorker;\n\n\ttry {\n\t\t// init message port (only for service workers)\n\t\tif (data.type == \"INIT_PORT\") {\n\t\t\tmessageTarget = event.ports[0];\n\t\t}\n\t\telse if (data.type == \"INIT\") {\n\t\t\t// TODO:\n\t\t\t// await import(\"https://ga.jspm.io/npm:es-module-shims@1.8.0/dist/es-module-shims.wasm.js\");\n\t\t\t// if (data.importMap) importShim.addImportMap(data.importMap);\n\n\t\t\t// inherit theme from parent\n\t\t\t(globalThis as any)._override_console_theme = data.theme;\n\n\t\t\tawait initDatex(data.datexURL, data.datexConfig);\n\t\t\tconst {WorkerInterface} = await getWorkerComInterface(data.workerInterfaceURL);\n\t\t\tconst {communicationHub} = await getCommunicationHub(data.communicationHubURL);\n\n\t\t\tawait initTsInterfaceGenerator(data.tsInterfaceGeneratorURL);\n\t\t\tconst remoteModule = data.moduleURL ? await loadModule(data.moduleURL) : null;\n\t\t\t\n\t\t\t// connect via worker com interface\n\t\t\tconst endpoint = Datex.Target.get(data.endpoint as any) as DatexType.Endpoint;\n\n\t\t\t// TODO: Worker type?\n\t\t\tawait communicationHub.addInterface(new WorkerInterface(self as unknown as Worker, endpoint));\n\n\t\t\tmessageTarget.postMessage({type: \"INITIALIZED\", remoteModule, endpoint: Datex.Runtime.endpoint.toString()});\n\t\t\t// trust parent endpoint\n\t\t\tDatex.Runtime.addTrustedEndpoint(endpoint, [\"remote-js-execution\"]);\n\t\t}\n\t\t\n\t}\n\tcatch (e) {\n\t\tconsole.log(\"error\",e)\n\t\tmessageTarget.postMessage({type: \"ERROR\", error: e.stack??e})\n\t}\n\t\n})"],"names":["Datex","generateTSModuleForRemoteAccess","isServiceWorker","globalThis","registration","ServiceWorkerRegistration","initDatex","url","datexConfig","endpoint_config","URL","toString","Runtime","OPTIONS","USE_DX_CONFIG","USE_PUBLIC_NODES","set","Supranet","start","initTsInterfaceGenerator","getWorkerComInterface","getCommunicationHub","loadModule","module","remoteModule","undefined","endpoint_entrypoint","console","log","self","addEventListener","event","waitUntil","clients","claim","messageTarget","data","type","ports","_override_console_theme","theme","datexURL","WorkerInterface","workerInterfaceURL","communicationHub","communicationHubURL","tsInterfaceGeneratorURL","moduleURL","endpoint","Target","get","addInterface","postMessage","addTrustedEndpoint","e","error","stack"],"mappings":"IAqBIA,OACAC;AAlBJ,IAAMC,kBAAkB,kBAAkBC,cAAc,AAACA,WAAmBC,YAAY,YAAYC;AAqBpG,eAAeC,UAAUC,GAAW,EAAEC,WAAgC;IACpE,CAAA,EAACR,KAAK,EAAC,GAAG,MAAM,MAAM,CAACO,IAAG;IAC3B,IAAM,EAACE,eAAe,EAAC,GAAG,MAAM,MAAM,CAAC,IAAIC,IAAI,gCAAgCH,KAAKI,QAAQ;IACxFH,gBACHR,MAAMY,OAAO,CAACC,OAAO,CAACC,aAAa,GAAG,CAAA,GACtCd,MAAMY,OAAO,CAACC,OAAO,CAACE,gBAAgB,GAAG,CAAA,GACzCN,gBAAgBO,GAAG,CAACR,eAErB,MAAMR,MAAMiB,QAAQ,CAACC,KAAK;AAC3B;AAEA,eAAeC,yBAAyBZ,GAAW;IACjD,CAAA,EAACN,+BAA+B,EAAC,GAAG,MAAM,MAAM,CAACM,IAAG;AACtD;AAEA,eAAea,sBAAsBb,GAAW;IAC/C,OAAO,MAAM,MAAM,CAACA;AACrB;AAEA,eAAec,oBAAoBd,GAAW;IAC7C,OAAO,MAAM,MAAM,CAACA;AACrB;AAGA,eAAee,WAAWf,GAAW;IACpC,IAAMgB,SAAS,MAAM,MAAM,CAAChB,MACtBiB,eAAe,MAAMvB,gCAAgC,IAAIS,IAAIH,MAAMkB,KAAAA,GAAW,CAAA;IAEpF,OADAzB,MAAMY,OAAO,CAACc,mBAAmB,GAAGH,QAC7BC;AACR;AAhDAG,QAAQC,GAAG,CAAC,8BAER1B,mBAEH2B,KAAKC,gBAAgB,CAAC,YAAY,CAACC;IAClCJ,QAAQC,GAAG,CAAC,6BAEZG,MAAMC,SAAS,CAACC,QAAQC,KAAK;AAC9B;AA0CD,IAAIC,gBAA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