()
| 7 | |
| 8 | export class EdgePythonElement extends HTMLElement { |
| 9 | async connectedCallback() { |
| 10 | const file = this.getAttribute('entry'); |
| 11 | const pkg = this.getAttribute('packages'); |
| 12 | |
| 13 | // host -> main-thread modules (lazy: name -> url, imported on first use), imports -> worker .py/.wasm modules |
| 14 | const hostModules = {}; |
| 15 | let imports; |
| 16 | if (pkg) { |
| 17 | const base = new URL(pkg, location.href); |
| 18 | const manifest = await fetch(base).then(r => r.json()); |
| 19 | for (const [name, url] of Object.entries(manifest.host ?? {})) { |
| 20 | hostModules[name] = new URL(url, base).href; |
| 21 | } |
| 22 | if (manifest.imports) { |
| 23 | imports = {}; |
| 24 | for (const [name, url] of Object.entries(manifest.imports)) imports[name] = new URL(url, base).href; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // Kept on the element so callers can drive the same worker after the declarative run. |
| 29 | this.worker = await createWorker({ |
| 30 | wasmUrl: "https://cdn.edgepython.com/compiler.wasm", |
| 31 | hostModules, |
| 32 | imports, |
| 33 | }); |
| 34 | // `entry` is optional: omit it to just spin up the worker and drive it via run(). |
| 35 | if (file) await this.worker.run(await fetch(file).then(r => r.text())); |
| 36 | this.dispatchEvent(new Event("ready")); |
| 37 | } |
| 38 | |
| 39 | // Run a Python source string on the element's worker. Resolves with { out, ms }. |
| 40 | run(src, opts) { return this.worker.run(src, opts); } |
nothing calls this directly
no test coverage detected