@dloizides/game-e2e 1.3.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  All notable changes to `@dloizides/game-e2e` are documented here.
4
4
 
5
+ ## 1.4.0
6
+
7
+ GAME-E2E-PRESS-1 "Faster pad presses in game-e2e walks":
8
+
9
+ - `stubGamepad().press` holds the button for exactly one sampled frame and resolves once a reader has seen it
10
+ released (`navigator.getGamepads()` calls latched to `document.timeline.currentTime`), instead of 3+3 fixed
11
+ `requestAnimationFrame` ticks. Under CPU load the old hold outlasted the 380 ms pad repeat delay and moved focus
12
+ twice, so the walk skipped rows. `STUB_FRAMES` is now 1; `PRESS_WAIT_FRAMES` (240) caps the wait when no reader polls.
13
+
5
14
  ## 1.3.0
6
15
 
7
16
  NAV-1 "Every game menu works with arrow keys and controller", phase 4 (canvas menus):
package/dist/index.d.mts CHANGED
@@ -252,7 +252,7 @@ interface PadDriver {
252
252
  press(button: number): Promise<void>;
253
253
  hold(axis: number, value: number): Promise<void>;
254
254
  }
255
- declare const STUB_FRAMES = 3;
255
+ declare const STUB_FRAMES = 1;
256
256
  /** Replaces navigator.getGamepads with one standard pad (call before the page loads); returns its driver. */
257
257
  declare function stubGamepad(page: Page): Promise<PadDriver>;
258
258
 
package/dist/index.d.ts CHANGED
@@ -252,7 +252,7 @@ interface PadDriver {
252
252
  press(button: number): Promise<void>;
253
253
  hold(axis: number, value: number): Promise<void>;
254
254
  }
255
- declare const STUB_FRAMES = 3;
255
+ declare const STUB_FRAMES = 1;
256
256
  /** Replaces navigator.getGamepads with one standard pad (call before the page loads); returns its driver. */
257
257
  declare function stubGamepad(page: Page): Promise<PadDriver>;
258
258
 
package/dist/index.js CHANGED
@@ -468,37 +468,65 @@ async function expectNoHorizontalOverflow(page, where) {
468
468
  }
469
469
 
470
470
  // src/gamepad-stub.ts
471
- var STUB_FRAMES = 3;
471
+ var STUB_FRAMES = 1;
472
+ var PRESS_WAIT_FRAMES = 240;
473
+ var IDLE_RELEASE_FRAMES = 3;
472
474
  var STANDARD_BUTTON_COUNT = 17;
473
475
  var AXIS_COUNT = 4;
476
+ function installStub([buttons, axes]) {
477
+ const pad = { id: "nav-walk-stub", index: 0, connected: true, mapping: "standard", timestamp: 0, axes: new Array(axes).fill(0), buttons: Array.from({ length: buttons }, () => ({ pressed: false, touched: false, value: 0 })) };
478
+ const latch = { button: -1, frame: null, released: true, reads: 0 };
479
+ const sample = () => {
480
+ const now = document.timeline.currentTime;
481
+ const held = pad.buttons[latch.button];
482
+ if (held !== void 0) {
483
+ latch.reads += 1;
484
+ }
485
+ if (held !== void 0 && latch.frame === null) {
486
+ latch.frame = typeof now === "number" ? now : 0;
487
+ } else if (held !== void 0 && now !== latch.frame) {
488
+ held.pressed = false;
489
+ held.value = 0;
490
+ latch.button = -1;
491
+ latch.released = true;
492
+ }
493
+ return [pad, null, null, null];
494
+ };
495
+ Object.assign(window, { __navPad: pad, __navPress: latch });
496
+ Object.defineProperty(navigator, "getGamepads", { configurable: true, value: sample });
497
+ window.addEventListener("load", () => window.dispatchEvent(Object.assign(new Event("gamepadconnected"), { gamepad: pad })));
498
+ }
499
+ async function pressInPage([button, maxFrames, idleFrames]) {
500
+ const { __navPad: pad, __navPress: latch } = window;
501
+ const target = pad.buttons[button];
502
+ if (target === void 0) {
503
+ return;
504
+ }
505
+ target.pressed = true;
506
+ target.value = 1;
507
+ Object.assign(latch, { button, frame: null, released: false, reads: 0 });
508
+ await new Promise((resolve) => {
509
+ let seen = 0;
510
+ let idle = 0;
511
+ const step = (left) => {
512
+ idle = latch.reads === seen ? idle + 1 : 0;
513
+ seen = latch.reads;
514
+ if (latch.released || left === 0 || idle >= idleFrames) {
515
+ target.pressed = false;
516
+ target.value = 0;
517
+ Object.assign(latch, { button: -1, frame: null, released: true });
518
+ resolve();
519
+ return;
520
+ }
521
+ requestAnimationFrame(() => step(left - 1));
522
+ };
523
+ requestAnimationFrame(() => step(maxFrames));
524
+ });
525
+ }
474
526
  async function stubGamepad(page) {
475
- await page.addInitScript(([buttons, axes]) => {
476
- const pad = { id: "nav-walk-stub", index: 0, connected: true, mapping: "standard", timestamp: 0, axes: new Array(axes).fill(0), buttons: Array.from({ length: buttons }, () => ({ pressed: false, touched: false, value: 0 })) };
477
- Object.assign(window, { __navPad: pad });
478
- Object.defineProperty(navigator, "getGamepads", { configurable: true, value: () => [pad, null, null, null] });
479
- window.addEventListener("load", () => window.dispatchEvent(Object.assign(new Event("gamepadconnected"), { gamepad: pad })));
480
- }, [STANDARD_BUTTON_COUNT, AXIS_COUNT]);
527
+ await page.addInitScript(installStub, [STANDARD_BUTTON_COUNT, AXIS_COUNT]);
481
528
  return {
482
- press: (button) => page.evaluate(async ([b, n]) => {
483
- const pad = window.__navPad;
484
- const frames = (k) => new Promise((resolve) => {
485
- const step = (left) => {
486
- if (left === 0) {
487
- resolve();
488
- return;
489
- }
490
- requestAnimationFrame(() => step(left - 1));
491
- };
492
- step(k);
493
- });
494
- const target = pad.buttons[b] ?? { pressed: false, value: 0 };
495
- target.pressed = true;
496
- target.value = 1;
497
- await frames(n);
498
- target.pressed = false;
499
- target.value = 0;
500
- await frames(n);
501
- }, [button, STUB_FRAMES]),
529
+ press: (button) => page.evaluate(pressInPage, [button, PRESS_WAIT_FRAMES, IDLE_RELEASE_FRAMES]),
502
530
  hold: (axis, value) => page.evaluate(([a, v]) => {
503
531
  window.__navPad.axes[a] = v;
504
532
  }, [axis, value])