Skip to content

Commit 68709cb

Browse files
bajrangCoderclaude
andcommitted
fix: keep SDL fetch routing after a plugin remount
dispose() removed the fetch route but kept the cached SDL runtime, and the route was only installed on first load, so a re-initialised plugin could reuse the runtime and download packages through a replaced fetch. - install the route at the start of every run (no-op while active) - dispose() drops the cached runtime so it is freed, not reused - main.js clears its cached sdl.js promise and removes the script tag on destroy, so a later init loads a fresh module Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4ca73ab commit 68709cb

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎plugin.zip‎

103 Bytes
Binary file not shown.

‎src/main.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Python {
3333
/** number of code runs still waiting for the worker */
3434
#running = 0;
3535
#sdlPromise = null;
36+
$sdlScript = null;
3637
/** bumped by every run and when the console closes, to drop stale runs */
3738
#runId = 0;
3839
#sdlAllowed = false;
@@ -244,6 +245,7 @@ class Python {
244245
#loadSdl() {
245246
this.#sdlPromise ??= new Promise((resolve, reject) => {
246247
const $script = tag("script", { src: `${this.baseUrl}sdl.js` });
248+
this.$sdlScript = $script;
247249
$script.onload = () => {
248250
if (window.acodePythonSdl) resolve(window.acodePythonSdl);
249251
else reject(new Error("sdl.js did not initialize"));
@@ -302,6 +304,9 @@ class Python {
302304
this.$wrapBtn?.remove();
303305
this.$status?.remove();
304306
window.acodePythonSdl?.dispose?.();
307+
// a later init loads a fresh sdl.js instead of the disposed one
308+
this.#sdlPromise = null;
309+
this.$sdlScript?.remove();
305310
this.#worker?.terminate();
306311
editorManager.off("switch-file", this.checkRunnable.bind(this));
307312
editorManager.off("rename-file", this.checkRunnable.bind(this));

‎src/sdl.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ function unrouteRuntimeFetch() {
112112

113113
function getRuntime(baseUrl) {
114114
runtimePromise ??= (async () => {
115-
routeRuntimeFetch(baseUrl);
116115
const pyodide = await loadRuntime({
117116
baseUrl,
118117
stdout: (text) => handlers.stdout(text),
@@ -162,6 +161,9 @@ async function run(
162161
{ baseUrl, canvas, stdout, stderr, showImage, width, pixelRatio },
163162
) {
164163
stop();
164+
// every run, not only the first load: package downloads need the route
165+
// too, and dispose() may have removed it while the runtime was cached
166+
routeRuntimeFetch(baseUrl);
165167
const run = { task: null, stopped: false };
166168
current = run;
167169
Object.assign(handlers, { stdout, stderr, showImage });
@@ -245,6 +247,9 @@ function stop() {
245247
function dispose() {
246248
stop();
247249
unrouteRuntimeFetch();
250+
// let the runtime be freed instead of reused after a remount
251+
runtimePromise = null;
252+
runner = null;
248253
if (self.acodePythonSdl === api) delete self.acodePythonSdl;
249254
}
250255

0 commit comments

Comments
 (0)