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(sdk): scope wasm-error state per-context, deterministic component ids
esengine360c1e1d718f
diff --git a/sdk/src/component.ts b/sdk/src/component.tsindex 0ff0bea3e..23a0bf8d8 100644--- a/sdk/src/component.ts+++ b/sdk/src/component.ts@@ -59,8 +59,6 @@ function deepClone<T>(value: T): T {return result as T;}-let componentCounter = 0;-function classifyKeys(obj: object): { flatKeys: string[]; objectKeys: string[]; arrayKeys: string[] } | null {const flatKeys: string[] = [];const objectKeys: string[] = [];@@ -87,11 +85,10 @@ function createComponentDef<T extends object>(defaults: T,metadata?: ComponentMetadata,): ComponentDef<T> {- const id = ++componentCounter;const keyInfo = classifyKeys(defaults);const defaultsRec = defaults as Record<string, unknown>;return {- _id: Symbol(`Component_${id}_${name}`),+ _id: Symbol(`Component_${name}`),_name: name,_default: defaults,_builtin: false as const,diff --git a/sdk/src/context.ts b/sdk/src/context.tsindex f16aa142a..ad2b32105 100644--- a/sdk/src/context.ts+++ b/sdk/src/context.ts@@ -12,10 +12,19 @@ export interface PendingSystemEntry {system: unknown;}+export type WasmErrorHandler = (error: unknown, context: string) => void;++export interface WasmErrorState {+ handler: WasmErrorHandler | null;+ lastReportTime: number;+ suppressedCount: number;+}+export class AppContext {readonly componentRegistry = new Map<string, any>();readonly pendingSystems: PendingSystemEntry[] = [];editorBridge: EditorBridge | null = null;+ readonly wasmError: WasmErrorState = { handler: null, lastReportTime: 0, suppressedCount: 0 };/** @brief Drain all pending systems and clear the queue */drainPendingSystems(): PendingSystemEntry[] {@@ -28,9 +37,22 @@ export class AppContext {this.pendingSystems.length = 0;this.componentRegistry.clear();this.editorBridge = null;+ this.wasmError.handler = null;+ this.wasmError.lastReportTime = 0;+ this.wasmError.suppressedCount = 0;}}+/**+ * Module-level default context.+ *+ * Multi-app note: this default is shared across all consumers that call+ * `getDefaultContext()` without first installing their own context. If you+ * run multiple isolated Apps in one process, each must call+ * `setDefaultContext(new AppContext())` on its own entry path — the lazy+ * singleton here will otherwise leak state (component registry, pending+ * systems, wasm-error throttle) between them.+ */let defaultContext_: AppContext | null = null;export function getDefaultContext(): AppContext {diff --git a/sdk/src/wasmError.ts b/sdk/src/wasmError.tsindex f026a60a4..540b5db19 100644--- a/sdk/src/wasmError.ts+++ b/sdk/src/wasmError.ts@@ -1,32 +1,28 @@import { log } from './logger';--type WasmErrorHandler = (error: unknown, context: string) => void;--let errorHandler: WasmErrorHandler | null = null;-let lastReportTime = 0;-let suppressedCount = 0;+import { getDefaultContext, type WasmErrorHandler } from './context';const THROTTLE_MS = 1000;export function setWasmErrorHandler(handler: WasmErrorHandler | null): void {- errorHandler = handler;+ getDefaultContext().wasmError.handler = handler;}export function handleWasmError(error: unknown, context: string): void {+ const state = getDefaultContext().wasmError;const now = Date.now();- if (now - lastReportTime < THROTTLE_MS) {- suppressedCount++;+ if (now - state.lastReportTime < THROTTLE_MS) {+ state.suppressedCount++;return;}- if (suppressedCount > 0) {- log.warn('wasm', `${suppressedCount} WASM error(s) suppressed`);- suppressedCount = 0;+ if (state.suppressedCount > 0) {+ log.warn('wasm', `${state.suppressedCount} WASM error(s) suppressed`);+ state.suppressedCount = 0;}- lastReportTime = now;+ state.lastReportTime = now;log.error('wasm', `error in ${context}`, error);- if (errorHandler) {- errorHandler(error, context);+ if (state.handler) {+ state.handler(error, context);}}