Skip to content

Commit 52df57a

Browse files
committed
fix: defer loadingVersion clear and destroy pending preview on unmount
Bug 1: loadingVersion was cleared before writeVersion, so the URL effect's re-entrant loadVersion call missed the already-loading shortcut and took the same-version branch instead, which incremented versionSeq and broke the original call's final status clear. Move loadingVersion = null into the post-compileAndRun guard so the shortcut stays active through the URL write. Bug 2: If the playground unmounts while a preview is awaiting ready, the in-flight nextPreview (not yet assigned to preview) was not destroyed, leaking its window listener and MutationObserver until the ready timeout. Track pendingPreview and destroy it in cleanup.
1 parent cf38dcf commit 52df57a

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

‎website/src/pages/playground/Playground.tsrx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,7 @@ export function Playground() @{
12461246
let loadingVersion: string | null = null;
12471247
let failedVersion: { param: string; fromUrl: boolean } | null = null;
12481248
let previewSlot: HTMLElement | null = null;
1249+
let pendingPreview: ReturnType<typeof pg.createPreview> | null = null;
12491250
const loadVersion = async (param: string, fromUrl: boolean) => {
12501251
const parsed = pgVersion.parseVersionParam(param);
12511252
const wanted = parsed.ok ? parsed.version : null;
@@ -1291,14 +1292,15 @@ export function Playground() @{
12911292
slot.style.cssText = 'height:100%;display:none;';
12921293
previewHost.appendChild(slot);
12931294
const nextPreview = pg.createPreview(slot, onRuntimeError, next.runtime);
1295+
pendingPreview = nextPreview;
12941296
const readyError = await nextPreview.ready;
1297+
pendingPreview = null;
12951298
if (disposed || seq !== versionSeq || readyError) {
12961299
nextPreview.destroy();
12971300
slot.remove();
12981301
if (readyError && !disposed && seq === versionSeq) fail(readyError);
12991302
return;
13001303
}
1301-
loadingVersion = null;
13021304
failedVersion = null;
13031305
pg.setPlaygroundCompiler(next.compiler);
13041306
toolchain = {
@@ -1324,7 +1326,10 @@ export function Playground() @{
13241326
// The status stays up until the new sandbox has run the workspace:
13251327
// a release's runtime is still downloading from esm.sh until then.
13261328
await compileAndRun();
1327-
if (!disposed && seq === versionSeq) setVersionLoading(null);
1329+
if (!disposed && seq === versionSeq) {
1330+
loadingVersion = null;
1331+
setVersionLoading(null);
1332+
}
13281333
};
13291334
controllerRef.current.loadVersion = (param, fromUrl) => {
13301335
if (!disposed) void loadVersion(param, fromUrl);
@@ -1389,6 +1394,7 @@ export function Playground() @{
13891394
sourceView?.destroy();
13901395
outputView?.destroy();
13911396
astPreview?.destroy();
1397+
pendingPreview?.destroy();
13921398
preview?.destroy();
13931399
};
13941400
});

0 commit comments

Comments
 (0)