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.

refactor(sdk): per-App timeline control (F3 B2a)

esengine79884ca3e011
diff --git a/sdk/src/timeline/TimelineControl.ts b/sdk/src/timeline/TimelineControl.ts
index c9b9722c3..513cc676e 100644
--- a/sdk/src/timeline/TimelineControl.ts
+++ b/sdk/src/timeline/TimelineControl.ts
@@ -1,65 +1,87 @@
import type { Entity } from '../types';
+import type { ESEngineModule } from '../wasm';
+import { defineResource } from '../resource';
-const handles_ = new Map<Entity, number>();
-let module_: any = null;
+/**
+ * Per-App timeline playback control. Holds this App's entity->handle map and the
+ * (guarded) wasm module; published as the {@link Timeline} resource. Replaces
+ * the former process-global `TimelineControl` object + module/handle globals so
+ * two Apps' timelines never collide on entity id.
+ *
+ * `TimelinePlugin` keeps the module current via {@link setModule} each frame and
+ * records handles via {@link setHandle}; game code drives playback through
+ * `app.getResource(Timeline)`.
+ */
+export class TimelineApi {
+ private readonly handles_ = new Map<Entity, number>();
+ private module_: ESEngineModule | null = null;
-export function setTimelineModule(mod: any): void {
- module_ = mod;
-}
+ /** @internal Plugin keeps the guarded module current each frame. */
+ setModule(mod: ESEngineModule | null): void {
+ this.module_ = mod;
+ }
-export function setTimelineHandle(entity: Entity, handle: number): void {
- handles_.set(entity, handle);
-}
+ /** @internal */
+ setHandle(entity: Entity, handle: number): void {
+ this.handles_.set(entity, handle);
+ }
-export function getTimelineHandle(entity: Entity): number | undefined {
- return handles_.get(entity);
-}
+ getHandle(entity: Entity): number | undefined {
+ return this.handles_.get(entity);
+ }
-export function removeTimelineHandle(entity: Entity): void {
- const handle = handles_.get(entity);
- if (handle && module_) {
- module_._tl_destroy(handle);
+ /** @internal Destroys the C++ timeline (if loaded) and forgets the handle. */
+ removeHandle(entity: Entity): void {
+ const handle = this.handles_.get(entity);
+ if (handle && this.module_) {
+ this.module_._tl_destroy(handle);
+ }
+ this.handles_.delete(entity);
}
- handles_.delete(entity);
-}
-export function clearTimelineHandles(): void {
- if (module_) {
- for (const handle of handles_.values()) {
- module_._tl_destroy(handle);
+ /** @internal */
+ clearHandles(): void {
+ if (this.module_) {
+ for (const handle of this.handles_.values()) {
+ this.module_._tl_destroy(handle);
+ }
}
+ this.handles_.clear();
}
- handles_.clear();
-}
-export const TimelineControl = {
play(entity: Entity): void {
- const handle = handles_.get(entity);
- if (handle && module_) module_._tl_play(handle);
- },
+ const handle = this.handles_.get(entity);
+ if (handle && this.module_) this.module_._tl_play(handle);
+ }
pause(entity: Entity): void {
- const handle = handles_.get(entity);
- if (handle && module_) module_._tl_pause(handle);
- },
+ const handle = this.handles_.get(entity);
+ if (handle && this.module_) this.module_._tl_pause(handle);
+ }
stop(entity: Entity): void {
- const handle = handles_.get(entity);
- if (handle && module_) module_._tl_stop(handle);
- },
+ const handle = this.handles_.get(entity);
+ if (handle && this.module_) this.module_._tl_stop(handle);
+ }
setTime(entity: Entity, time: number): void {
- const handle = handles_.get(entity);
- if (handle && module_) module_._tl_setTime(handle, time);
- },
+ const handle = this.handles_.get(entity);
+ if (handle && this.module_) this.module_._tl_setTime(handle, time);
+ }
isPlaying(entity: Entity): boolean {
- const handle = handles_.get(entity);
- return handle && module_ ? module_._tl_isPlaying(handle) !== 0 : false;
- },
+ const handle = this.handles_.get(entity);
+ return handle && this.module_ ? this.module_._tl_isPlaying(handle) !== 0 : false;
+ }
getCurrentTime(entity: Entity): number {
- const handle = handles_.get(entity);
- return handle && module_ ? module_._tl_getTime(handle) : 0;
- },
-};
+ const handle = this.handles_.get(entity);
+ return handle && this.module_ ? this.module_._tl_getTime(handle) : 0;
+ }
+}
+
+/**
+ * Per-App timeline control resource, published by `TimelinePlugin`. Drive
+ * playback as `app.getResource(Timeline).play(entity)`.
+ */
+export const Timeline = defineResource<TimelineApi>(null!, 'Timeline');
diff --git a/sdk/src/timeline/TimelinePlugin.ts b/sdk/src/timeline/TimelinePlugin.ts
index e7ec664a9..b0397ee20 100644
--- a/sdk/src/timeline/TimelinePlugin.ts
+++ b/sdk/src/timeline/TimelinePlugin.ts
@@ -8,7 +8,7 @@ import { Audio, type AudioAPI } from '../audio/Audio';
import { WrapMode, TrackType, type TimelineAsset, type AnimFramesTrack } from './TimelineTypes';
import { parseTimelineAsset } from './TimelineLoader';
import { uploadTimelineToWasm, type UploadResult } from './TimelineUploader';
-import { setTimelineHandle, setTimelineModule, removeTimelineHandle } from './TimelineControl';
+import { Timeline, TimelineApi } from './TimelineControl';
import {
resolveTrackTargets,
advanceAndProcess,
@@ -92,9 +92,10 @@ export class TimelinePlugin implements Plugin {
build(app: App): void {
activeTimelinePlugin = this;
const world = app.world;
+ app.insertResource(Timeline, new TimelineApi());
world.onDespawn((entity: Entity) => {
- removeTimelineHandle(entity);
+ app.getResource(Timeline).removeHandle(entity);
this.handles_.delete(entity);
this.animFramesStates_.delete(entity);
});
@@ -106,13 +107,14 @@ export class TimelinePlugin implements Plugin {
if (!rawModule) return;
// Route the main module through the bridge once (re-connect only
// if the underlying module changes). Every `_tl_*` call below —
- // and everything handed `module` (TimelineControl via
- // setTimelineModule, the uploader, the runtime) — then inherits
- // the terminal-abort guard.
+ // and everything handed `module` (the Timeline resource via
+ // setModule, the uploader, the runtime) — then inherits the
+ // terminal-abort guard.
if (this.bridge_.raw !== rawModule) this.bridge_.connect(rawModule);
const module = this.bridge_.module;
- setTimelineModule(module);
+ const tl = app.getResource(Timeline);
+ tl.setModule(module);
const registry = world.getCppRegistry() as any;
const audio: AudioAPI | null = app.hasResource(Audio) ? app.getResource(Audio) : null;
@@ -129,7 +131,7 @@ export class TimelinePlugin implements Plugin {
if (!uploadResult.handle) continue;
resolveTrackTargets(world, module, uploadResult, entity);
this.handles_.set(entity, uploadResult);
- setTimelineHandle(entity, uploadResult.handle);
+ tl.setHandle(entity, uploadResult.handle);
const afTracks = asset.tracks.filter(
(t): t is AnimFramesTrack => t.type === TrackType.AnimFrames,
diff --git a/sdk/src/timeline/index.ts b/sdk/src/timeline/index.ts
index 4f19c384b..bdeea00b2 100644
--- a/sdk/src/timeline/index.ts
+++ b/sdk/src/timeline/index.ts
@@ -36,12 +36,8 @@ export {
} from './TimelineUploader';
export {
- TimelineControl,
- setTimelineHandle,
- getTimelineHandle,
- removeTimelineHandle,
- clearTimelineHandles,
- setTimelineModule,
+ Timeline,
+ TimelineApi,
} from './TimelineControl';
export {
diff --git a/sdk/src/webAppFactory.ts b/sdk/src/webAppFactory.ts
index dd2141b48..2500e514e 100644
--- a/sdk/src/webAppFactory.ts
+++ b/sdk/src/webAppFactory.ts
@@ -32,7 +32,7 @@ export { AnimationPlugin, animationPlugin } from './animation';
export { AudioPlugin, audioPlugin } from './audio';
export { ParticlePlugin, particlePlugin } from './particle';
export { PostProcessPlugin, postProcessPlugin } from './postprocess';
-export { TimelinePlugin, timelinePlugin, registerTimelineAsset, parseTimelineAsset, clearTimelineHandles, getTimelineHandle, TimelinePlayer, type TimelinePlayerData, TimelineControl } from './timeline';
+export { TimelinePlugin, timelinePlugin, registerTimelineAsset, parseTimelineAsset, Timeline, TimelineApi, TimelinePlayer, type TimelinePlayerData } from './timeline';
export interface CreateWebAppOptions extends WebAppOptions {
spineProvider?: SpineWasmProvider;
diff --git a/sdk/tests/timeline-tilemap-ui-bridge.test.ts b/sdk/tests/timeline-tilemap-ui-bridge.test.ts
index 77cebebf1..826c5b0b9 100644
--- a/sdk/tests/timeline-tilemap-ui-bridge.test.ts
+++ b/sdk/tests/timeline-tilemap-ui-bridge.test.ts
@@ -10,9 +10,7 @@ import { WasmModuleAborted } from '../src/moduleHealth';
import { CoreApiBridge } from '../src/CoreApiBridge';
import { TilemapAPI, initTilemapAPI, shutdownTilemapAPI } from '../src/tilemap/tilemapAPI';
import { initUIHelpers, setUIRectSizeNative } from '../src/ui/uiHelpers';
-import {
- TimelineControl, setTimelineModule, setTimelineHandle,
-} from '../src/timeline/TimelineControl';
+import { TimelineApi } from '../src/timeline/TimelineControl';
import type { ESEngineModule, CppRegistry } from '../src/wasm';
import type { Entity } from '../src/types';
@@ -108,14 +106,15 @@ describe('timeline bridge (control path)', () => {
const raw = makeModule();
const bridge = new CoreApiBridge('timeline');
bridge.connect(raw);
- setTimelineModule(bridge.module); // exactly what TimelinePlugin does
- setTimelineHandle(7 as unknown as Entity, 100);
+ const tl = new TimelineApi();
+ tl.setModule(bridge.module); // exactly what TimelinePlugin does
+ tl.setHandle(7 as unknown as Entity, 100);
- TimelineControl.play(7 as unknown as Entity);
+ tl.play(7 as unknown as Entity);
expect((raw as unknown as { _tl_play: ReturnType<typeof vi.fn> })._tl_play)
.toHaveBeenCalledWith(100);
abort(raw);
- expect(() => TimelineControl.play(7 as unknown as Entity)).toThrow(WasmModuleAborted);
+ expect(() => tl.play(7 as unknown as Entity)).toThrow(WasmModuleAborted);
});
});