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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
perf(editor): freeze panel re-renders during a viewport transform drag
yhh78430873a1b5
diff --git a/desktop/src/engine/SceneStore.ts b/desktop/src/engine/SceneStore.tsindex 9ee1dff27..7a5db3334 100644--- a/desktop/src/engine/SceneStore.ts+++ b/desktop/src/engine/SceneStore.ts@@ -20,6 +20,9 @@ import { SceneModel, SceneModelImpl, type ModelEvent } from './SceneModel';*/export class SceneStoreImpl {private installed = false;+ private suspended = false;+ // A change (and whether any was structural) arrived while suspended.+ private pending: { structural: boolean } | null = null;private readonly store = createStore<{ revision: number; structureRevision: number }>(() => ({revision: 1,structureRevision: 1,@@ -35,12 +38,37 @@ export class SceneStoreImpl {}private bump(structural: boolean) {+ if (this.suspended) {+ // Coalesce: hold the bump (remembering if anything was structural) so a+ // high-frequency gesture doesn't re-render React panels every mutation.+ this.pending = { structural: (this.pending?.structural ?? false) || structural };+ return;+ }this.store.setState((s) => ({revision: s.revision + 1,structureRevision: structural ? s.structureRevision + 1 : s.structureRevision,}));}+ /**+ * Pause reactivity bumps during a high-frequency gesture (a viewport transform+ * drag): React panels (Details, Outliner…) stop re-rendering until {@link resume}.+ * The World stays live — the Reconciler subscribes to the model DIRECTLY, not+ * through this store — and the status-bar readout samples on its own timer, so+ * only the (potentially expensive) inspector re-render is deferred. Idempotent.+ */+ suspend = (): void => { this.suspended = true; };++ /** End the gesture: flush a single coalesced bump if anything changed. Safe to+ * call when not suspended (no-op) so a missed pair can't wedge the panels off. */+ resume = (): void => {+ if (!this.suspended) return;+ this.suspended = false;+ const p = this.pending;+ this.pending = null;+ if (p) this.bump(p.structural);+ };+subscribe = (fn: () => void): (() => void) => this.store.subscribe(fn);getRevision = (): number => this.store.getState().revision;getStructureRevision = (): number => this.store.getState().structureRevision;diff --git a/desktop/src/tools/transformTools.ts b/desktop/src/tools/transformTools.tsindex e8749a357..e7fd0234a 100644--- a/desktop/src/tools/transformTools.ts+++ b/desktop/src/tools/transformTools.ts@@ -16,6 +16,7 @@ import { ViewportController } from '@/engine/ViewportController';import { SceneCommands, type EditorTransaction } from '@/engine/SceneCommands';import { SceneQuery } from '@/engine/SceneQuery';import { SceneModel } from '@/engine/SceneModel';+import { SceneStore } from '@/engine/SceneStore';import { EngineHost } from '@/engine/EngineHost';import { snapTo } from '@/engine/viewportMath';import { useSelection } from '@/store/selectionStore';@@ -233,6 +234,10 @@ function beginDrag(): Drag {const label = kind === 'rotate' ? 'Rotate' : kind === 'scale' ? 'Scale' : 'Move';const downWorld = ViewportController.canvasToWorld(p.clientX, p.clientY) ?? { x: 0, y: 0 };+ // Freeze React panel re-renders (Details/Outliner) for the drag — the model+ // mutates every frame while the viewport stays live via the Reconciler. Resumed+ // on commit (onPointerUp) or abort (cancel), flushing one final bump.+ SceneStore.suspend();return {tx: SceneCommands.transaction(label),kind,@@ -435,6 +440,7 @@ function makeTransformTool(mode: ToolMode): EditorTool {ctx.release(p.pointerId);if (drag) {drag.tx.commit();+ SceneStore.resume();drag = null;useEditorStore.getState().setActiveGizmoAxis(null);// A bare click (no drag) on overlapping entities walks to the next one down.@@ -477,6 +483,7 @@ function makeTransformTool(mode: ToolMode): EditorTool {cancel() {if (drag) {drag.tx.abort();+ SceneStore.resume();drag = null;useEditorStore.getState().setActiveGizmoAxis(null);}diff --git a/desktop/tests/scene-store.test.ts b/desktop/tests/scene-store.test.tsnew file mode 100644index 000000000..4f224dae7--- /dev/null+++ b/desktop/tests/scene-store.test.ts@@ -0,0 +1,62 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { describe, it, expect } from 'vitest';+import { SceneStoreImpl } from '@/engine/SceneStore';++// A stand-in for the SceneModel change bus: captures the store's listener and+// lets a test fire model events (only `.kind` is read, via isStructural).+function fakeModel() {+ let listener: ((ev: { kind: string }) => void) | null = null;+ return {+ subscribe: (fn: (ev: { kind: string }) => void) => { listener = fn; return () => {}; },+ emit: (kind: string) => listener?.({ kind }),+ };+}++describe('SceneStore reactivity coalescing', () => {+ it('bumps the revision once per model change when live', () => {+ const m = fakeModel();+ const s = new SceneStoreImpl(m as never);+ s.install();+ const r0 = s.getRevision();+ m.emit('componentChanged');+ m.emit('componentChanged');+ expect(s.getRevision()).toBe(r0 + 2);+ });++ it('suspend coalesces a burst of changes into a single bump on resume', () => {+ const m = fakeModel();+ const s = new SceneStoreImpl(m as never);+ s.install();+ const r0 = s.getRevision();+ s.suspend();+ for (let i = 0; i < 20; i++) m.emit('componentChanged');+ expect(s.getRevision()).toBe(r0); // frozen for the whole gesture+ s.resume();+ expect(s.getRevision()).toBe(r0 + 1); // exactly one flush+ });++ it('preserves structural-ness across a coalesced gesture', () => {+ const m = fakeModel();+ const s = new SceneStoreImpl(m as never);+ s.install();+ const sr0 = s.getStructureRevision();+ s.suspend();+ m.emit('componentChanged'); // non-structural field edit+ m.emit('entityAdded'); // structural+ s.resume();+ expect(s.getStructureRevision()).toBe(sr0 + 1); // structural survives the merge+ });++ it('resume with nothing pending does not bump, and is safe when not suspended', () => {+ const m = fakeModel();+ const s = new SceneStoreImpl(m as never);+ s.install();+ const r0 = s.getRevision();+ s.resume(); // not suspended → no-op+ expect(s.getRevision()).toBe(r0);+ s.suspend();+ s.resume(); // suspended but nothing changed → no bump+ expect(s.getRevision()).toBe(r0);+ });+});