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: atomic state swap for scene operations and WASM error throttling

esenginedbbf16a93729
diff --git a/editor/src/renderer/SharedRenderContext.ts b/editor/src/renderer/SharedRenderContext.ts
index 182d581f3..25218f03e 100644
--- a/editor/src/renderer/SharedRenderContext.ts
+++ b/editor/src/renderer/SharedRenderContext.ts
@@ -173,6 +173,10 @@ export class SharedRenderContext {
...uiPlugins,
]);
+ app.onWasmError((error, context) => {
+ console.error(`[ESEngine:${context}]`, error);
+ });
+
this.assetGraph_ = new AssetGraph();
this.assetGraph_.registerProcessor(imageProcessor);
this.assetGraph_.onChange((events) => {
diff --git a/editor/src/store/EditorStore.ts b/editor/src/store/EditorStore.ts
index 0660cad2e..c6d44d32d 100644
--- a/editor/src/store/EditorStore.ts
+++ b/editor/src/store/EditorStore.ts
@@ -274,32 +274,13 @@ export class EditorStore {
newScene(name: string = 'Untitled', designResolution?: { width: number; height: number }): void {
this.saveRecoveryBackup();
- this.state_.scene = createEmptyScene(name, designResolution);
- this.state_.selectedEntities.clear();
- this.state_.selectedAsset = null;
- this.state_.filePath = null;
- this.history_.clear();
- this.forceDirty_ = false;
- this.nextEntityId_ = this.computeNextEntityId(this.state_.scene);
- this.rebuildEntityMap();
- this.worldTransforms_.setScene(this.state_.scene);
- this.notifySceneSync();
- this.notify('scene');
+ const scene = createEmptyScene(name, designResolution);
+ this.swapScene_(scene, null);
}
loadScene(scene: SceneData, filePath: string | null = null): void {
this.saveRecoveryBackup();
- this.state_.scene = scene;
- this.state_.selectedEntities.clear();
- this.state_.selectedAsset = null;
- this.state_.filePath = filePath;
- this.history_.clear();
- this.forceDirty_ = false;
-
- this.nextEntityId_ = this.computeNextEntityId(scene);
-
- this.rebuildEntityMap();
- this.worldTransforms_.setScene(scene);
+ this.swapScene_(scene, filePath);
const tracker = getPrefabDependencyTracker();
tracker.setScene(scene, () => {
@@ -307,7 +288,21 @@ export class EditorStore {
this.state_.isDirty = true;
this.notify('scene');
});
+ }
+ private swapScene_(scene: SceneData, filePath: string | null): void {
+ this.state_ = {
+ scene,
+ selectedEntities: new Set(),
+ selectedAsset: null,
+ isDirty: false,
+ filePath,
+ };
+ this.history_.clear();
+ this.forceDirty_ = false;
+ this.nextEntityId_ = this.computeNextEntityId(scene);
+ this.rebuildEntityMap();
+ this.worldTransforms_.setScene(scene);
this.notifySceneSync();
this.notify('scene');
}
@@ -322,10 +317,14 @@ export class EditorStore {
}
restoreSnapshot(snapshot: SceneSnapshot): void {
- this.state_.scene = snapshot.scene;
- this.state_.selectedEntities = new Set(snapshot.selectedEntities);
+ this.state_ = {
+ scene: snapshot.scene,
+ selectedEntities: new Set(snapshot.selectedEntities),
+ selectedAsset: null,
+ isDirty: snapshot.isDirty,
+ filePath: snapshot.filePath,
+ };
this.forceDirty_ = snapshot.isDirty;
- this.state_.filePath = snapshot.filePath;
this.nextEntityId_ = this.computeNextEntityId(this.state_.scene);
this.rebuildEntityMap();
this.worldTransforms_.setScene(this.state_.scene);
diff --git a/sdk/src/wasmError.ts b/sdk/src/wasmError.ts
index bc6059cd1..29cce4653 100644
--- a/sdk/src/wasmError.ts
+++ b/sdk/src/wasmError.ts
@@ -1,12 +1,28 @@
type WasmErrorHandler = (error: unknown, context: string) => void;
let errorHandler: WasmErrorHandler | null = null;
+let lastReportTime = 0;
+let suppressedCount = 0;
+
+const THROTTLE_MS = 1000;
export function setWasmErrorHandler(handler: WasmErrorHandler | null): void {
errorHandler = handler;
}
export function handleWasmError(error: unknown, context: string): void {
+ const now = Date.now();
+ if (now - lastReportTime < THROTTLE_MS) {
+ suppressedCount++;
+ return;
+ }
+
+ if (suppressedCount > 0) {
+ console.warn(`[ESEngine] ${suppressedCount} WASM error(s) suppressed`);
+ suppressedCount = 0;
+ }
+
+ lastReportTime = now;
console.error(`[ESEngine] WASM error in ${context}:`, error);
if (errorHandler) {
errorHandler(error, context);