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(test): unregister mini-game host keyboard subscribers
yhh7ba8659cd3da
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6e668a449..964068e54 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Mini-game verification unregisters keyboard callbacks.** The launch host now removes the DOM listener belonging to each vendor keyboard callback. Repeated platform input binding no longer leaves retired keyboard subscribers active, and duplicate registrations do not multiply notifications.+- **Mini-game verification forwards touch gestures.** The package launch host forwards real canvas touch events and unregisters individual subscribers. The UI controls project now verifies the mini-game touch path alongside Web mouse input, using the modal button's current position instead of the dropdown below it.- **Inertial scrolling travels consistently across frame rates.** Coast displacement integrates the exponentially decaying velocity instead of using its initial value for the whole frame. Frames that cross the stopping threshold integrate only the remaining motion, so a slow frame no longer extends a fling. Constant-speed and immediate-stop settings remain finite.diff --git a/tools/launchers/minigameHost.mjs b/tools/launchers/minigameHost.mjsindex 17e4ca228..1bfef9dc2 100644--- a/tools/launchers/minigameHost.mjs+++ b/tools/launchers/minigameHost.mjs@@ -43,6 +43,28 @@ export function miniGameTouchBridge(canvas) {return api;}+/** Keep keyboard callback identity so a platform unbind removes its DOM listener. */+export function miniGameKeyboardBridge(target) {+ const api = {};+ for (const phase of ['Down', 'Up']) {+ const kind = 'key' + phase.toLowerCase();+ const listeners = new Map();+ api['onKey' + phase] = callback => {+ if (listeners.has(callback)) return;+ const listener = event => callback({ code: event.code, key: event.key });+ listeners.set(callback, listener);+ target.addEventListener(kind, listener);+ };+ api['offKey' + phase] = callback => {+ const listener = listeners.get(callback);+ if (!listener) return;+ target.removeEventListener(kind, listener);+ listeners.delete(callback);+ };+ }+ return api;+}+/*** The page source; `BASE` is substituted with the served root.*@@ -66,6 +88,7 @@ export const HOST_PAGE = (entry, failSubpackage = null) => `<!doctype html>screenCanvas.height = window.innerHeight;document.body.appendChild(screenCanvas);const touchBridge = (${miniGameTouchBridge.toString()})(screenCanvas);+ const keyboardBridge = (${miniGameKeyboardBridge.toString()})(window);let screenTaken = false;const makeCanvas = () => {if (!screenTaken) { screenTaken = true; return screenCanvas; }@@ -161,10 +184,7 @@ export const HOST_PAGE = (entry, failSubpackage = null) => `<!doctype html>};},...touchBridge,- // Keyboard: forwarded from the real DOM so a driven run can reach the game.- onKeyDown: (cb) => window.addEventListener('keydown', (e) => cb({ code: e.code, key: e.key })),- onKeyUp: (cb) => window.addEventListener('keyup', (e) => cb({ code: e.code, key: e.key })),- offKeyDown: noop, offKeyUp: noop,+ ...keyboardBridge,onMemoryWarning: noop, offMemoryWarning: noop,onError: noop, offError: noop,onUnhandledRejection: noop, offUnhandledRejection: noop,diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 64af1e610..e23a34450 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(test): unregister mini-game host keyboard subscribers':+ { note: 'Mini-game verification unregisters keyboard callbacks.' },'fix(test): forward mini-game touch gestures in package verification':{ note: 'Mini-game verification forwards touch gestures.' },'fix(ui): integrate inertial scroll distance across frame rates':diff --git a/tools/tests/minigame-keyboard-bridge.test.ts b/tools/tests/minigame-keyboard-bridge.test.tsnew file mode 100644index 000000000..9a02429c2--- /dev/null+++ b/tools/tests/minigame-keyboard-bridge.test.ts@@ -0,0 +1,43 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { expect, it, vi } from 'vitest';+import { miniGameKeyboardBridge, miniGameTouchBridge, HOST_PAGE } from '../launchers/minigameHost.mjs';+import { MiniGamePlatformAdapter } from '../../sdk/src/platform/minigame/adapter';+import type { MiniGameProfile } from '../../sdk/src/platform/minigame/api';++const keyEvent = (kind: string) => {+ const event = new Event(kind);+ Object.defineProperties(event, { code: { value: 'KeyA' }, key: { value: 'a' } });+ return event;+};++it('unregisters one keyboard subscriber without dropping others or duplicating registrations', () => {+ const target = new EventTarget(), bridge = miniGameKeyboardBridge(target);+ const first = vi.fn(), second = vi.fn();+ for (const phase of ['Down', 'Up']) {+ bridge['onKey' + phase](first); bridge['onKey' + phase](first); bridge['onKey' + phase](second);+ target.dispatchEvent(keyEvent('key' + phase.toLowerCase()));+ bridge['offKey' + phase](first); target.dispatchEvent(keyEvent('key' + phase.toLowerCase()));+ expect(first).toHaveBeenCalledWith({ code: 'KeyA', key: 'a' });+ expect(first).toHaveBeenCalledTimes(1); expect(second).toHaveBeenCalledTimes(2);+ first.mockClear(); second.mockClear();+ }+ expect(HOST_PAGE('game.js')).toContain(miniGameKeyboardBridge.toString());+});++it('the real platform adapter leaves no retired keyboard callbacks after 100 binding cycles', () => {+ const target = new EventTarget();+ const global = { ...miniGameKeyboardBridge(target), ...miniGameTouchBridge(new EventTarget()) };+ const adapter = new MiniGamePlatformAdapter({ id: 'wechat', global } as unknown as MiniGameProfile);+ const retired = [];+ for (let i = 0; i < 100; i++) {+ const callbacks = { onKeyDown: vi.fn(), onKeyUp: vi.fn(), onPointerMove: vi.fn(),+ onPointerDown: vi.fn(), onPointerUp: vi.fn(), onWheel: vi.fn() };+ adapter.bindInputEvents(callbacks);+ target.dispatchEvent(keyEvent('keydown'));+ expect(callbacks.onKeyDown).toHaveBeenCalledOnce();+ adapter.unbindInputEvents(); retired.push(callbacks.onKeyDown);+ }+ target.dispatchEvent(keyEvent('keydown'));+ for (const callback of retired) expect(callback).toHaveBeenCalledOnce();+});