@flighthq/application 0.5.0-next.1944.cd6b6e7 → 0.5.1-edge.638.d4dbd4e

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.
@@ -1,29 +1,23 @@
1
1
  import { connectSignal, emitSignal } from '@flighthq/signals/contract';
2
- import type { LoopBackend } from '@flighthq/types/contract';
2
+ import { EntityRuntimeKey } from '@flighthq/types/contract';
3
+ import type { ApplicationExitBackend, ApplicationVisibilityBackend, LoopBackend } from '@flighthq/types/contract';
3
4
 
4
5
  import {
5
6
  attachApplicationExit,
6
7
  attachApplicationLifecycle,
7
8
  createApplication,
8
- createWebLoopBackend,
9
9
  detachApplicationExit,
10
10
  disposeApplication,
11
11
  enableApplicationLifecycleSignals,
12
- explainLoopBackend,
13
12
  forEachApplicationWindow,
14
13
  getApplicationFrameRate,
15
14
  getApplicationMainWindow,
16
15
  getApplicationWindows,
17
- getLoopBackend,
18
- installLoopHostBackend,
19
16
  isApplicationRunning,
20
- observeLoopHostResult,
21
17
  pauseApplicationLoop,
22
18
  registerApplicationWindow,
23
- resetLoopBackendForTest,
24
19
  resumeApplicationLoop,
25
20
  setApplicationMainWindow,
26
- setLoopBackend,
27
21
  startApplicationLoop,
28
22
  stepApplicationLoop,
29
23
  stopApplicationLoop,
@@ -54,42 +48,96 @@ function makeManualLoopBackend(): LoopBackend & { tick: (time: number) => void;
54
48
  };
55
49
  }
56
50
 
57
- afterEach(() => resetLoopBackendForTest());
51
+ type RecordingApplicationVisibilityBackend = ApplicationVisibilityBackend & { visible: boolean };
52
+
53
+ type LoopTestHost = {
54
+ readonly app: {
55
+ readonly loop: LoopBackend;
56
+ readonly visibility: RecordingApplicationVisibilityBackend;
57
+ };
58
+ };
59
+
60
+ function createLoopTestHost(loop: LoopBackend, visible = true): LoopTestHost {
61
+ const visibility: RecordingApplicationVisibilityBackend = {
62
+ visible,
63
+ isVisible() {
64
+ return visibility.visible;
65
+ },
66
+ };
67
+ return { app: { loop, visibility } };
68
+ }
69
+
70
+ type RecordingApplicationExitBackend = ApplicationExitBackend & {
71
+ readonly calls: string[];
72
+ emit(): void;
73
+ };
74
+
75
+ type ExitTestHost = { readonly app: { readonly exit: RecordingApplicationExitBackend } };
76
+
77
+ function createExitTestHost(): ExitTestHost {
78
+ const calls: string[] = [];
79
+ const listeners = new Set<() => void>();
80
+ return {
81
+ app: {
82
+ exit: {
83
+ calls,
84
+ emit() {
85
+ for (const listener of listeners) listener();
86
+ },
87
+ subscribe(listener) {
88
+ calls.push('subscribe');
89
+ listeners.add(listener);
90
+ },
91
+ unsubscribe(listener) {
92
+ calls.push('unsubscribe');
93
+ listeners.delete(listener);
94
+ },
95
+ },
96
+ },
97
+ };
98
+ }
58
99
 
