Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(editor): reset editor state on project/scene switch

esenginea9160721e971
diff --git a/desktop/src/engine/PlayRealm.ts b/desktop/src/engine/PlayRealm.ts
index 319742e2e..d4aed3997 100644
--- a/desktop/src/engine/PlayRealm.ts
+++ b/desktop/src/engine/PlayRealm.ts
@@ -211,7 +211,10 @@ export class PlayRealmInstance {
destroy(): void {
this.warm = false; // force stop() down the cold teardown path (release wasm + GL)
this.stop();
- window.removeEventListener('message', this.onMessage);
+ // Unbind from the window the listener was actually added to (msgWin — the
+ // popout window when the viewport is popped out), not the global window.
+ try { this.msgWin?.removeEventListener('message', this.onMessage); } catch { /* window may be closed */ }
+ this.msgWin = null;
this.detach();
this.iframe = null;
}
diff --git a/desktop/src/panels/ContentBrowser.tsx b/desktop/src/panels/ContentBrowser.tsx
index 52b05318e..e7742a8c3 100644
--- a/desktop/src/panels/ContentBrowser.tsx
+++ b/desktop/src/panels/ContentBrowser.tsx
@@ -329,10 +329,13 @@ export function ContentBrowser() {
});
useEffect(() => localStorage.setItem(TILE_KEY, String(tileSize)), [tileSize]);
- // Reset navigation + selection when the open project changes.
+ // Reset navigation + selection + search/filter when the open project changes —
+ // project A's query/type-filter must not carry into project B's browser.
useEffect(() => {
reset();
selectAsset(null);
+ setQuery('');
+ setFilters(new Set());
}, [project?.name, reset, selectAsset]);
// Selection doesn't survive a folder change.
useEffect(() => selectAsset(null), [cwd, selectAsset]);
diff --git a/desktop/src/panels/Outliner.tsx b/desktop/src/panels/Outliner.tsx
index e0eae1c16..068486bb1 100644
--- a/desktop/src/panels/Outliner.tsx
+++ b/desktop/src/panels/Outliner.tsx
@@ -147,6 +147,13 @@ export function Outliner() {
[],
);
+ // A scene swap resets the model ('reset' event). This panel is a persistent dock
+ // panel (it doesn't remount), so re-arm the one-time auto-expand/select below —
+ // otherwise the NEW scene renders fully collapsed with nothing selected.
+ useEffect(() => SceneModel.subscribe((ev) => {
+ if (ev.kind === 'reset') initRef.current = false;
+ }), []);
+
// First time entities appear: expand groups + folders, select the first entity.
useEffect(() => {
if (initRef.current || sceneCount === 0) return;
diff --git a/desktop/src/project/ProjectStore.ts b/desktop/src/project/ProjectStore.ts
index d7c58a72f..f04fd9ade 100644
--- a/desktop/src/project/ProjectStore.ts
+++ b/desktop/src/project/ProjectStore.ts
@@ -29,6 +29,9 @@ import type { AssetType } from '@/types';
import { resolveLayout, orientationFromDesignResolution, resolveOrientation, cameraScaleModeValue, WORKSPACE_DIR, PROJECT_MANIFEST_FILE, type OpenedProject, type ProjectFeatures, type ProjectLayout, type ProjectPackaging, type WorkspaceState, type DesignResolution, type ScreenOrientation, type CameraScaleMode } from './format';
import { useEditorMode } from '@/store/editorModeStore';
import { PlayRealms } from '@/engine/PlayRealm';
+import { PlayInspect } from '@/engine/PlayInspect';
+import { useEditorStore } from '@/store/editorStore';
+import { resetFsWatch } from './fsWatch';
import type { DocSnapshot } from '@/document/DirtyRegistry';
/** Pad/truncate collision-layer names to the 16 Box2D filter bits (layer 0 = Default). */
@@ -204,6 +207,10 @@ class ProjectStoreImpl {
private readonly assetLoadFailures = new Map<string, string>();
/** Guards the off-critical-path asset revalidation after a cache-first boot. */
private revalidating = false;
+ /** Bumped on every project open ({@link adopt}); a revalidation captures it and
+ * bails after its await if it changed, so a slow scan for project A can never
+ * clobber project B's registry when the user switches within the scan window. */
+ private projectGeneration = 0;
/** The latest scene preload result; the Reconciler resolver reads handles from
* it for entities recreated incrementally (duplicate / undo / play-stop). */
private lastAssetResult: PreloadResult | null = null;
@@ -348,6 +355,23 @@ class ProjectStoreImpl {
}
private adopt(opened: OpenedProject) {
+ // If a play session is live, tear it down before swapping projects. The App
+ // play-effect keys off `isPlaying`, which opening a project never changes, so
+ // without this the editor stays "Running" on a realm we're about to reset and
+ // PlayInspect keeps polling the destroyed realm. Stop the poll BEFORE the
+ // reset; flipping isPlaying lets the effect finish the teardown (dock tabs).
+ const es = useEditorStore.getState();
+ if (es.isPlaying || es.isPaused) {
+ PlayInspect.stop();
+ es.setInspectWorld('editor');
+ es.stop();
+ }
+ // A new project supersedes any in-flight revalidation of the old one (its
+ // late populateRegistry must not clobber this project's registry).
+ this.projectGeneration++;
+ this.revalidating = false;
+ // Drop any file-watch burst still queued for the previous project.
+ resetFsWatch();
// A play realm warmed for the PREVIOUS project holds its bundle + assets;
// cold-reset so the next prewarm/Play stages this project.
PlayRealms.resetPrimary();
@@ -652,10 +676,12 @@ class ProjectStoreImpl {
* common case is zero changes → a pure no-op). */
private async revalidateAssets(): Promise<void> {
if (this.revalidating) return;
+ const gen = this.projectGeneration;
this.revalidating = true;
try {
const entries = await this.scanAssetsReporting();
if (!entries) return;
+ if (gen !== this.projectGeneration) return; // switched projects mid-scan — don't clobber the new one
const fresh = new Map<string, string>();
for (const e of entries) fresh.set(e.path, e.uuid.toLowerCase());
const changed: string[] = [];
@@ -666,7 +692,8 @@ class ProjectStoreImpl {
this.populateRegistry(entries);
this.hotSyncChangedPaths(changed);
} finally {
- this.revalidating = false;
+ // A superseded scan must not clear the flag the new project's scan now owns.
+ if (gen === this.projectGeneration) this.revalidating = false;
}
}
diff --git a/desktop/src/project/fsWatch.ts b/desktop/src/project/fsWatch.ts
index 0beefeec1..8237772bc 100644
--- a/desktop/src/project/fsWatch.ts
+++ b/desktop/src/project/fsWatch.ts
@@ -108,3 +108,14 @@ export function initFsWatch(): void {
}
});
}
+
+/** Drop pending debounced work + accumulated paths. Call on project switch so a
+ * burst captured for the old project can't flush against the new one. */
+export function resetFsWatch(): void {
+ for (const t of [debounce, schemaDebounce, scriptsDebounce, sceneDebounce]) {
+ if (t) clearTimeout(t);
+ }
+ debounce = schemaDebounce = scriptsDebounce = sceneDebounce = null;
+ pendingPaths = new Set();
+ sawOverflow = false;
+}