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
refactor(editor): derive panel dirty sources from the document
esengineb2dd49df6ccb
diff --git a/desktop/src/layout/panelDirty.ts b/desktop/src/layout/panelDirty.tsindex 8a84b1a02..0f84d8f5c 100644--- a/desktop/src/layout/panelDirty.ts+++ b/desktop/src/layout/panelDirty.ts@@ -26,14 +26,31 @@ export interface DirtySource {discard?(): void;}+/** The reactive shape every AssetDocument singleton shares — enough to build a+ * DirtySource. Structural (not the AssetDocument<T> class) so the generic asset+ * type doesn't have to be erased at each call site. */+interface DirtyDoc {+ readonly docId: string;+ subscribe(fn: () => void): () => void;+ readonly dirty: boolean;+}++// Derive subscribe/isDirty/docId from the document itself — only `discard` (the+// subclass `close()`) is per-entry. In particular docId is READ from the document+// rather than re-typed here, so a panel's Ctrl+S can never route to the wrong+// document id by a copy-paste drift.+function docSource(doc: DirtyDoc, discard: () => void): DirtySource {+ return { docId: doc.docId, subscribe: doc.subscribe, isDirty: () => doc.dirty, discard };+}+const SOURCES: Record<string, DirtySource> = {- statemachine: { docId: 'fsm', subscribe: FsmGraphDocument.subscribe, isDirty: () => FsmGraphDocument.dirty, discard: () => FsmGraphDocument.close() },- animatorcontroller: { docId: 'animator', subscribe: AnimatorGraphDocument.subscribe, isDirty: () => AnimatorGraphDocument.dirty, discard: () => AnimatorGraphDocument.close() },- behaviortree: { docId: 'bt', subscribe: BtDocument.subscribe, isDirty: () => BtDocument.dirty, discard: () => BtDocument.close() },- materialgraph: { docId: 'materialgraph', subscribe: MaterialGraphDocument.subscribe, isDirty: () => MaterialGraphDocument.dirty, discard: () => MaterialGraphDocument.close() },- tileset: { docId: 'tileset', subscribe: TilesetDocument.subscribe, isDirty: () => TilesetDocument.dirty, discard: () => TilesetDocument.close() },- flipbook: { docId: 'flipbook', subscribe: AnimClipDocument.subscribe, isDirty: () => AnimClipDocument.dirty, discard: () => AnimClipDocument.close() },- sequencer: { docId: 'timeline', subscribe: TimelineDocument.subscribe, isDirty: () => TimelineDocument.dirty, discard: () => TimelineDocument.close() },+ statemachine: docSource(FsmGraphDocument, () => FsmGraphDocument.close()),+ animatorcontroller: docSource(AnimatorGraphDocument, () => AnimatorGraphDocument.close()),+ behaviortree: docSource(BtDocument, () => BtDocument.close()),+ materialgraph: docSource(MaterialGraphDocument, () => MaterialGraphDocument.close()),+ tileset: docSource(TilesetDocument, () => TilesetDocument.close()),+ flipbook: docSource(AnimClipDocument, () => AnimClipDocument.close()),+ sequencer: docSource(TimelineDocument, () => TimelineDocument.close()),};const NONE: DirtySource = { subscribe: () => () => {}, isDirty: () => false };diff --git a/desktop/tests/panel-dirty.test.ts b/desktop/tests/panel-dirty.test.tsnew file mode 100644index 000000000..04ba9abf5--- /dev/null+++ b/desktop/tests/panel-dirty.test.ts@@ -0,0 +1,38 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { describe, it, expect } from 'vitest';+import { panelDirtySource } from '../src/layout/panelDirty';+import { FsmGraphDocument } from '../src/fsm/FsmGraphDocument';+import { AnimatorGraphDocument } from '../src/animator/AnimatorGraphDocument';+import { BtDocument } from '../src/bt/BtDocument';+import { MaterialGraphDocument } from '../src/material/MaterialGraphDocument';+import { TilesetDocument } from '../src/tileset/TilesetDocument';+import { AnimClipDocument } from '../src/flipbook/AnimClipDocument';+import { TimelineDocument } from '../src/timeline/TimelineDocument';++describe('panelDirtySource', () => {+ it('routes each panel id to its document (docId read from the document, not re-typed)', () => {+ // The mapping panel-id → document is the one thing the table still hand-wires;+ // this catches a doc plugged into the wrong panel id. docId itself is derived,+ // so it necessarily equals the document's own id.+ expect(panelDirtySource('statemachine').docId).toBe(FsmGraphDocument.docId);+ expect(panelDirtySource('animatorcontroller').docId).toBe(AnimatorGraphDocument.docId);+ expect(panelDirtySource('behaviortree').docId).toBe(BtDocument.docId);+ expect(panelDirtySource('materialgraph').docId).toBe(MaterialGraphDocument.docId);+ expect(panelDirtySource('tileset').docId).toBe(TilesetDocument.docId);+ expect(panelDirtySource('flipbook').docId).toBe(AnimClipDocument.docId);+ expect(panelDirtySource('sequencer').docId).toBe(TimelineDocument.docId);+ });++ it('reflects the live dirty flag of the wired document', () => {+ const src = panelDirtySource('sequencer');+ expect(src.isDirty()).toBe(TimelineDocument.dirty);+ });++ it('gives an inert source for a panel with no document', () => {+ const src = panelDirtySource('viewport');+ expect(src.isDirty()).toBe(false);+ expect(src.docId).toBeUndefined();+ expect(src.discard).toBeUndefined();+ });+});