59
100
  describe('attachApplicationExit', () => {
60
- it('emits onExit on beforeunload', () => {
101
+ it('emits onExit through the host subscription', () => {
102
+ const host = createExitTestHost();
61
103
  const app = createApplication();
62
104
  let called = false;
63
105
  connectSignal(app.onExit, () => {
64
106
  called = true;
65
107
  });
66
108
 
67
- attachApplicationExit(app);
68
- window.dispatchEvent(new Event('beforeunload'));
109
+ attachApplicationExit(host, app);
110
+ host.app.exit.emit();
69
111
 
70
112
  expect(called).toBe(true);
113
+ expect(host.app.exit.calls).toEqual(['subscribe']);
71
114
  });
72
115
 
73
- it('replaces a previous exit listener when called again', () => {
116
+ it('unsubscribes the prior host before replacing the exit listener', () => {
117
+ const first = createExitTestHost();
118
+ const second = createExitTestHost();
74
119
  const app = createApplication();
75
120
  let count = 0;
76
121
  connectSignal(app.onExit, () => count++);
77
122
 
78
- attachApplicationExit(app);
79
- attachApplicationExit(app);
80
- window.dispatchEvent(new Event('beforeunload'));
123
+ attachApplicationExit(first, app);
124
+ attachApplicationExit(second, app);
125
+ first.app.exit.emit();
126
+ second.app.exit.emit();
81
127
 
82
128
  expect(count).toBe(1);
129
+ expect(first.app.exit.calls).toEqual(['subscribe', 'unsubscribe']);
130
+ expect(second.app.exit.calls).toEqual(['subscribe']);
83
131
  });
84
132
  });
85
133
 
86
134
  describe('attachApplicationLifecycle', () => {
87
135
  it('pauses the loop when the window deactivates', () => {
88
136
  const backend = makeManualLoopBackend();
89
- setLoopBackend(backend);
137
+ const host = createLoopTestHost(backend);
90
138
  const app = createApplication();
91
139
  const win = createApplicationWindow();
92
- startApplicationLoop(app);
140
+ startApplicationLoop(host, app);
93
141
  attachApplicationLifecycle(app, win);
94
142
 
95
143
  // Simulate window deactivation by emitting its signal directly.
@@ -100,10 +148,10 @@ describe('attachApplicationLifecycle', () => {
100
148
 
101
149
  it('resumes the loop when the window activates', () => {
102
150
  const backend = makeManualLoopBackend();
103
- setLoopBackend(backend);
151
+ const host = createLoopTestHost(backend);
104
152
  const app = createApplication();
105
153
  const win = createApplicationWindow();
106
- startApplicationLoop(app);
154
+ startApplicationLoop(host, app);
107
155
  attachApplicationLifecycle(app, win);
108
156
 
109
157
  emitSignal(win.onDeactivate);
@@ -115,7 +163,13 @@ describe('attachApplicationLifecycle', () => {
115
163
  });
116
164
 
117
165
  describe('createApplication', () => {
166
+ it('creates an Entity-backed application identity', () => {
167
+ const app = createApplication();
168
+ expect(EntityRuntimeKey in app).toBe(true);
169
+ });
170
+
118
171
  it('returns signals with no side effects', () => {
172
+ const host = createExitTestHost();
119
173
  const app = createApplication();
120
174
  expect(app.onUpdate).toBeDefined();
121
175
  expect(app.onRender).toBeDefined();
@@ -125,7 +179,7 @@ describe('createApplication', () => {
125
179
  connectSignal(app.onExit, () => {
126
180
  exitCalled = true;
127
181
  });
128
- window.dispatchEvent(new Event('beforeunload'));
182
+ host.app.exit.emit();
129
183
  expect(exitCalled).toBe(false);
130
184
  });
131
185
 
@@ -148,50 +202,29 @@ describe('createApplication', () => {
148
202
  });
149
203
  });
150
204
 
151
- describe('createWebLoopBackend', () => {
152
- it('wraps requestAnimationFrame', () => {
153
- const raf = vi.fn().mockReturnValue(42);
154
- vi.stubGlobal('requestAnimationFrame', raf);
155
- vi.stubGlobal('cancelAnimationFrame', vi.fn());
156
- const backend = createWebLoopBackend();
157
- const handle = backend.requestFrame(() => {});
158
- expect(raf).toHaveBeenCalled();
159
- expect(handle).toBe(42);
160
- vi.unstubAllGlobals();
161
- });
162
-
163
- it('wraps cancelAnimationFrame', () => {
164
- const caf = vi.fn();
165
- vi.stubGlobal('requestAnimationFrame', vi.fn().mockReturnValue(1));
166
- vi.stubGlobal('cancelAnimationFrame', caf);
167
- const backend = createWebLoopBackend();
168
- const handle = backend.requestFrame(() => {});
169
- backend.cancelFrame(handle);
170
- expect(caf).toHaveBeenCalledWith(1);
171
- vi.unstubAllGlobals();
172
- });
173
- });
174
-
175
205
  describe('detachApplicationExit', () => {
176
206
  it('removes the listener', () => {
207
+ const host = createExitTestHost();
177
208
  const app = createApplication();
178
209
  let called = false;
179
210
  connectSignal(app.onExit, () => {
180
211
  called = true;
181
212
  });
182
213
 
183
- attachApplicationExit(app);
214
+ attachApplicationExit(host, app);
184
215
  detachApplicationExit(app);
185
- window.dispatchEvent(new Event('beforeunload'));
216
+ host.app.exit.emit();
186
217
 
187
218
  expect(called).toBe(false);
219
+ expect(host.app.exit.calls).toEqual(['subscribe', 'unsubscribe']);
188
220
  });
189
221
  });
190
222
 
191
223
  describe('disposeApplication', () => {
192
224
  it('stops loop, removes exit listener, and sets isRunning false', () => {
193
225
  const backend = makeManualLoopBackend();
194
- setLoopBackend(backend);
226
+ const loopHost = createLoopTestHost(backend);
227
+ const exitHost = createExitTestHost();
195
228
 
196
229
  const app = createApplication();
197
230
  let exitCalled = false;
@@ -199,14 +232,15 @@ describe('disposeApplication', () => {
199
232
  exitCalled = true;
200
233
  });
201
234
 
202
- startApplicationLoop(app);
203
- attachApplicationExit(app);
235
+ startApplicationLoop(loopHost, app);
236
+ attachApplicationExit(exitHost, app);
204
237
  expect(app.isRunning).toBe(true);
205
238
  disposeApplication(app);
206
239
 
207
240
  expect(app.isRunning).toBe(false);
208
- window.dispatchEvent(new Event('beforeunload'));
241
+ exitHost.app.exit.emit();
209
242
  expect(exitCalled).toBe(false);
243
+ expect(exitHost.app.exit.calls).toEqual(['subscribe', 'unsubscribe']);
210
244
  });
211
245
  });
212
246
 
@@ -230,48 +264,6 @@ describe('enableApplicationLifecycleSignals', () => {
230
264
  });
231
265
  });
232
266
 
233
- describe('explainLoopBackend', () => {
234
- it('reports host-not-enabled when no backend is installed', () => {
235
- expect(explainLoopBackend()).toEqual({
236
- conflict: false,
237
- layer: 'host-not-enabled',
238
- operation: null,
239
- viability: 'unobserved',
240
- });
241
- });
242
-
243
- it('reports custom layer when custom backend is set', () => {
244
- setLoopBackend(makeManualLoopBackend());
245
- expect(explainLoopBackend().layer).toBe('custom');
246
- });
247
-
248
- it('reports host layer after install', () => {
249
- installLoopHostBackend(createWebLoopBackend());
250
- expect(explainLoopBackend().layer).toBe('host');
251
- expect(explainLoopBackend().viability).toBe('unobserved');
252
- });
253
-
254
- it('reports conflict when a distinct host is installed twice', () => {
255
- installLoopHostBackend(createWebLoopBackend());
256
- installLoopHostBackend(createWebLoopBackend());
257
- expect(explainLoopBackend().conflict).toBe(true);
258
- });
259
-
260
- it('preserves first host on conflict', () => {
261
- const first = createWebLoopBackend();
262
- installLoopHostBackend(first);
263
- installLoopHostBackend(createWebLoopBackend());
264
- expect(getLoopBackend()).toBe(first);
265
- });
266
-
267
- it('is idempotent — same reference does not conflict', () => {
268
- const backend = createWebLoopBackend();
269
- installLoopHostBackend(backend);
270
- installLoopHostBackend(backend);
271
- expect(explainLoopBackend().conflict).toBe(false);
272
- });
273
- });
274
-
275
267
  describe('forEachApplicationWindow', () => {
276
268
  it('iterates all registered windows', () => {
277
269
  const app = createApplication();
@@ -293,9 +285,9 @@ describe('getApplicationFrameRate', () => {
293
285
 
294
286
  it('returns approximate FPS after several ticks', () => {
295
287
  const backend = makeManualLoopBackend();
296
- setLoopBackend(backend);
288
+ const host = createLoopTestHost(backend);
297
289
  const app = createApplication();
298
- startApplicationLoop(app);
290
+ startApplicationLoop(host, app);
299
291
  // Simulate 60fps ticks (~16.67ms each).
300
292
  for (let i = 0; i <= 60; i++) {
301
293
  backend.tick(i * 16.67);
@@ -350,48 +342,6 @@ describe('getApplicationWindows', () => {
350
342
  });
351
343
  });
352
344
 
353
- describe('getLoopBackend', () => {
354
- it('returns null when no backend is installed', () => {
355
- expect(getLoopBackend()).toBeNull();
356
- });
357
-
358
- it('returns custom backend over host', () => {
359
- installLoopHostBackend(createWebLoopBackend());
360
- const custom = makeManualLoopBackend();
361
- setLoopBackend(custom);
362
- expect(getLoopBackend()).toBe(custom);
363
- });
364
-
365
- it('returns host backend when no custom is set', () => {
366
- const host = createWebLoopBackend();
367
- installLoopHostBackend(host);
368
- expect(getLoopBackend()).toBe(host);
369
- });
370
- });
371
-
372
- describe('installLoopHostBackend', () => {
373
- it('installs a host backend retrievable via getLoopBackend', () => {
374
- const backend = createWebLoopBackend();
375
- installLoopHostBackend(backend);
376
- expect(getLoopBackend()).toBe(backend);
377
- });
378
-
379
- it('first-host-wins — second distinct backend sets conflict', () => {
380
- const first = createWebLoopBackend();
381
- installLoopHostBackend(first);
382
- installLoopHostBackend(createWebLoopBackend());
383
- expect(getLoopBackend()).toBe(first);
384
- expect(explainLoopBackend().conflict).toBe(true);
385
- });
386
-
387
- it('same reference is idempotent — no conflict', () => {
388
- const backend = createWebLoopBackend();
389
- installLoopHostBackend(backend);
390
- installLoopHostBackend(backend);
391
- expect(explainLoopBackend().conflict).toBe(false);
392
- });
393
- });
394
-
395
345
  describe('isApplicationRunning', () => {
396
346
  it('returns false before loop is started', () => {
397
347
  const app = createApplication();
@@ -400,84 +350,32 @@ describe('isApplicationRunning', () => {
400
350
 
401
351
  it('returns true after loop is started', () => {
402
352
  const backend = makeManualLoopBackend();
403
- setLoopBackend(backend);
353
+ const host = createLoopTestHost(backend);
404
354
  const app = createApplication();
405
- startApplicationLoop(app);
355
+ startApplicationLoop(host, app);
406
356
  expect(isApplicationRunning(app)).toBe(true);
407
357
  stopApplicationLoop(app);
408
358
  });
409
359
 
410
360
  it('returns false after loop is stopped', () => {
411
361
  const backend = makeManualLoopBackend();
412
- setLoopBackend(backend);
362
+ const host = createLoopTestHost(backend);
413
363
  const app = createApplication();
414
- startApplicationLoop(app);
364
+ startApplicationLoop(host, app);
415
365
  stopApplicationLoop(app);
416
366
  expect(isApplicationRunning(app)).toBe(false);
417
367
  });
418
368
  });
419
369
 
420
- describe('observeLoopHostResult', () => {
421
- it('records available after success', () => {
422
- installLoopHostBackend(createWebLoopBackend());
423
- observeLoopHostResult('requestFrame', true);
424
- expect(explainLoopBackend()).toEqual({
425
- conflict: false,
426
- layer: 'host',
427
- operation: 'requestFrame',
428
- viability: 'available',
429
- });
430
- });
431
-
432
- it('records runtime-api-unavailable after failure', () => {
433
- installLoopHostBackend(createWebLoopBackend());
434
- observeLoopHostResult('requestFrame', false);
435
- expect(explainLoopBackend().viability).toBe('runtime-api-unavailable');
436
- });
437
-
438
- it('later observation replaces prior — loss then recovery', () => {
439
- installLoopHostBackend(createWebLoopBackend());
440
- observeLoopHostResult('requestFrame', false);
441
- expect(explainLoopBackend().viability).toBe('runtime-api-unavailable');
442
- observeLoopHostResult('requestFrame', true);
443
- expect(explainLoopBackend().viability).toBe('available');
444
- });
445
-
446
- it('recovery then loss — available reverts to unavailable', () => {
447
- installLoopHostBackend(createWebLoopBackend());
448
- observeLoopHostResult('requestFrame', true);
449
- expect(explainLoopBackend().viability).toBe('available');
450
- observeLoopHostResult('requestFrame', false);
451
- expect(explainLoopBackend().viability).toBe('runtime-api-unavailable');
452
- });
453
-
454
- it('cancelFrame observation is recorded independently', () => {
455
- installLoopHostBackend(createWebLoopBackend());
456
- observeLoopHostResult('requestFrame', true);
457
- expect(explainLoopBackend().operation).toBe('requestFrame');
458
- observeLoopHostResult('cancelFrame', true);
459
- expect(explainLoopBackend().operation).toBe('cancelFrame');
460
- expect(explainLoopBackend().viability).toBe('available');
461
- });
462
-
463
- it('cancelFrame failure overwrites prior requestFrame success', () => {
464
- installLoopHostBackend(createWebLoopBackend());
465
- observeLoopHostResult('requestFrame', true);
466
- observeLoopHostResult('cancelFrame', false);
467
- expect(explainLoopBackend().operation).toBe('cancelFrame');
468
- expect(explainLoopBackend().viability).toBe('runtime-api-unavailable');
469
- });
470
- });
471
-
472
370
  describe('pauseApplicationLoop', () => {
473
371
  it('pauses emission without cancelling the rAF chain', () => {
474
372
  const backend = makeManualLoopBackend();
475
- setLoopBackend(backend);
373
+ const host = createLoopTestHost(backend);
476
374
  const app = createApplication();
477
375
  const updates: number[] = [];
478
376
  connectSignal(app.onUpdate, (dt) => updates.push(dt));
479
377
 
480
- startApplicationLoop(app);
378
+ startApplicationLoop(host, app);
481
379
  backend.tick(0);
482
380
  backend.tick(16);
483
381
  pauseApplicationLoop(app);
@@ -489,9 +387,9 @@ describe('pauseApplicationLoop', () => {
489
387
 
490
388
  it('is idempotent', () => {
491
389
  const backend = makeManualLoopBackend();
492
- setLoopBackend(backend);
390
+ const host = createLoopTestHost(backend);
493
391
  const app = createApplication();
494
- startApplicationLoop(app);
392
+ startApplicationLoop(host, app);
495
393
  pauseApplicationLoop(app);
496
394
  pauseApplicationLoop(app); // second call should not throw
497
395
  expect(app.isRunning).toBe(false);
@@ -516,26 +414,15 @@ describe('registerApplicationWindow', () => {
516
414
  });
517
415
  });
518
416
 
519
- describe('resetLoopBackendForTest', () => {
520
- it('clears all state', () => {
521
- installLoopHostBackend(createWebLoopBackend());
522
- observeLoopHostResult('requestFrame', true);
523
- setLoopBackend(makeManualLoopBackend());
524
- resetLoopBackendForTest();
525
- expect(explainLoopBackend().layer).toBe('host-not-enabled');
526
- expect(getLoopBackend()).toBeNull();
527
- });
528
- });
529
-
530
417
  describe('resumeApplicationLoop', () => {
531
418
  it('resumes emission after pause without dumping the gap delta', () => {
532
419
  const backend = makeManualLoopBackend();
533
- setLoopBackend(backend);
420
+ const host = createLoopTestHost(backend);
534
421
  const app = createApplication();
535
422
  const updates: number[] = [];
536
423
  connectSignal(app.onUpdate, (dt) => updates.push(dt));
537
424
 
538
- startApplicationLoop(app);
425
+ startApplicationLoop(host, app);
539
426
  backend.tick(0);
540
427
  backend.tick(16);
541
428
  pauseApplicationLoop(app);
@@ -550,9 +437,9 @@ describe('resumeApplicationLoop', () => {
550
437
 
551
438
  it('is a no-op when not paused', () => {
552
439
  const backend = makeManualLoopBackend();
553
- setLoopBackend(backend);
440
+ const host = createLoopTestHost(backend);
554
441
  const app = createApplication();
555
- startApplicationLoop(app);
442
+ startApplicationLoop(host, app);
556
443
  resumeApplicationLoop(app); // not paused, should not throw
557
444
  expect(app.isRunning).toBe(true);
558
445
  stopApplicationLoop(app);
@@ -569,61 +456,37 @@ describe('setApplicationMainWindow', () => {
569
456
  });
570
457
  });
571
458
 
572
- describe('setLoopBackend', () => {
573
- it('clears custom back to host or null when passed null', () => {
574
- const custom = makeManualLoopBackend();
575
- setLoopBackend(custom);
576
- expect(getLoopBackend()).toBe(custom);
577
- setLoopBackend(null);
578
- expect(getLoopBackend()).toBeNull();
579
- });
580
-
581
- it('clears custom but keeps host when passed null', () => {
582
- const host = createWebLoopBackend();
583
- installLoopHostBackend(host);
584
- setLoopBackend(makeManualLoopBackend());
585
- setLoopBackend(null);
586
- expect(getLoopBackend()).toBe(host);
587
- });
588
- });
589
-
590
459
  describe('startApplicationLoop', () => {
591
- it('is a no-op when no backend is installed', () => {
592
- const app = createApplication();
593
- startApplicationLoop(app);
594
- expect(app.isRunning).toBe(false);
595
- });
596
-
597
460
  it('sets isRunning to true', () => {
598
461
  const backend = makeManualLoopBackend();
599
- setLoopBackend(backend);
462
+ const host = createLoopTestHost(backend);
600
463
  const app = createApplication();
601
- startApplicationLoop(app);
464
+ startApplicationLoop(host, app);
602
465
  expect(app.isRunning).toBe(true);
603
466
  stopApplicationLoop(app);
604
467
  });
605
468
 
606
469
  it('replaces a previous loop when called again', () => {
607
470
  const backend = makeManualLoopBackend();
608
- setLoopBackend(backend);
471
+ const host = createLoopTestHost(backend);
609
472
  const app = createApplication();
610
- startApplicationLoop(app);
473
+ startApplicationLoop(host, app);
611
474
  const initialCancelCount = backend.cancelCount;
612
- startApplicationLoop(app);
475
+ startApplicationLoop(host, app);
613
476
  expect(backend.cancelCount).toBeGreaterThan(initialCancelCount);
614
477
  stopApplicationLoop(app);
615
478
  });
616
479
 
617
480
  it('emits onUpdate with clamped delta and onRender on each tick', () => {
618
481
  const backend = makeManualLoopBackend();
619
- setLoopBackend(backend);
482
+ const host = createLoopTestHost(backend);
620
483
  const app = createApplication();
621
484
  const updates: number[] = [];
622
485
  let renders = 0;
623
486
  connectSignal(app.onUpdate, (dt) => updates.push(dt));
624
487
  connectSignal(app.onRender, () => renders++);
625
488
 
626
- startApplicationLoop(app);
489
+ startApplicationLoop(host, app);
627
490
  backend.tick(0);
628
491
  backend.tick(100);
629
492
 
@@ -634,12 +497,12 @@ describe('startApplicationLoop', () => {
634
497
 
635
498
  it('clamps large delta gaps to maxDeltaTime', () => {
636
499
  const backend = makeManualLoopBackend();
637
- setLoopBackend(backend);
500
+ const host = createLoopTestHost(backend);
638
501
  const app = createApplication();
639
502
  const updates: number[] = [];
640
503
  connectSignal(app.onUpdate, (dt) => updates.push(dt));
641
504
 
642
- startApplicationLoop(app, { maxDeltaTime: 100 });
505
+ startApplicationLoop(host, app, { maxDeltaTime: 100 });
643
506
  backend.tick(0);
644
507
  backend.tick(5000); // 5 second gap clamped to 100ms
645
508
 
@@ -649,12 +512,12 @@ describe('startApplicationLoop', () => {
649
512
 
650
513
  it('defaults to 250ms max delta clamp', () => {
651
514
  const backend = makeManualLoopBackend();
652
- setLoopBackend(backend);
515
+ const host = createLoopTestHost(backend);
653
516
  const app = createApplication();
654
517
  const updates: number[] = [];
655
518
  connectSignal(app.onUpdate, (dt) => updates.push(dt));
656
519
 
657
- startApplicationLoop(app);
520
+ startApplicationLoop(host, app);
658
521
  backend.tick(0);
659
522
  backend.tick(10000); // 10 second gap clamped to 250ms
660
523
 
@@ -664,10 +527,10 @@ describe('startApplicationLoop', () => {
664
527
 
665
528
  it('accumulates frame count and elapsed time', () => {
666
529
  const backend = makeManualLoopBackend();
667
- setLoopBackend(backend);
530
+ const host = createLoopTestHost(backend);
668
531
  const app = createApplication();
669
532
 
670
- startApplicationLoop(app);
533
+ startApplicationLoop(host, app);
671
534
  backend.tick(0);
672
535
  backend.tick(16);
673
536
  backend.tick(32);
@@ -680,12 +543,12 @@ describe('startApplicationLoop', () => {
680
543
 
681
544
  it('throttles to targetFrameRate when set', () => {
682
545
  const backend = makeManualLoopBackend();
683
- setLoopBackend(backend);
546
+ const host = createLoopTestHost(backend);
684
547
  const app = createApplication();
685
548
  let renders = 0;
686
549
  connectSignal(app.onRender, () => renders++);
687
550
 
688
- startApplicationLoop(app, { targetFrameRate: 30 }); // 33.33ms interval
551
+ startApplicationLoop(host, app, { targetFrameRate: 30 }); // 33.33ms interval
689
552
  backend.tick(0); // first tick: lastTime=-1 so delta=0, accumulated=0 → emits
690
553
  backend.tick(16); // 16ms < 33.33ms → should skip
691
554
  backend.tick(34); // 34ms accumulated ≥ 33.33ms → should emit
@@ -695,18 +558,36 @@ describe('startApplicationLoop', () => {
695
558
  expect(renders).toBeGreaterThanOrEqual(2);
696
559
  stopApplicationLoop(app);
697
560
  });
561
+
562
+ it('pulls visibility from the distinct host query when choosing the frame interval', () => {
563
+ const backend = makeManualLoopBackend();
564
+ const host = createLoopTestHost(backend, false);
565
+ const app = createApplication();
566
+ let renders = 0;
567
+ connectSignal(app.onRender, () => renders++);
568
+
569
+ startApplicationLoop(host, app, { backgroundFrameRate: 1 });
570
+ backend.tick(0);
571
+ backend.tick(500);
572
+ expect(renders).toBe(1);
573
+
574
+ host.app.visibility.visible = true;
575
+ backend.tick(516);
576
+ expect(renders).toBe(2);
577
+ stopApplicationLoop(app);
578
+ });
698
579
  });
699
580
 
700
581
  describe('startApplicationLoop (fixed timestep)', () => {
701
582
  it('emits onFixedUpdate when fixedTimeStep is set', () => {
702
583
  const backend = makeManualLoopBackend();
703
- setLoopBackend(backend);
584
+ const host = createLoopTestHost(backend);
704
585
  const app = createApplication();
705
586
  enableApplicationLifecycleSignals(app);
706
587
  const fixedDeltas: number[] = [];
707
588
  connectSignal(app.onFixedUpdate!, (dt) => fixedDeltas.push(dt));
708
589
 
709
- startApplicationLoop(app, { fixedTimeStep: 16 });
590
+ startApplicationLoop(host, app, { fixedTimeStep: 16 });
710
591
  backend.tick(0);
711
592
  backend.tick(48); // 48ms → 3 fixed steps of 16ms
712
593
 
@@ -717,13 +598,13 @@ describe('startApplicationLoop (fixed timestep)', () => {
717
598
 
718
599
  it('clamps to maxUpdatesPerFrame to prevent spiral-of-death', () => {
719
600
  const backend = makeManualLoopBackend();
720
- setLoopBackend(backend);
601
+ const host = createLoopTestHost(backend);
721
602
  const app = createApplication();
722
603
  enableApplicationLifecycleSignals(app);
723
604
  let fixedCount = 0;
724
605
  connectSignal(app.onFixedUpdate!, () => fixedCount++);
725
606
 
726
- startApplicationLoop(app, { fixedTimeStep: 16, maxUpdatesPerFrame: 3 });
607
+ startApplicationLoop(host, app, { fixedTimeStep: 16, maxUpdatesPerFrame: 3 });
727
608
  backend.tick(0);
728
609
  backend.tick(10000); // huge gap
729
610
 
@@ -733,11 +614,11 @@ describe('startApplicationLoop (fixed timestep)', () => {
733
614
 
734
615
  it('sets interpolationAlpha between 0 and 1 during fixed step', () => {
735
616
  const backend = makeManualLoopBackend();
736
- setLoopBackend(backend);
617
+ const host = createLoopTestHost(backend);
737
618
  const app = createApplication();
738
619
  enableApplicationLifecycleSignals(app);
739
620
 
740
- startApplicationLoop(app, { fixedTimeStep: 16 });
621
+ startApplicationLoop(host, app, { fixedTimeStep: 16 });
741
622
  backend.tick(0);
742
623
  backend.tick(24); // 24ms = 1 full step (16ms) + 8ms remainder → alpha = 8/16 = 0.5
743
624
 
@@ -748,7 +629,7 @@ describe('startApplicationLoop (fixed timestep)', () => {
748
629
 
749
630
  it('routes fixed-update errors through onError and continues the frame', () => {
750
631
  const backend = makeManualLoopBackend();
751
- setLoopBackend(backend);
632
+ const host = createLoopTestHost(backend);
752
633
  const app = createApplication();
753
634
  enableApplicationLifecycleSignals(app);
754
635
  const errors: unknown[] = [];
@@ -762,7 +643,7 @@ describe('startApplicationLoop (fixed timestep)', () => {
762
643
  connectSignal(app.onUpdate, () => updates++);
763
644
  connectSignal(app.onRender, () => renders++);
764
645
 
765
- startApplicationLoop(app, { fixedTimeStep: 16 });
646
+ startApplicationLoop(host, app, { fixedTimeStep: 16 });
766
647
  backend.tick(0);
767
648
  backend.tick(32);
768
649
 
@@ -776,7 +657,7 @@ describe('startApplicationLoop (fixed timestep)', () => {
776
657
  describe('startApplicationLoop (tick-error routing)', () => {
777
658
  it('routes onUpdate errors to onError when lifecycle signals enabled', () => {
778
659
  const backend = makeManualLoopBackend();
779
- setLoopBackend(backend);
660
+ const host = createLoopTestHost(backend);
780
661
  const app = createApplication();
781
662
  enableApplicationLifecycleSignals(app);
782
663
  const errors: unknown[] = [];
@@ -787,7 +668,7 @@ describe('startApplicationLoop (tick-error routing)', () => {
787
668
  throw boom;
788
669
  });
789
670
 
790
- startApplicationLoop(app);
671
+ startApplicationLoop(host, app);
791
672
  backend.tick(0);
792
673
 
793
674
  expect(errors).toContain(boom);
@@ -854,13 +735,13 @@ describe('stepApplicationLoop', () => {
854
735
 
855
736
  it('uses explicit fixed-step options instead of policy from an active backend loop', () => {
856
737
  const backend = makeManualLoopBackend();
857
- setLoopBackend(backend);
738
+ const host = createLoopTestHost(backend);
858
739
  const app = createApplication();
859
740
  enableApplicationLifecycleSignals(app);
860
741
  const fixedDeltas: number[] = [];
861
742
  connectSignal(app.onFixedUpdate!, (delta) => fixedDeltas.push(delta));
862
743
 
863
- startApplicationLoop(app, { fixedTimeStep: 8 });
744
+ startApplicationLoop(host, app, { fixedTimeStep: 8 });
864
745
  stepApplicationLoop(app, 12, { fixedTimeStep: 12 });
865
746
 
866
747
  expect(fixedDeltas).toEqual([12]);
@@ -899,10 +780,10 @@ describe('stepApplicationLoop', () => {
899
780
 
900
781
  it('uses an explicit max delta instead of the active backend loop clamp', () => {
901
782
  const backend = makeManualLoopBackend();
902
- setLoopBackend(backend);
783
+ const host = createLoopTestHost(backend);
903
784
  const app = createApplication();
904
785
 
905
- startApplicationLoop(app, { maxDeltaTime: 100 });
786
+ startApplicationLoop(host, app, { maxDeltaTime: 100 });
906
787
  stepApplicationLoop(app, 9999, { maxDeltaTime: 40 });
907
788
 
908
789
  expect(app.deltaTime).toBe(40);
@@ -968,20 +849,20 @@ describe('stepApplicationLoop', () => {
968
849
  describe('stopApplicationLoop', () => {
969
850
  it('sets isRunning to false', () => {
970
851
  const backend = makeManualLoopBackend();
971
- setLoopBackend(backend);
852
+ const host = createLoopTestHost(backend);
972
853
  const app = createApplication();
973
- startApplicationLoop(app);
854
+ startApplicationLoop(host, app);
974
855
  stopApplicationLoop(app);
975
856
  expect(app.isRunning).toBe(false);
976
857
  });
977
858
 
978
859
  it('stops emitting after stop', () => {
979
860
  const backend = makeManualLoopBackend();
980
- setLoopBackend(backend);
861
+ const host = createLoopTestHost(backend);
981
862
  const app = createApplication();
982
863
  let renders = 0;
983
864
  connectSignal(app.onRender, () => renders++);
984
- startApplicationLoop(app);
865
+ startApplicationLoop(host, app);
985
866
  backend.tick(0);
986
867
  stopApplicationLoop(app);
987
868
  // After stop, calling tick should not emit even if the backend fires.