function postReply(id, type, payload) { self.postMessage({ id, type, ok: true, ...payload }); } function postError(id, type, error) { self.postMessage({ id, type, ok: false, error: error?.message ?? String(error), }); } function pythonRuntimeAvailable() { return typeof self.loadPyodide === "function" || Boolean(self.pyodide?.runPythonAsync); } self.addEventListener("message", async (event) => { const message = event.data ?? {}; const { id, type } = message; try { if (type === "start") { const ready = pythonRuntimeAvailable(); postReply(id, type, { runtimeMode: "browser-python-wasm-worker", runtimeExecutionReady: ready, status: ready ? "browser_python_wasm_runtime_ready" : "blocked_browser_python_wasm_runtime_missing", executionEnabled: false, promotionAllowed: false, }); return; } throw new Error("blocked_browser_python_wasm_runtime_missing"); } catch (error) { postError(id, type, error); } });