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): a stepped frame hands the boundary back to its caller

yhh82d66e8bb198
diff --git a/sdk/src/app/app.ts b/sdk/src/app/app.ts
index daa98f575..20a488976 100644
--- a/sdk/src/app/app.ts
+++ b/sdk/src/app/app.ts
@@ -858,7 +858,11 @@ export class App {
if (wasRunning) {
this.lastTime_ = platformNow();
this.running_ = true;
- void this.mainLoop();
+ // Resumed on the next animation frame, not by starting one here:
+ // a frame begun inside this call's own microtask drain swallows
+ // the input edge a caller injects the moment it returns.
+ if (typeof requestAnimationFrame === 'function') requestAnimationFrame(this.mainLoop);
+ else void this.mainLoop();
}
}
}
diff --git a/sdk/tests/app-tick.test.ts b/sdk/tests/app-tick.test.ts
index b1d514fb3..43fadd30a 100644
--- a/sdk/tests/app-tick.test.ts
+++ b/sdk/tests/app-tick.test.ts
@@ -5,6 +5,8 @@ import { App } from '../src/app/app';
import { Schedule, defineSystem } from '../src/ecs/system';
import { Time } from '../src/ecs/resource';
import { defineResource, Res } from '../src/ecs/resource';
+import { setPlatform } from '../src/platform/base';
+import type { PlatformAdapter } from '../src/platform/types';
describe('App.tick()', () => {
it('should lazily initialize runner and Time resource', async () => {
@@ -165,4 +167,29 @@ describe('App.stepFrames()', () => {
await app.stepFrames();
expect(app.getResource(Time).frameCount).toBe(1);
});
+
+ // A driver injects an input edge the moment this returns; a frame started
+ // inside the call would clear it at its Last before anything sampled it.
+ it('leaves the resumed loop to the next animation frame, so the caller keeps the boundary', async () => {
+ // The loop reads the clock through the platform, which only a real entry
+ // point installs.
+ setPlatform({ now: () => performance.now() } as unknown as PlatformAdapter);
+ const app = App.new();
+ let frames = 0;
+ app.addSystemToSchedule(Schedule.Update, defineSystem(
+ [], () => { frames++; }, { name: 'Count' },
+ ));
+
+ await app.run();
+ for (let i = 0; i < 20; i++) await Promise.resolve(); // the loop's first frame finishes
+ const before = frames;
+
+ await app.stepFrames(2);
+ expect(frames).toBe(before + 2);
+
+ for (let i = 0; i < 20; i++) await Promise.resolve();
+ expect(frames).toBe(before + 2);
+
+ app.quit();
+ });
});