@flighthq/lifecycle 0.5.1-next.973.788d3d7 → 0.6.0
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/README.md +2 -2
- package/dist/.tsbuildinfo.contract +1 -0
- package/dist/.tsbuildinfo.runtime +1 -0
- package/dist/contract.d.ts +2 -2
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +1 -2
- package/dist/contract.js.map +1 -1
- package/dist/contract.lifecycle-types.d.ts +2 -0
- package/dist/contract.lifecycle-types.d.ts.map +1 -0
- package/dist/contract.lifecycle-types.js +2 -0
- package/dist/contract.lifecycle-types.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lifecycle.d.ts +11 -12
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +34 -143
- package/dist/lifecycle.js.map +1 -1
- package/dist/types.d.ts +30 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +7 -5
- package/src/lifecycle.test.ts +88 -147
- package/src/lifecycleHost.test.ts +24 -19
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC5F,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAEzD,UAAU,eAAgB,SAAQ,SAAS;IACzC,CAAC,gBAAgB,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC;CAC/C;AAED,MAAM,MAAM,iBAAiB,GAAG,QAAQ,GAAG,UAAU,GAAG,YAAY,CAAC;AAIrE,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,CAAC;AAI5C,MAAM,MAAM,iBAAiB,GAAG,QAAQ,GAAG,UAAU,GAAG,UAAU,CAAC;AAKnE,MAAM,WAAW,uBAAuB;IACtC,QAAQ,IAAI,iBAAiB,CAAC;IAE9B,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC;IAG5C,aAAa,CAAC,IAAI,aAAa,CAAC;IAGhC,sBAAsB,CAAC,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;CACnF;AAED,sFAAsF;AACtF,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,KAAK,CAAC,EAAE,uBAAuB,CAAC;CAC1C;AAID,MAAM,WAAW,YAAa,SAAQ,eAAe;IACnD,aAAa,EAAE,MAAM,CAAC,iBAAiB,CAAC,CAAC;IACzC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IAE3B,eAAe,EAAE,MAAM,CAAC,iBAAiB,CAAC,CAAC;IAE3C,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAE7C,cAAc,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;CAC3D;AAKD,MAAM,MAAM,kBAAkB,GAAG,OAAO,CAAC,MAAM,uBAAuB,EAAE,MAAM,eAAe,CAAC,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flighthq/lifecycle",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"author": "Joshua Granick and other contributors",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -32,18 +32,20 @@
|
|
|
32
32
|
"scripts": {
|
|
33
33
|
"build": "tsc -b",
|
|
34
34
|
"clean": "tsc -b --clean",
|
|
35
|
+
"generate": "tsx ../../scripts/generate-rust-package.ts --crate flight-lifecycle --package lifecycle",
|
|
36
|
+
"generate:check": "tsx ../../scripts/generate-rust-package.ts --crate flight-lifecycle --package lifecycle --check",
|
|
35
37
|
"test": "vitest run --config vitest.config.ts",
|
|
36
38
|
"test:watch": "vitest --watch --config vitest.config.ts",
|
|
37
39
|
"prepack": "npm run clean && npm run clean:dist && npm run build",
|
|
38
40
|
"clean:dist": "tsx ../../scripts/clean-package-dist.ts"
|
|
39
41
|
},
|
|
40
42
|
"dependencies": {
|
|
41
|
-
"@flighthq/entity": "0.
|
|
42
|
-
"@flighthq/
|
|
43
|
-
"@flighthq/
|
|
43
|
+
"@flighthq/entity": "0.6.0",
|
|
44
|
+
"@flighthq/host": "0.6.0",
|
|
45
|
+
"@flighthq/signals": "0.6.0"
|
|
44
46
|
},
|
|
45
47
|
"devDependencies": {
|
|
46
|
-
"typescript": "^
|
|
48
|
+
"typescript": "^7.0.2"
|
|
47
49
|
},
|
|
48
50
|
"description": "Application foreground/background lifecycle state and resume/pause/back signals over a swappable web/native backend",
|
|
49
51
|
"sideEffects": false
|
package/src/lifecycle.test.ts
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
import { allocateEntity
|
|
2
|
-
import { cancelSignal, connectSignal } from '@flighthq/signals/contract';
|
|
1
|
+
import { allocateEntity } from '@flighthq/entity/contract';
|
|
2
|
+
import { cancelSignal, connectSignal, disconnectSignal } from '@flighthq/signals/contract';
|
|
3
|
+
import type { Signal, SlotId } from '@flighthq/signals/contract';
|
|
4
|
+
|
|
3
5
|
import type {
|
|
4
|
-
HasSystemLifecycle,
|
|
5
6
|
AppLaunchKind,
|
|
6
7
|
AppLifecycleState,
|
|
7
8
|
AppMemoryPressure,
|
|
8
|
-
|
|
9
|
-
LifecycleBackend,
|
|
9
|
+
HostLifecycleCapability,
|
|
10
10
|
LifecycleOperation,
|
|
11
|
-
} from '
|
|
12
|
-
|
|
11
|
+
} from './contract.lifecycle-types.ts';
|
|
13
12
|
import {
|
|
14
13
|
attachAppLifecycle,
|
|
15
14
|
createAppLifecycle,
|
|
16
|
-
createWebLifecycleBackend,
|
|
17
15
|
detachAppLifecycle,
|
|
18
16
|
disposeAppLifecycle,
|
|
19
17
|
explainLifecycleOperation,
|
|
@@ -21,27 +19,28 @@ import {
|
|
|
21
19
|
getAppLifecycleState,
|
|
22
20
|
hasLifecycleOperation,
|
|
23
21
|
initializeAppLifecycle,
|
|
24
|
-
initializeWebLifecycleBackend,
|
|
25
22
|
isAppActive,
|
|
26
23
|
isAppBackground,
|
|
27
24
|
isAppInactive,
|
|
28
25
|
requestAppBack,
|
|
29
|
-
} from './lifecycle';
|
|
26
|
+
} from './lifecycle.ts';
|
|
30
27
|
|
|
31
|
-
type FakeBackend =
|
|
28
|
+
type FakeBackend = HostLifecycleCapability & {
|
|
32
29
|
state: AppLifecycleState;
|
|
33
30
|
fire: () => void;
|
|
34
31
|
fireMemory: (level: AppMemoryPressure) => void;
|
|
35
32
|
};
|
|
36
33
|
|
|
37
|
-
function hostOf(backend:
|
|
38
|
-
|
|
34
|
+
function hostOf(backend: HostLifecycleCapability): {
|
|
35
|
+
readonly system: { readonly lifecycle: HostLifecycleCapability };
|
|
36
|
+
} {
|
|
37
|
+
return { system: { lifecycle: backend } } as { readonly system: { readonly lifecycle: HostLifecycleCapability } };
|
|
39
38
|
}
|
|
40
39
|
|
|
41
40
|
function fakeBackend(): FakeBackend {
|
|
42
41
|
let stateListener: (() => void) | null = null;
|
|
43
42
|
let memoryListener: ((level: AppMemoryPressure) => void) | null = null;
|
|
44
|
-
const out =
|
|
43
|
+
const out = {} as FakeBackend;
|
|
45
44
|
out.state = 'active' as AppLifecycleState;
|
|
46
45
|
out.getState = () => {
|
|
47
46
|
return out.state;
|
|
@@ -64,10 +63,27 @@ function fakeBackend(): FakeBackend {
|
|
|
64
63
|
out.fireMemory = (level: AppMemoryPressure) => {
|
|
65
64
|
memoryListener?.(level);
|
|
66
65
|
};
|
|
67
|
-
return
|
|
66
|
+
return out;
|
|
68
67
|
}
|
|
69
68
|
|
|
70
69
|
describe('attachAppLifecycle', () => {
|
|
70
|
+
it('stops a pause slot disconnected by its SlotId', () => {
|
|
71
|
+
const backend = fakeBackend();
|
|
72
|
+
const host = hostOf(backend);
|
|
73
|
+
const app = createAppLifecycle();
|
|
74
|
+
let pauses = 0;
|
|
75
|
+
const id: SlotId = connectSignal(app.onPause, () => pauses++);
|
|
76
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
77
|
+
backend.state = 'background';
|
|
78
|
+
backend.fire();
|
|
79
|
+
disconnectSignal(app.onPause, id);
|
|
80
|
+
backend.state = 'active';
|
|
81
|
+
backend.fire();
|
|
82
|
+
backend.state = 'background';
|
|
83
|
+
backend.fire();
|
|
84
|
+
expect(pauses).toBe(1);
|
|
85
|
+
});
|
|
86
|
+
|
|
71
87
|
it('emits onPause and onStateChange when leaving active for background', () => {
|
|
72
88
|
const backend = fakeBackend();
|
|
73
89
|
const host = hostOf(backend);
|
|
@@ -76,7 +92,7 @@ describe('attachAppLifecycle', () => {
|
|
|
76
92
|
let pauses = 0;
|
|
77
93
|
connectSignal(app.onStateChange, () => changes++);
|
|
78
94
|
connectSignal(app.onPause, () => pauses++);
|
|
79
|
-
attachAppLifecycle(host, app);
|
|
95
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
80
96
|
backend.state = 'background';
|
|
81
97
|
backend.fire();
|
|
82
98
|
expect(changes).toBe(1);
|
|
@@ -89,7 +105,7 @@ describe('attachAppLifecycle', () => {
|
|
|
89
105
|
const app = createAppLifecycle();
|
|
90
106
|
let pauses = 0;
|
|
91
107
|
connectSignal(app.onPause, () => pauses++);
|
|
92
|
-
attachAppLifecycle(host, app);
|
|
108
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
93
109
|
backend.state = 'inactive';
|
|
94
110
|
backend.fire();
|
|
95
111
|
expect(pauses).toBe(1);
|
|
@@ -102,7 +118,7 @@ describe('attachAppLifecycle', () => {
|
|
|
102
118
|
const app = createAppLifecycle();
|
|
103
119
|
let pauses = 0;
|
|
104
120
|
connectSignal(app.onPause, () => pauses++);
|
|
105
|
-
attachAppLifecycle(host, app);
|
|
121
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
106
122
|
backend.state = 'background';
|
|
107
123
|
backend.fire();
|
|
108
124
|
// inactive → background should not re-fire onPause (already paused)
|
|
@@ -116,7 +132,7 @@ describe('attachAppLifecycle', () => {
|
|
|
116
132
|
const app = createAppLifecycle();
|
|
117
133
|
let resumes = 0;
|
|
118
134
|
connectSignal(app.onResume, () => resumes++);
|
|
119
|
-
attachAppLifecycle(host, app);
|
|
135
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
120
136
|
backend.state = 'active';
|
|
121
137
|
backend.fire();
|
|
122
138
|
expect(resumes).toBe(1);
|
|
@@ -129,7 +145,7 @@ describe('attachAppLifecycle', () => {
|
|
|
129
145
|
const app = createAppLifecycle();
|
|
130
146
|
let resumes = 0;
|
|
131
147
|
connectSignal(app.onResume, () => resumes++);
|
|
132
|
-
attachAppLifecycle(host, app);
|
|
148
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
133
149
|
backend.state = 'active';
|
|
134
150
|
backend.fire();
|
|
135
151
|
expect(resumes).toBe(1);
|
|
@@ -141,8 +157,8 @@ describe('attachAppLifecycle', () => {
|
|
|
141
157
|
const app = createAppLifecycle();
|
|
142
158
|
let changes = 0;
|
|
143
159
|
connectSignal(app.onStateChange, () => changes++);
|
|
144
|
-
attachAppLifecycle(host, app);
|
|
145
|
-
attachAppLifecycle(host, app);
|
|
160
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
161
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
146
162
|
backend.state = 'background';
|
|
147
163
|
backend.fire();
|
|
148
164
|
// Only one subscription should be active.
|
|
@@ -158,7 +174,7 @@ describe('attachAppLifecycle', () => {
|
|
|
158
174
|
bag['key'] = 'value';
|
|
159
175
|
saved = true;
|
|
160
176
|
});
|
|
161
|
-
attachAppLifecycle(host, app);
|
|
177
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
162
178
|
backend.state = 'background';
|
|
163
179
|
backend.fire();
|
|
164
180
|
expect(saved).toBe(true);
|
|
@@ -175,7 +191,7 @@ describe('attachAppLifecycle', () => {
|
|
|
175
191
|
connectSignal(app.onRestoreState, (state) => {
|
|
176
192
|
restored = state;
|
|
177
193
|
});
|
|
178
|
-
attachAppLifecycle(host, app);
|
|
194
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
179
195
|
// Transition away to trigger save.
|
|
180
196
|
backend.state = 'background';
|
|
181
197
|
backend.fire();
|
|
@@ -192,7 +208,7 @@ describe('attachAppLifecycle', () => {
|
|
|
192
208
|
const app = createAppLifecycle();
|
|
193
209
|
const levels: AppMemoryPressure[] = [];
|
|
194
210
|
connectSignal(app.onMemoryWarning, (level) => levels.push(level));
|
|
195
|
-
attachAppLifecycle(host, app);
|
|
211
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
196
212
|
backend.fireMemory('critical');
|
|
197
213
|
expect(levels).toEqual(['critical']);
|
|
198
214
|
});
|
|
@@ -210,7 +226,7 @@ describe('attachAppLifecycle', () => {
|
|
|
210
226
|
const app = createAppLifecycle();
|
|
211
227
|
let changes = 0;
|
|
212
228
|
connectSignal(app.onStateChange, () => changes++);
|
|
213
|
-
attachAppLifecycle(host, app);
|
|
229
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
214
230
|
const fireCount = 1 + Math.floor(Math.random() * 30);
|
|
215
231
|
for (let i = 0; i < fireCount; i++) {
|
|
216
232
|
backend.state = states[Math.floor(Math.random() * states.length)];
|
|
@@ -234,7 +250,7 @@ describe('attachAppLifecycle', () => {
|
|
|
234
250
|
let resumes = 0;
|
|
235
251
|
connectSignal(app.onPause, () => pauses++);
|
|
236
252
|
connectSignal(app.onResume, () => resumes++);
|
|
237
|
-
attachAppLifecycle(host, app);
|
|
253
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
238
254
|
|
|
239
255
|
// Drive a random storm while computing the expected edge counts from an independent model:
|
|
240
256
|
// a pause edge is every 'active' → non-'active' transition, a resume edge every
|
|
@@ -272,7 +288,7 @@ describe('attachAppLifecycle', () => {
|
|
|
272
288
|
connectSignal(app.onStateChange, () => changes++);
|
|
273
289
|
connectSignal(app.onPause, () => pauses++);
|
|
274
290
|
connectSignal(app.onResume, () => resumes++);
|
|
275
|
-
attachAppLifecycle(host, app);
|
|
291
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
276
292
|
const fireCount = 1 + Math.floor(Math.random() * 30);
|
|
277
293
|
for (let i = 0; i < fireCount; i++) backend.fire();
|
|
278
294
|
expect(changes).toBe(fireCount);
|
|
@@ -297,7 +313,7 @@ describe('attachAppLifecycle', () => {
|
|
|
297
313
|
let resumes = 0;
|
|
298
314
|
connectSignal(app.onPause, () => pauses++);
|
|
299
315
|
connectSignal(app.onResume, () => resumes++);
|
|
300
|
-
attachAppLifecycle(host, app);
|
|
316
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
301
317
|
const fireCount = 1 + Math.floor(Math.random() * 30);
|
|
302
318
|
for (let i = 0; i < fireCount; i++) {
|
|
303
319
|
backend.state = states[Math.floor(Math.random() * states.length)];
|
|
@@ -310,6 +326,17 @@ describe('attachAppLifecycle', () => {
|
|
|
310
326
|
});
|
|
311
327
|
|
|
312
328
|
describe('createAppLifecycle', () => {
|
|
329
|
+
it('types the lifecycle signals by their payload, void where there is none', () => {
|
|
330
|
+
const app = createAppLifecycle();
|
|
331
|
+
expectTypeOf(app.onStateChange).toEqualTypeOf<Signal<AppLifecycleState>>();
|
|
332
|
+
expectTypeOf(app.onResume).toEqualTypeOf<Signal<void>>();
|
|
333
|
+
expectTypeOf(app.onPause).toEqualTypeOf<Signal<void>>();
|
|
334
|
+
expectTypeOf(app.onBackButton).toEqualTypeOf<Signal<void>>();
|
|
335
|
+
expectTypeOf(app.onMemoryWarning).toEqualTypeOf<Signal<AppMemoryPressure>>();
|
|
336
|
+
expectTypeOf(app.onSaveState).toEqualTypeOf<Signal<Record<string, unknown>>>();
|
|
337
|
+
expectTypeOf(app.onRestoreState).toEqualTypeOf<Signal<Readonly<Record<string, unknown>>>>();
|
|
338
|
+
});
|
|
339
|
+
|
|
313
340
|
it('creates an entity with all seven signals', () => {
|
|
314
341
|
const app = createAppLifecycle();
|
|
315
342
|
expect(app.onStateChange).toBeDefined();
|
|
@@ -322,88 +349,6 @@ describe('createAppLifecycle', () => {
|
|
|
322
349
|
});
|
|
323
350
|
});
|
|
324
351
|
|
|
325
|
-
describe('createWebLifecycleBackend', () => {
|
|
326
|
-
it("getLaunchKind returns 'cold' when no performance navigation entries", () => {
|
|
327
|
-
vi.spyOn(performance, 'getEntriesByType').mockReturnValue([]);
|
|
328
|
-
const backend = createWebLifecycleBackend();
|
|
329
|
-
expect(backend.getLaunchKind?.()).toBe('cold');
|
|
330
|
-
vi.restoreAllMocks();
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
it("getLaunchKind returns 'warm' for back_forward navigation type", () => {
|
|
334
|
-
vi.spyOn(performance, 'getEntriesByType').mockReturnValue([
|
|
335
|
-
{ type: 'back_forward' } as PerformanceNavigationTiming,
|
|
336
|
-
]);
|
|
337
|
-
const backend = createWebLifecycleBackend();
|
|
338
|
-
expect(backend.getLaunchKind?.()).toBe('warm');
|
|
339
|
-
vi.restoreAllMocks();
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
it("getLaunchKind returns 'cold' for reload navigation type", () => {
|
|
343
|
-
vi.spyOn(performance, 'getEntriesByType').mockReturnValue([{ type: 'reload' } as PerformanceNavigationTiming]);
|
|
344
|
-
const backend = createWebLifecycleBackend();
|
|
345
|
-
expect(backend.getLaunchKind?.()).toBe('cold');
|
|
346
|
-
vi.restoreAllMocks();
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
it("getLaunchKind returns 'cold' for navigate navigation type", () => {
|
|
350
|
-
vi.spyOn(performance, 'getEntriesByType').mockReturnValue([{ type: 'navigate' } as PerformanceNavigationTiming]);
|
|
351
|
-
const backend = createWebLifecycleBackend();
|
|
352
|
-
expect(backend.getLaunchKind?.()).toBe('cold');
|
|
353
|
-
vi.restoreAllMocks();
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
it('reads a state without throwing', () => {
|
|
357
|
-
expect(typeof createWebLifecycleBackend().getState()).toBe('string');
|
|
358
|
-
});
|
|
359
|
-
|
|
360
|
-
it('subscribeMemoryWarning fires critical when memory-pressure event fires', () => {
|
|
361
|
-
const backend = createWebLifecycleBackend();
|
|
362
|
-
const levels: AppMemoryPressure[] = [];
|
|
363
|
-
const unsubscribe = backend.subscribeMemoryWarning?.((level) => levels.push(level));
|
|
364
|
-
// Simulate a memory-pressure event with 'critical' pressure detail.
|
|
365
|
-
const event = new CustomEvent('memory-pressure', { detail: { pressure: 'critical' } });
|
|
366
|
-
window.dispatchEvent(event);
|
|
367
|
-
expect(levels).toEqual(['critical']);
|
|
368
|
-
unsubscribe?.();
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
it('subscribeMemoryWarning fires moderate when memory-pressure event fires with moderate pressure', () => {
|
|
372
|
-
const backend = createWebLifecycleBackend();
|
|
373
|
-
const levels: AppMemoryPressure[] = [];
|
|
374
|
-
const unsubscribe = backend.subscribeMemoryWarning?.((level) => levels.push(level));
|
|
375
|
-
const event = new CustomEvent('memory-pressure', { detail: { pressure: 'moderate' } });
|
|
376
|
-
window.dispatchEvent(event);
|
|
377
|
-
expect(levels).toEqual(['moderate']);
|
|
378
|
-
unsubscribe?.();
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
it('subscribeMemoryWarning fires normal when memory-pressure-relieved event fires', () => {
|
|
382
|
-
const backend = createWebLifecycleBackend();
|
|
383
|
-
const levels: AppMemoryPressure[] = [];
|
|
384
|
-
const unsubscribe = backend.subscribeMemoryWarning?.((level) => levels.push(level));
|
|
385
|
-
const event = new CustomEvent('memory-pressure-relieved');
|
|
386
|
-
window.dispatchEvent(event);
|
|
387
|
-
expect(levels).toEqual(['normal']);
|
|
388
|
-
unsubscribe?.();
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
it('subscribeMemoryWarning stops delivering after unsubscribe', () => {
|
|
392
|
-
const backend = createWebLifecycleBackend();
|
|
393
|
-
const levels: AppMemoryPressure[] = [];
|
|
394
|
-
const unsubscribe = backend.subscribeMemoryWarning?.((level) => levels.push(level));
|
|
395
|
-
unsubscribe?.();
|
|
396
|
-
const event = new CustomEvent('memory-pressure', { detail: { pressure: 'critical' } });
|
|
397
|
-
window.dispatchEvent(event);
|
|
398
|
-
expect(levels).toHaveLength(0);
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
it('subscribes and unsubscribes without throwing', () => {
|
|
402
|
-
const unsubscribe = createWebLifecycleBackend().subscribe(() => {});
|
|
403
|
-
expect(() => unsubscribe()).not.toThrow();
|
|
404
|
-
});
|
|
405
|
-
});
|
|
406
|
-
|
|
407
352
|
describe('detachAppLifecycle', () => {
|
|
408
353
|
it('stops further delivery', () => {
|
|
409
354
|
const backend = fakeBackend();
|
|
@@ -411,7 +356,7 @@ describe('detachAppLifecycle', () => {
|
|
|
411
356
|
const app = createAppLifecycle();
|
|
412
357
|
let changes = 0;
|
|
413
358
|
connectSignal(app.onStateChange, () => changes++);
|
|
414
|
-
attachAppLifecycle(host, app);
|
|
359
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
415
360
|
detachAppLifecycle(app);
|
|
416
361
|
backend.fire();
|
|
417
362
|
expect(changes).toBe(0);
|
|
@@ -428,7 +373,7 @@ describe('disposeAppLifecycle', () => {
|
|
|
428
373
|
const backend = fakeBackend();
|
|
429
374
|
const host = hostOf(backend);
|
|
430
375
|
const app = createAppLifecycle();
|
|
431
|
-
attachAppLifecycle(host, app);
|
|
376
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
432
377
|
expect(() => disposeAppLifecycle(app)).not.toThrow();
|
|
433
378
|
});
|
|
434
379
|
|
|
@@ -438,7 +383,7 @@ describe('disposeAppLifecycle', () => {
|
|
|
438
383
|
const app = createAppLifecycle();
|
|
439
384
|
let changes = 0;
|
|
440
385
|
connectSignal(app.onStateChange, () => changes++);
|
|
441
|
-
attachAppLifecycle(host, app);
|
|
386
|
+
attachAppLifecycle(host.system.lifecycle, app);
|
|
442
387
|
disposeAppLifecycle(app);
|
|
443
388
|
backend.fire();
|
|
444
389
|
expect(changes).toBe(0);
|
|
@@ -451,7 +396,7 @@ describe('explainLifecycleOperation', () => {
|
|
|
451
396
|
// about a process-wide fallback that answered everything.
|
|
452
397
|
it('reports the host provider as implementing what it provides', () => {
|
|
453
398
|
const host = hostOf(fakeBackend());
|
|
454
|
-
expect(explainLifecycleOperation(host, 'getState')).toEqual({
|
|
399
|
+
expect(explainLifecycleOperation(host.system.lifecycle, 'getState')).toEqual({
|
|
455
400
|
implemented: true,
|
|
456
401
|
layer: 'host',
|
|
457
402
|
operation: 'getState',
|
|
@@ -461,13 +406,13 @@ describe('explainLifecycleOperation', () => {
|
|
|
461
406
|
it('reports an operation the host provider omits as unimplemented', () => {
|
|
462
407
|
const host = hostOf(
|
|
463
408
|
(() => {
|
|
464
|
-
const out =
|
|
409
|
+
const out = {} as HostLifecycleCapability;
|
|
465
410
|
out.getState = () => 'active' as const;
|
|
466
411
|
out.subscribe = () => () => {};
|
|
467
|
-
return
|
|
412
|
+
return out;
|
|
468
413
|
})(),
|
|
469
414
|
);
|
|
470
|
-
expect(explainLifecycleOperation(host, 'getLaunchKind')).toEqual({
|
|
415
|
+
expect(explainLifecycleOperation(host.system.lifecycle, 'getLaunchKind')).toEqual({
|
|
471
416
|
implemented: false,
|
|
472
417
|
layer: 'sentinel',
|
|
473
418
|
operation: 'getLaunchKind',
|
|
@@ -480,14 +425,14 @@ describe('explainLifecycleOperation', () => {
|
|
|
480
425
|
const rich = hostOf(fakeBackend());
|
|
481
426
|
const bare = hostOf(
|
|
482
427
|
(() => {
|
|
483
|
-
const out =
|
|
428
|
+
const out = {} as HostLifecycleCapability;
|
|
484
429
|
out.getState = () => 'active' as const;
|
|
485
430
|
out.subscribe = () => () => {};
|
|
486
|
-
return
|
|
431
|
+
return out;
|
|
487
432
|
})(),
|
|
488
433
|
);
|
|
489
|
-
expect(explainLifecycleOperation(rich, 'subscribeMemoryWarning').implemented).toBe(true);
|
|
490
|
-
expect(explainLifecycleOperation(bare, 'subscribeMemoryWarning').implemented).toBe(false);
|
|
434
|
+
expect(explainLifecycleOperation(rich.system.lifecycle, 'subscribeMemoryWarning').implemented).toBe(true);
|
|
435
|
+
expect(explainLifecycleOperation(bare.system.lifecycle, 'subscribeMemoryWarning').implemented).toBe(false);
|
|
491
436
|
});
|
|
492
437
|
});
|
|
493
438
|
|
|
@@ -495,17 +440,17 @@ describe('getAppLaunchKind', () => {
|
|
|
495
440
|
it("returns 'warm' when backend does not implement getLaunchKind", () => {
|
|
496
441
|
const backend = fakeBackend();
|
|
497
442
|
const host = hostOf(backend);
|
|
498
|
-
expect(getAppLaunchKind(host)).toBe('warm');
|
|
443
|
+
expect(getAppLaunchKind(host.system.lifecycle)).toBe('warm');
|
|
499
444
|
});
|
|
500
445
|
|
|
501
446
|
it('delegates to backend.getLaunchKind when present', () => {
|
|
502
|
-
const out =
|
|
447
|
+
const out = {} as HostLifecycleCapability;
|
|
503
448
|
out.getState = () => 'active' as AppLifecycleState;
|
|
504
449
|
out.subscribe = () => () => {};
|
|
505
450
|
out.getLaunchKind = () => 'cold' as AppLaunchKind;
|
|
506
|
-
const backend =
|
|
451
|
+
const backend = out;
|
|
507
452
|
const host = hostOf(backend);
|
|
508
|
-
expect(getAppLaunchKind(host)).toBe('cold');
|
|
453
|
+
expect(getAppLaunchKind(host.system.lifecycle)).toBe('cold');
|
|
509
454
|
});
|
|
510
455
|
});
|
|
511
456
|
|
|
@@ -514,26 +459,28 @@ describe('getAppLifecycleState', () => {
|
|
|
514
459
|
const backend = fakeBackend();
|
|
515
460
|
backend.state = 'inactive';
|
|
516
461
|
const host = hostOf(backend);
|
|
517
|
-
expect(getAppLifecycleState(host)).toBe('inactive');
|
|
462
|
+
expect(getAppLifecycleState(host.system.lifecycle)).toBe('inactive');
|
|
518
463
|
});
|
|
519
464
|
});
|
|
520
465
|
|
|
521
466
|
describe('hasLifecycleOperation', () => {
|
|
522
467
|
it('agrees with explainLifecycleOperation for the same host', () => {
|
|
523
468
|
const host = hostOf(fakeBackend());
|
|
524
|
-
expect(hasLifecycleOperation(host, 'getState')).toBe(
|
|
469
|
+
expect(hasLifecycleOperation(host.system.lifecycle, 'getState')).toBe(
|
|
470
|
+
explainLifecycleOperation(host.system.lifecycle, 'getState').implemented,
|
|
471
|
+
);
|
|
525
472
|
});
|
|
526
473
|
|
|
527
474
|
it('is false for an operation the host provider omits', () => {
|
|
528
475
|
const host = hostOf(
|
|
529
476
|
(() => {
|
|
530
|
-
const out =
|
|
477
|
+
const out = {} as HostLifecycleCapability;
|
|
531
478
|
out.getState = () => 'active' as const;
|
|
532
479
|
out.subscribe = () => () => {};
|
|
533
|
-
return
|
|
480
|
+
return out;
|
|
534
481
|
})(),
|
|
535
482
|
);
|
|
536
|
-
expect(hasLifecycleOperation(host, 'getLaunchKind')).toBe(false);
|
|
483
|
+
expect(hasLifecycleOperation(host.system.lifecycle, 'getLaunchKind')).toBe(false);
|
|
537
484
|
});
|
|
538
485
|
});
|
|
539
486
|
|
|
@@ -543,23 +490,17 @@ describe('initializeAppLifecycle', () => {
|
|
|
543
490
|
});
|
|
544
491
|
});
|
|
545
492
|
|
|
546
|
-
describe('initializeWebLifecycleBackend', () => {
|
|
547
|
-
it('is the construction initializer of createWebLifecycleBackend', () => {
|
|
548
|
-
expect(typeof initializeWebLifecycleBackend).toBe('function');
|
|
549
|
-
});
|
|
550
|
-
});
|
|
551
|
-
|
|
552
493
|
// Per-operation availability for LifecycleBackend. The operations below are the ones the interface declares
|
|
553
494
|
// OPTIONAL, so a host that omits them is compliant rather than broken — that is the absence-of-an-export
|
|
554
495
|
// ruling, and this is the query that makes it observable.
|
|
555
496
|
const OPTIONAL_OPERATIONS: readonly LifecycleOperation[] = ['getLaunchKind', 'subscribeMemoryWarning'];
|
|
556
497
|
|
|
557
498
|
// A host implementing only the REQUIRED members — partial support declared by absence.
|
|
558
|
-
function partialBackend():
|
|
559
|
-
const out =
|
|
499
|
+
function partialBackend(): HostLifecycleCapability {
|
|
500
|
+
const out = {} as HostLifecycleCapability;
|
|
560
501
|
out.getState = (() => undefined) as never;
|
|
561
502
|
out.subscribe = (() => undefined) as never;
|
|
562
|
-
return
|
|
503
|
+
return out;
|
|
563
504
|
}
|
|
564
505
|
|
|
565
506
|
describe('isAppActive', () => {
|
|
@@ -567,21 +508,21 @@ describe('isAppActive', () => {
|
|
|
567
508
|
const backend = fakeBackend();
|
|
568
509
|
backend.state = 'active';
|
|
569
510
|
const host = hostOf(backend);
|
|
570
|
-
expect(isAppActive(host)).toBe(true);
|
|
511
|
+
expect(isAppActive(host.system.lifecycle)).toBe(true);
|
|
571
512
|
});
|
|
572
513
|
|
|
573
514
|
it("returns false when state is 'background'", () => {
|
|
574
515
|
const backend = fakeBackend();
|
|
575
516
|
backend.state = 'background';
|
|
576
517
|
const host = hostOf(backend);
|
|
577
|
-
expect(isAppActive(host)).toBe(false);
|
|
518
|
+
expect(isAppActive(host.system.lifecycle)).toBe(false);
|
|
578
519
|
});
|
|
579
520
|
|
|
580
521
|
it("returns false when state is 'inactive'", () => {
|
|
581
522
|
const backend = fakeBackend();
|
|
582
523
|
backend.state = 'inactive';
|
|
583
524
|
const host = hostOf(backend);
|
|
584
|
-
expect(isAppActive(host)).toBe(false);
|
|
525
|
+
expect(isAppActive(host.system.lifecycle)).toBe(false);
|
|
585
526
|
});
|
|
586
527
|
});
|
|
587
528
|
|
|
@@ -590,14 +531,14 @@ describe('isAppBackground', () => {
|
|
|
590
531
|
const backend = fakeBackend();
|
|
591
532
|
backend.state = 'background';
|
|
592
533
|
const host = hostOf(backend);
|
|
593
|
-
expect(isAppBackground(host)).toBe(true);
|
|
534
|
+
expect(isAppBackground(host.system.lifecycle)).toBe(true);
|
|
594
535
|
});
|
|
595
536
|
|
|
596
537
|
it("returns false when state is 'active'", () => {
|
|
597
538
|
const backend = fakeBackend();
|
|
598
539
|
backend.state = 'active';
|
|
599
540
|
const host = hostOf(backend);
|
|
600
|
-
expect(isAppBackground(host)).toBe(false);
|
|
541
|
+
expect(isAppBackground(host.system.lifecycle)).toBe(false);
|
|
601
542
|
});
|
|
602
543
|
});
|
|
603
544
|
describe('isAppInactive', () => {
|
|
@@ -605,14 +546,14 @@ describe('isAppInactive', () => {
|
|
|
605
546
|
const backend = fakeBackend();
|
|
606
547
|
backend.state = 'inactive';
|
|
607
548
|
const host = hostOf(backend);
|
|
608
|
-
expect(isAppInactive(host)).toBe(true);
|
|
549
|
+
expect(isAppInactive(host.system.lifecycle)).toBe(true);
|
|
609
550
|
});
|
|
610
551
|
|
|
611
552
|
it("returns false when state is 'active'", () => {
|
|
612
553
|
const backend = fakeBackend();
|
|
613
554
|
backend.state = 'active';
|
|
614
555
|
const host = hostOf(backend);
|
|
615
|
-
expect(isAppInactive(host)).toBe(false);
|
|
556
|
+
expect(isAppInactive(host.system.lifecycle)).toBe(false);
|
|
616
557
|
});
|
|
617
558
|
});
|
|
618
559
|
|
|
@@ -1,57 +1,62 @@
|
|
|
1
|
-
import type { AppLifecycleState,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import type { AppLifecycleState, HostLifecycleCapability } from './contract.lifecycle-types.ts';
|
|
2
|
+
import { getAppLaunchKind, getAppLifecycleState, isAppActive, isAppBackground, isAppInactive } from './lifecycle.ts';
|
|
3
|
+
|
|
4
|
+
function hostWith(backend: Partial<HostLifecycleCapability>): {
|
|
5
|
+
readonly system: { readonly lifecycle: HostLifecycleCapability };
|
|
6
|
+
} {
|
|
7
|
+
return { system: { lifecycle: backend as HostLifecycleCapability } } as {
|
|
8
|
+
readonly system: { readonly lifecycle: HostLifecycleCapability };
|
|
9
|
+
};
|
|
7
10
|
}
|
|
8
11
|
|
|
9
|
-
function stateHost(state: AppLifecycleState):
|
|
12
|
+
function stateHost(state: AppLifecycleState): { readonly system: { readonly lifecycle: HostLifecycleCapability } } {
|
|
10
13
|
return hostWith({ getState: () => state, subscribe: () => () => {} });
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
describe('getAppLaunchKind', () => {
|
|
14
17
|
it('reads the launch kind from the host it is given', () => {
|
|
15
|
-
expect(getAppLaunchKind(hostWith({ getState: () => 'active', getLaunchKind: () => 'warm' }))).toBe(
|
|
18
|
+
expect(getAppLaunchKind(hostWith({ getState: () => 'active', getLaunchKind: () => 'warm' }).system.lifecycle)).toBe(
|
|
19
|
+
'warm',
|
|
20
|
+
);
|
|
16
21
|
});
|
|
17
22
|
|
|
18
23
|
// getLaunchKind is optional on the backend; a provider without it must not throw.
|
|
19
24
|
it('falls back to warm when the provider does not implement it', () => {
|
|
20
|
-
expect(getAppLaunchKind(stateHost('active'))).toBe('warm');
|
|
25
|
+
expect(getAppLaunchKind(stateHost('active').system.lifecycle)).toBe('warm');
|
|
21
26
|
});
|
|
22
27
|
});
|
|
23
28
|
|
|
24
29
|
describe('getAppLifecycleState', () => {
|
|
25
30
|
it('reads the state from the host it is given', () => {
|
|
26
|
-
expect(getAppLifecycleState(stateHost('background'))).toBe('background');
|
|
31
|
+
expect(getAppLifecycleState(stateHost('background').system.lifecycle)).toBe('background');
|
|
27
32
|
});
|
|
28
33
|
|
|
29
34
|
// Two hosts, two answers, no ambient selection in between. This is the property the migration is
|
|
30
35
|
// for: before it, both calls resolved one process-wide backend and the second host was unreachable.
|
|
31
36
|
it('keeps two hosts independent', () => {
|
|
32
|
-
expect(getAppLifecycleState(stateHost('active'))).toBe('active');
|
|
33
|
-
expect(getAppLifecycleState(stateHost('inactive'))).toBe('inactive');
|
|
37
|
+
expect(getAppLifecycleState(stateHost('active').system.lifecycle)).toBe('active');
|
|
38
|
+
expect(getAppLifecycleState(stateHost('inactive').system.lifecycle)).toBe('inactive');
|
|
34
39
|
});
|
|
35
40
|
});
|
|
36
41
|
|
|
37
42
|
describe('isAppActive', () => {
|
|
38
43
|
it('reports active only for the active state', () => {
|
|
39
|
-
expect(isAppActive(stateHost('active'))).toBe(true);
|
|
40
|
-
expect(isAppActive(stateHost('inactive'))).toBe(false);
|
|
41
|
-
expect(isAppActive(stateHost('background'))).toBe(false);
|
|
44
|
+
expect(isAppActive(stateHost('active').system.lifecycle)).toBe(true);
|
|
45
|
+
expect(isAppActive(stateHost('inactive').system.lifecycle)).toBe(false);
|
|
46
|
+
expect(isAppActive(stateHost('background').system.lifecycle)).toBe(false);
|
|
42
47
|
});
|
|
43
48
|
});
|
|
44
49
|
|
|
45
50
|
describe('isAppBackground', () => {
|
|
46
51
|
it('reports background only for the background state', () => {
|
|
47
|
-
expect(isAppBackground(stateHost('background'))).toBe(true);
|
|
48
|
-
expect(isAppBackground(stateHost('active'))).toBe(false);
|
|
52
|
+
expect(isAppBackground(stateHost('background').system.lifecycle)).toBe(true);
|
|
53
|
+
expect(isAppBackground(stateHost('active').system.lifecycle)).toBe(false);
|
|
49
54
|
});
|
|
50
55
|
});
|
|
51
56
|
|
|
52
57
|
describe('isAppInactive', () => {
|
|
53
58
|
it('reports inactive only for the inactive state', () => {
|
|
54
|
-
expect(isAppInactive(stateHost('inactive'))).toBe(true);
|
|
55
|
-
expect(isAppInactive(stateHost('active'))).toBe(false);
|
|
59
|
+
expect(isAppInactive(stateHost('inactive').system.lifecycle)).toBe(true);
|
|
60
|
+
expect(isAppInactive(stateHost('active').system.lifecycle)).toBe(false);
|
|
56
61
|
});
|
|
57
62
|
});
|