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
fix(app): the frame loop runs on vivo's quick game, not just its first frame
esengine67c475ed6732
diff --git a/sdk/src/app/app.ts b/sdk/src/app/app.tsindex 23e5bf043..c5f54cf42 100644--- a/sdk/src/app/app.ts+++ b/sdk/src/app/app.ts@@ -1044,7 +1044,7 @@ export class App {// 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);+ if (typeof requestAnimationFrame === 'function') requestAnimationFrame(this.onAnimationFrame_);else void this.mainLoop();}}@@ -1066,6 +1066,11 @@ export class App {/*** @param rafTime The host's animation-frame timestamp, when it passes one.*/+ /** What every requestAnimationFrame is handed: a plain function, because a host+ * may refuse an async one — vivo's quick game warned "handler is not a function"+ * and every game stood on its first frame. */+ private readonly onAnimationFrame_ = (rafTime?: number): Promise<void> => this.mainLoop(rafTime);+private mainLoop = async (rafTime?: number): Promise<void> => {if (!this.running_) {return;@@ -1081,7 +1086,7 @@ export class App {// loses to jitter half the time and waits a third (30 fps on 60 Hz ran at 24).if (this.targetFrameInterval_ > 0) {if (currentTime < this.nextCapFrameAt_ - CAP_JITTER_MS) {- requestAnimationFrame(this.mainLoop);+ requestAnimationFrame(this.onAnimationFrame_);return;}// More than a frame behind (a stall, a hidden page): the cadence restarts here.@@ -1101,7 +1106,7 @@ export class App {await this.flushStartupSystems_();await this.runFrame_(delta);- requestAnimationFrame(this.mainLoop);+ requestAnimationFrame(this.onAnimationFrame_);};quit(options?: { keepRenderer?: boolean }): void {diff --git a/sdk/tests/app-tick.test.ts b/sdk/tests/app-tick.test.tsindex 639efb7c9..4483ca6bf 100644--- a/sdk/tests/app-tick.test.ts+++ b/sdk/tests/app-tick.test.ts@@ -247,6 +247,38 @@ describe('App.stepFrames()', () => {}});+ it('keeps running on a host whose requestAnimationFrame refuses an async handler', async () => {+ // vivo's quick-game runtime: "requestAnimationFrame handler is not a function"+ // for anything whose tag is not [object Function] — and an async function's is not.+ const realRaf = globalThis.requestAnimationFrame;+ let pending: ((ts: number) => void) | null = null;+ globalThis.requestAnimationFrame = ((fn: FrameRequestCallback) => {+ if (Object.prototype.toString.call(fn) !== '[object Function]') return 0;+ pending = fn as unknown as (ts: number) => void;+ return 1;+ }) as unknown as typeof globalThis.requestAnimationFrame;+ setPlatform({ now: () => 1000 } as unknown as PlatformAdapter);+ const app = App.new();+ let frames = 0;+ app.addSystemToSchedule(Schedule.Update, defineSystem([], () => { frames++; }, { name: 'CountFrames' }));+ const settle = async (): Promise<void> => { for (let i = 0; i < 30; i++) await Promise.resolve(); };+ try {+ await app.run();+ await settle();+ for (let i = 1; i <= 5; i++) {+ const fire = pending as ((ts: number) => void) | null;+ if (!fire) break;+ pending = null;+ fire(1000 + i * 16.7);+ await settle();+ }+ expect(frames).toBeGreaterThanOrEqual(6);+ } finally {+ app.quit();+ globalThis.requestAnimationFrame = realRaf;+ }+ });+it('defaults to one frame', async () => {const app = App.new();await app.stepFrames();