speed up by loading pyodide on page load

This commit is contained in:
Xuan Son Nguyen 2025-02-08 19:39:26 +01:00
parent 84fe6c4e93
commit 475b2906ba
3 changed files with 15 additions and 6 deletions

Binary file not shown.

View file

@ -3,6 +3,7 @@ import { useAppContext } from '../utils/app.context';
import { OpenInNewTab, XCloseButton } from '../utils/common'; import { OpenInNewTab, XCloseButton } from '../utils/common';
import { CanvasType } from '../utils/types'; import { CanvasType } from '../utils/types';
import { PlayIcon, StopIcon } from '@heroicons/react/24/outline'; import { PlayIcon, StopIcon } from '@heroicons/react/24/outline';
import StorageUtils from '../utils/storage';
const canInterrupt = typeof SharedArrayBuffer === 'function'; const canInterrupt = typeof SharedArrayBuffer === 'function';
@ -54,6 +55,18 @@ const interruptBuffer = canInterrupt
? new Uint8Array(new SharedArrayBuffer(1)) ? new Uint8Array(new SharedArrayBuffer(1))
: null; : null;
const startWorker = () => {
if (!worker) {
worker = new Worker(
URL.createObjectURL(new Blob([WORKER_CODE], { type: 'text/javascript' }))
);
}
};
if (StorageUtils.getConfig().pyIntepreterEnabled) {
startWorker();
}
const runCodeInWorker = ( const runCodeInWorker = (
pyCode: string, pyCode: string,
callbackRunning: () => void callbackRunning: () => void
@ -61,11 +74,7 @@ const runCodeInWorker = (
donePromise: Promise<string>; donePromise: Promise<string>;
interrupt: () => void; interrupt: () => void;
} => { } => {
if (!worker) { startWorker();
worker = new Worker(
URL.createObjectURL(new Blob([WORKER_CODE], { type: 'text/javascript' }))
);
}
const id = Math.random() * 1e8; const id = Math.random() * 1e8;
const context = {}; const context = {};
if (interruptBuffer) { if (interruptBuffer) {

View file

@ -137,7 +137,7 @@ export default function ChatScreen() {
</div> </div>
</div> </div>
{canvasData && ( {canvasData && (
<div className="w-full sticky top-[8em] h-[calc(100vh-9em)]"> <div className="w-full sticky top-[7em] h-[calc(100vh-9em)]">
<CanvasPyInterpreter /> <CanvasPyInterpreter />
</div> </div>
)} )}