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(sdk): plugins retire their onDespawn subscription in cleanup

esengineb85707869805
diff --git a/sdk/src/animation/AnimationPlugin.ts b/sdk/src/animation/AnimationPlugin.ts
index c517ae6ae..9ff90e062 100644
--- a/sdk/src/animation/AnimationPlugin.ts
+++ b/sdk/src/animation/AnimationPlugin.ts
@@ -19,6 +19,7 @@ import { SystemLabel } from '../systemLabels';
export class AnimationPlugin implements Plugin {
name = 'animation';
+ private offDespawn_: (() => void) | null = null;
build(app: App): void {
const module = app.wasmModule as ESEngineModule;
@@ -31,7 +32,7 @@ export class AnimationPlugin implements Plugin {
app.insertResource(AnimatorController, animator);
const world = app.world;
- world.onDespawn((entity: Entity) => {
+ this.offDespawn_ = world.onDespawn((entity: Entity) => {
tween.cancelAll(entity);
anim.removeEntityListeners(entity);
animator.removeEntity(entity);
@@ -63,6 +64,13 @@ export class AnimationPlugin implements Plugin {
{ name: 'SpriteAnimatorSystem' }
), { runAfter: [SystemLabel.Tween, SystemLabel.Animator], runIf: playModeOnly });
}
+
+ cleanup(): void {
+ // Drop the despawn subscription so a warm re-Play with a reused world
+ // doesn't stack a second (stale) closure.
+ this.offDespawn_?.();
+ this.offDespawn_ = null;
+ }
}
export const animationPlugin = new AnimationPlugin();
diff --git a/sdk/src/audio/AudioPlugin.ts b/sdk/src/audio/AudioPlugin.ts
index bbd1dedb2..b1497ee35 100644
--- a/sdk/src/audio/AudioPlugin.ts
+++ b/sdk/src/audio/AudioPlugin.ts
@@ -27,6 +27,7 @@ export class AudioPlugin implements Plugin {
private playedEntities_: Set<number> | null = null;
private audio_: AudioAPI | null = null;
private offMemoryWarning_: (() => void) | null = null;
+ private offDespawn_: (() => void) | null = null;
constructor(config: AudioPluginConfig = {}) {
this.config_ = config;
@@ -63,7 +64,7 @@ export class AudioPlugin implements Plugin {
const playedEntities = new Set<number>();
this.playedEntities_ = playedEntities;
- app.world.onDespawn((entity: Entity) => {
+ this.offDespawn_ = app.world.onDespawn((entity: Entity) => {
const handle = activeSourceHandles.get(entity);
if (handle) {
handle.stop();
@@ -200,6 +201,8 @@ export class AudioPlugin implements Plugin {
cleanup(): void {
this.offMemoryWarning_?.();
this.offMemoryWarning_ = null;
+ this.offDespawn_?.();
+ this.offDespawn_ = null;
this.stopAllSources();
this.audio_?.dispose();
this.audio_ = null;
diff --git a/sdk/src/timeline/TimelinePlugin.ts b/sdk/src/timeline/TimelinePlugin.ts
index 53d808c4d..9e08c034e 100644
--- a/sdk/src/timeline/TimelinePlugin.ts
+++ b/sdk/src/timeline/TimelinePlugin.ts
@@ -59,6 +59,7 @@ export class TimelinePlugin implements Plugin, TimelineAssetRegistry {
private loadedAssets_ = new Map<string, TimelineAsset>();
private textureHandles_ = new Map<string, Map<string, number>>();
private animFramesStates_ = new Map<number, AnimFramesState>();
+ private offDespawn_: (() => void) | null = null;
registerAsset(path: string, asset: TimelineAsset): void {
this.loadedAssets_.set(path, asset);
@@ -96,7 +97,7 @@ export class TimelinePlugin implements Plugin, TimelineAssetRegistry {
reset: entity => setFlags(entity, false, true),
}));
- world.onDespawn((entity: Entity) => {
+ this.offDespawn_ = world.onDespawn((entity: Entity) => {
app.getResource(Timeline).removeState(entity);
this.animFramesStates_.delete(entity);
});
@@ -151,6 +152,8 @@ export class TimelinePlugin implements Plugin, TimelineAssetRegistry {
}
cleanup(): void {
+ this.offDespawn_?.();
+ this.offDespawn_ = null;
this.animFramesStates_.clear();
this.loadedAssets_.clear();
this.textureHandles_.clear();
diff --git a/sdk/src/trail/TrailPlugin.ts b/sdk/src/trail/TrailPlugin.ts
index 5917f1801..4c642545a 100644
--- a/sdk/src/trail/TrailPlugin.ts
+++ b/sdk/src/trail/TrailPlugin.ts
@@ -11,6 +11,7 @@ import { Trail, TrailAPI } from './TrailAPI';
export class TrailPlugin implements Plugin {
name = 'trail';
+ private offDespawn_: (() => void) | null = null;
build(app: App): void {
const module = app.wasmModule as ESEngineModule;
@@ -20,7 +21,7 @@ export class TrailPlugin implements Plugin {
// Trail history lives in a C++ side table keyed by entity, not in the
// component — drop it on despawn or the recorded points leak.
- app.world.onDespawn((entity) => {
+ this.offDespawn_ = app.world.onDespawn((entity) => {
if (app.world.has(entity, TrailRenderer)) api.clear(entity);
});
@@ -36,6 +37,11 @@ export class TrailPlugin implements Plugin {
{ name: 'TrailSystem' }
), { runIf: fxPreviewOrPlayMode });
}
+
+ cleanup(): void {
+ this.offDespawn_?.();
+ this.offDespawn_ = null;
+ }
}
export const trailPlugin = new TrailPlugin();