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.

feat(scene): SceneManager.getLoading and whenLoadsSettle

yhhc7cf6e5c0908
diff --git a/desktop b/desktop
index e7261be1b..71761c17e 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit e7261be1bb7f829e878d44a9fe7c5baacab945c0
+Subproject commit 71761c17e50cd2588761a72c63459a0edc9e03a4
diff --git a/sdk/src/scene/sceneManager.ts b/sdk/src/scene/sceneManager.ts
index cc20739fb..61a66a13c 100644
--- a/sdk/src/scene/sceneManager.ts
+++ b/sdk/src/scene/sceneManager.ts
@@ -975,6 +975,21 @@ export class SceneManagerState {
return Array.from(this.scenes_.keys());
}
+ /** Scenes whose load has started and not yet finished, in the order they began. */
+ getLoading(): string[] {
+ return Array.from(this.loadPromises_.keys());
+ }
+
+ /**
+ * Resolves once every load in flight NOW has finished, failed or been
+ * cancelled — loads read their files in wall time, so a caller that advances
+ * the game by frames (a test, an automation driver) cannot count its way to
+ * a loaded scene. A load started after this call is not waited for.
+ */
+ async whenLoadsSettle(): Promise<void> {
+ await Promise.allSettled([...this.loadPromises_.values()]);
+ }
+
getLoadOrder(): string[] {
return [...this.loadOrder_];
}
diff --git a/sdk/tests/scene-loads-settle.test.ts b/sdk/tests/scene-loads-settle.test.ts
new file mode 100644
index 000000000..3b761df83
--- /dev/null
+++ b/sdk/tests/scene-loads-settle.test.ts
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/** What a frame-stepping driver needs: which scenes are loading, and a way to wait for them. */
+import { describe, it, expect } from 'vitest';
+import { App } from '../src/app/app';
+import { SceneManager } from '../src/scene/sceneManager';
+import { sceneManagerPlugin } from '../src/scene/scenePlugin';
+
+describe('SceneManager load tracking', () => {
+ it('names a scene while it loads, and settles once it has', async () => {
+ const app = App.new();
+ app.addPlugin(sceneManagerPlugin);
+ const scenes = app.getResource(SceneManager);
+ let release!: () => void;
+ const gate = new Promise<void>((r) => { release = r; });
+ scenes.register({ name: 'level', setup: async () => { await gate; } });
+ const loading = scenes.load('level');
+ await Promise.resolve();
+ expect(scenes.getLoading()).toEqual(['level']);
+ let settled = false;
+ const waiting = scenes.whenLoadsSettle().then(() => { settled = true; });
+ await Promise.resolve();
+ expect(settled).toBe(false);
+ release();
+ await loading; await waiting;
+ expect(settled).toBe(true);
+ expect(scenes.getLoading()).toEqual([]);
+ expect(scenes.isLoaded('level')).toBe(true);
+ });
+});