Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/lib/components/console/HeadlessConsole.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

<script lang="ts">
import { getPy, initConsole } from "$lib/pyodide";
import { afterUpdate, beforeUpdate, onMount } from "svelte";
import { afterUpdate, beforeUpdate, onDestroy, onMount } from "svelte";
import { toast } from "svelte-sonner";

export let ready = false;
export let status: Status = "complete";
Expand All @@ -23,12 +24,33 @@
export let autoScroll: false | "instant" | "smooth" = "smooth";

let loading = 0;
const controller = new AbortController();
const { signal } = controller;

let getWrapped: (future: PyAwaitable) => Promise<[unknown, string]>;

onDestroy(() => {
controller.abort();
if (ready) {
pyConsole.stdout_callback = undefined;
pyConsole.stderr_callback = undefined;
}
});

onMount(async () => {
const py = await getPy();
await initConsole();
if (signal.aborted)
return;
try {
await initConsole(signal);
}
catch (error) {
if (!signal.aborted)
toast.error("Unable to initialize console", { description: String(error), action: { label: "Reload", onClick: () => location.reload() } });
return;
}
if (signal.aborted)
return;
pyConsole = py.globals.get("console");
complete = py.globals.get("complete");
getWrapped = py.globals.get("get_wrapped");
Expand All @@ -40,6 +62,8 @@
});

export function pushLog(item: Item, behind?: Item) {
if (signal.aborted)
return;
if (!log.length)
return void (log = [item]);

Expand Down
5 changes: 3 additions & 2 deletions src/lib/pyodide/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ async function initPy() {

export const getPy = cacheSingleton(withToast(initPy, { loading: "preparing pyodide runtime", success: `successfully initialized pyodide runtime` }));

export async function initConsole() {
export async function initConsole(signal?: AbortSignal) {
const [py, { default: initConsoleCode }] = await Promise.all([getPy(), import("./console.py?raw")]);
await py.runPythonAsync(initConsoleCode);
if (!signal?.aborted)
await py.runPythonAsync(initConsoleCode);
}
Loading