@api-client/ui 0.5.30 → 0.5.31
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/build/src/core/ActivityManager.d.ts +112 -4
- package/build/src/core/ActivityManager.d.ts.map +1 -1
- package/build/src/core/ActivityManager.js +251 -62
- package/build/src/core/ActivityManager.js.map +1 -1
- package/package.json +1 -1
- package/src/core/ActivityManager.ts +334 -61
- package/test/core/activity_manager.spec.ts +4 -4
|
@@ -54,9 +54,9 @@ export declare enum IntentFlags {
|
|
|
54
54
|
*/
|
|
55
55
|
ByReference = 4
|
|
56
56
|
}
|
|
57
|
-
export interface Intent {
|
|
57
|
+
export interface Intent<T = unknown> {
|
|
58
58
|
action: string;
|
|
59
|
-
data?:
|
|
59
|
+
data?: T;
|
|
60
60
|
category?: string[];
|
|
61
61
|
flags?: number;
|
|
62
62
|
/**
|
|
@@ -68,6 +68,22 @@ export declare enum IntentResult {
|
|
|
68
68
|
RESULT_CANCELED = 0,
|
|
69
69
|
RESULT_OK = 1
|
|
70
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Type for activity constructor with static action property.
|
|
73
|
+
*/
|
|
74
|
+
export interface ActivityConstructor {
|
|
75
|
+
new (parent: Application): Activity;
|
|
76
|
+
action?: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Enhanced activity registration options.
|
|
80
|
+
*/
|
|
81
|
+
export interface ActivityRegistrationOptions {
|
|
82
|
+
/** Whether to override existing registrations */
|
|
83
|
+
override?: boolean;
|
|
84
|
+
/** Optional validation function for intents */
|
|
85
|
+
validateIntent?: (intent: Intent) => boolean;
|
|
86
|
+
}
|
|
71
87
|
/**
|
|
72
88
|
* Manages application activities.
|
|
73
89
|
*
|
|
@@ -97,7 +113,13 @@ export declare class ActivityManager {
|
|
|
97
113
|
private modalActivityStack;
|
|
98
114
|
private generateActivityId;
|
|
99
115
|
constructor(parent: Application);
|
|
100
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Registers an activity class with enhanced options.
|
|
118
|
+
* @param action The action name to register the activity for.
|
|
119
|
+
* @param activityClass The activity class constructor.
|
|
120
|
+
* @param options Optional registration configuration.
|
|
121
|
+
*/
|
|
122
|
+
registerActivity(action: string, activityClass: ActivityConstructor, options?: ActivityRegistrationOptions): void;
|
|
101
123
|
/**
|
|
102
124
|
* Creates an activity and pushes it into the stack, but it does not initialize any of the lifecycle methods.
|
|
103
125
|
* It is a way to put an activity at some place of the stack,
|
|
@@ -150,13 +172,99 @@ export declare class ActivityManager {
|
|
|
150
172
|
* @param activity The activity to bring to the foreground.
|
|
151
173
|
*/
|
|
152
174
|
private updateCurrentActivity;
|
|
175
|
+
/**
|
|
176
|
+
* Safely executes an async lifecycle method with error handling.
|
|
177
|
+
* @param lifecycleMethod The lifecycle method to execute.
|
|
178
|
+
* @param activity The activity instance.
|
|
179
|
+
* @param methodName The name of the method for error reporting.
|
|
180
|
+
* @returns Promise that resolves when the method completes or rejects with wrapped error.
|
|
181
|
+
*/
|
|
182
|
+
private safeExecuteLifecycleMethod;
|
|
153
183
|
/**
|
|
154
184
|
* Allows navigating back through the activity stack.
|
|
155
185
|
* Call `ActivityManager.navigateBack()` when the user performs a back action
|
|
156
186
|
* (e.g., clicks a back button).
|
|
187
|
+
* @returns Promise that resolves to true if navigation was successful, false if at root.
|
|
188
|
+
*/
|
|
189
|
+
navigateBack(): Promise<boolean>;
|
|
190
|
+
/**
|
|
191
|
+
* Navigates to a specific activity in the stack, clearing activities above it.
|
|
192
|
+
* @param action The action of the activity to navigate to.
|
|
193
|
+
* @returns Promise that resolves to true if navigation was successful.
|
|
194
|
+
*/
|
|
195
|
+
navigateToActivity(action: string): Promise<boolean>;
|
|
196
|
+
/**
|
|
197
|
+
* Clears the entire activity stack and starts a new root activity.
|
|
198
|
+
* @param intent The intent for the new root activity.
|
|
199
|
+
*/
|
|
200
|
+
clearStackAndStart(intent: Intent): Promise<void>;
|
|
201
|
+
/**
|
|
202
|
+
* Creates an intent with better type safety and validation.
|
|
203
|
+
* @param action The action name.
|
|
204
|
+
* @param data Optional intent data.
|
|
205
|
+
* @param options Optional intent configuration.
|
|
206
|
+
* @returns A properly formed intent.
|
|
157
207
|
*/
|
|
158
|
-
|
|
208
|
+
createIntent<T = unknown>(action: string, data?: T, options?: {
|
|
209
|
+
category?: string[];
|
|
210
|
+
flags?: number;
|
|
211
|
+
requestCode?: number;
|
|
212
|
+
}): Intent<T>;
|
|
159
213
|
createRequestCode(): number;
|
|
160
214
|
findActiveActivity(action: string): Activity | undefined;
|
|
215
|
+
/**
|
|
216
|
+
* Transitions an activity from Created to Resumed state.
|
|
217
|
+
* @param activity The activity to transition.
|
|
218
|
+
* @returns true if successful, false if activity was destroyed during transition.
|
|
219
|
+
*/
|
|
220
|
+
private transitionCreatedToResumed;
|
|
221
|
+
/**
|
|
222
|
+
* Transitions an activity from Started to Resumed state.
|
|
223
|
+
* @param activity The activity to transition.
|
|
224
|
+
* @returns true if successful, false if activity was destroyed during transition.
|
|
225
|
+
*/
|
|
226
|
+
private transitionStartedToResumed;
|
|
227
|
+
/**
|
|
228
|
+
* Transitions an activity from Initialized to Resumed state.
|
|
229
|
+
* @param activity The activity to transition.
|
|
230
|
+
* @param intent The intent used to create the activity.
|
|
231
|
+
* @returns true if successful, false if activity was destroyed during transition.
|
|
232
|
+
*/
|
|
233
|
+
private transitionInitializedToResumed;
|
|
234
|
+
/**
|
|
235
|
+
* Cleans up resources for destroyed activities to prevent memory leaks.
|
|
236
|
+
* @param activity The activity to clean up.
|
|
237
|
+
*/
|
|
238
|
+
private cleanupActivity;
|
|
239
|
+
/**
|
|
240
|
+
* Validates that an activity stack is in a consistent state.
|
|
241
|
+
* @param stack The activity stack to validate.
|
|
242
|
+
* @returns Array of validation errors, empty if valid.
|
|
243
|
+
*/
|
|
244
|
+
private validateStackConsistency;
|
|
245
|
+
/**
|
|
246
|
+
* Gets diagnostic information about the current state of the activity manager.
|
|
247
|
+
* Useful for debugging and monitoring.
|
|
248
|
+
* @returns Object containing current state information.
|
|
249
|
+
*/
|
|
250
|
+
getDiagnostics(): {
|
|
251
|
+
totalActivities: number;
|
|
252
|
+
modalActivities: number;
|
|
253
|
+
currentActivity?: string;
|
|
254
|
+
topActivity?: string;
|
|
255
|
+
stackValidation: string[];
|
|
256
|
+
modalStackValidation: string[];
|
|
257
|
+
};
|
|
258
|
+
/**
|
|
259
|
+
* Gets all registered activity actions.
|
|
260
|
+
* @returns Array of registered action names.
|
|
261
|
+
*/
|
|
262
|
+
getRegisteredActions(): string[];
|
|
263
|
+
/**
|
|
264
|
+
* Checks if an action is registered.
|
|
265
|
+
* @param action The action to check.
|
|
266
|
+
* @returns true if the action is registered, false otherwise.
|
|
267
|
+
*/
|
|
268
|
+
isActionRegistered(action: string): boolean;
|
|
161
269
|
}
|
|
162
270
|
//# sourceMappingURL=ActivityManager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ActivityManager.d.ts","sourceRoot":"","sources":["../../../src/core/ActivityManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAGnD,oBAAY,iBAAiB;IAC3B;;;OAGG;IACH,WAAW,IAAA;IACX;;;OAGG;IACH,OAAO,IAAA;IACP;;;OAGG;IACH,OAAO,IAAA;IACP;;;OAGG;IACH,OAAO,IAAA;IACP;;;OAGG;IACH,MAAM,IAAA;IACN;;;OAGG;IACH,OAAO,IAAA;IACP;;;OAGG;IACH,SAAS,IAAA;CACV;AAED,oBAAY,WAAW;IACrB;;OAEG;IACH,KAAK,IAAS;IACd;;OAEG;IACH,SAAS,IAAS;IAClB;;;;;OAKG;IACH,WAAW,IAAS;CACrB;AAED,MAAM,WAAW,MAAM;
|
|
1
|
+
{"version":3,"file":"ActivityManager.d.ts","sourceRoot":"","sources":["../../../src/core/ActivityManager.ts"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,CAAC,IAAI,CAAC;IAkBvD;;;;;;OAMG;IACH,YAAY,CAAC,CAAC,GAAG,OAAO,EACtB,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,CAAC,EACR,OAAO,GAAE;QACP,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;QACnB,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,WAAW,CAAC,EAAE,MAAM,CAAA;KAChB,GACL,MAAM,CAAC,CAAC,CAAC;IAYZ,iBAAiB,IAAI,MAAM;IAM3B,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,GAAG,SAAS;IAIxD;;;;OAIG;YACW,0BAA0B;IAgBxC;;;;OAIG;YACW,0BAA0B;IAUxC;;;;;OAKG;YACW,8BAA8B;IAU5C;;;OAGG;IACH,OAAO,CAAC,eAAe;IAevB;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAsBhC;;;;OAIG;IACH,cAAc,IAAI;QAChB,eAAe,EAAE,MAAM,CAAA;QACvB,eAAe,EAAE,MAAM,CAAA;QACvB,eAAe,CAAC,EAAE,MAAM,CAAA;QACxB,WAAW,CAAC,EAAE,MAAM,CAAA;QACpB,eAAe,EAAE,MAAM,EAAE,CAAA;QACzB,oBAAoB,EAAE,MAAM,EAAE,CAAA;KAC/B;IAWD;;;OAGG;IACH,oBAAoB,IAAI,MAAM,EAAE;IAIhC;;;;OAIG;IACH,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;CAG5C"}
|
|
@@ -94,10 +94,22 @@ export class ActivityManager {
|
|
|
94
94
|
this.#parent = parent;
|
|
95
95
|
// this.reflectEvent = this.reflectEvent.bind(this)
|
|
96
96
|
}
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Registers an activity class with enhanced options.
|
|
99
|
+
* @param action The action name to register the activity for.
|
|
100
|
+
* @param activityClass The activity class constructor.
|
|
101
|
+
* @param options Optional registration configuration.
|
|
102
|
+
*/
|
|
103
|
+
registerActivity(action, activityClass, options = {}) {
|
|
104
|
+
const { override = false } = options;
|
|
105
|
+
if (this.activityClasses.has(action) && !override) {
|
|
99
106
|
// eslint-disable-next-line no-console
|
|
100
107
|
console.warn(`Activity with action "${action}" already registered.`, new Error().stack);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
// Validate that the class extends Activity
|
|
111
|
+
if (typeof activityClass !== 'function') {
|
|
112
|
+
throw new Error(`Invalid activity class for action "${action}": must be a constructor function`);
|
|
101
113
|
}
|
|
102
114
|
this.activityClasses.set(action, activityClass);
|
|
103
115
|
}
|
|
@@ -245,6 +257,8 @@ export class ActivityManager {
|
|
|
245
257
|
stackEntry.activity.lifecycle = ActivityLifecycle.Stopped;
|
|
246
258
|
await stackEntry.activity.onDestroy();
|
|
247
259
|
stackEntry.activity.lifecycle = ActivityLifecycle.Destroyed;
|
|
260
|
+
// Clean up resources to prevent memory leaks
|
|
261
|
+
this.cleanupActivity(stackEntry.activity);
|
|
248
262
|
await this.manageActivityResult(stackEntry.activity, stackEntry.intent);
|
|
249
263
|
// Resume the previous activity
|
|
250
264
|
await this.bringLastActivityToFront();
|
|
@@ -284,11 +298,11 @@ export class ActivityManager {
|
|
|
284
298
|
async bringLastActivityToFront() {
|
|
285
299
|
if (this.modalActivityStack.length > 0) {
|
|
286
300
|
const previousEntry = this.modalActivityStack[this.activityStack.length - 1];
|
|
287
|
-
await this.updateCurrentActivity(previousEntry.activity, false);
|
|
301
|
+
await this.updateCurrentActivity(previousEntry.activity, false, previousEntry.intent);
|
|
288
302
|
}
|
|
289
303
|
else if (this.activityStack.length > 0) {
|
|
290
304
|
const previousEntry = this.activityStack[this.activityStack.length - 1];
|
|
291
|
-
await this.updateCurrentActivity(previousEntry.activity, false);
|
|
305
|
+
await this.updateCurrentActivity(previousEntry.activity, false, previousEntry.intent);
|
|
292
306
|
}
|
|
293
307
|
else {
|
|
294
308
|
this.currentActivity = undefined; // No more activities
|
|
@@ -343,7 +357,7 @@ export class ActivityManager {
|
|
|
343
357
|
*
|
|
344
358
|
* @param activity The activity to bring to the foreground.
|
|
345
359
|
*/
|
|
346
|
-
async updateCurrentActivity(activity, isModal) {
|
|
360
|
+
async updateCurrentActivity(activity, isModal, intent) {
|
|
347
361
|
this.setupStyles(activity, this.currentActivity);
|
|
348
362
|
if (this.currentActivity &&
|
|
349
363
|
this.currentActivity !== activity &&
|
|
@@ -353,61 +367,33 @@ export class ActivityManager {
|
|
|
353
367
|
await this.currentActivity.onPause();
|
|
354
368
|
this.currentActivity.lifecycle = ActivityLifecycle.Paused;
|
|
355
369
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}
|
|
384
|
-
else if (activity.lifecycle === ActivityLifecycle.Started) {
|
|
385
|
-
await activity.onResume();
|
|
386
|
-
if (this.isDestroyed(activity)) {
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
activity.lifecycle = ActivityLifecycle.Resumed;
|
|
390
|
-
activity.requestUpdate();
|
|
391
|
-
}
|
|
392
|
-
else if (activity.lifecycle === ActivityLifecycle.Initialized) {
|
|
393
|
-
// TODO: Figure out intent passing here.
|
|
394
|
-
await activity.onCreate();
|
|
395
|
-
activity.lifecycle = ActivityLifecycle.Created;
|
|
396
|
-
if (this.isDestroyed(activity)) {
|
|
397
|
-
// the activity finished and the manager already took care of this situation.
|
|
398
|
-
return;
|
|
399
|
-
}
|
|
400
|
-
await activity.onStart();
|
|
401
|
-
if (this.isDestroyed(activity)) {
|
|
402
|
-
return;
|
|
403
|
-
}
|
|
404
|
-
activity.lifecycle = ActivityLifecycle.Started;
|
|
405
|
-
await activity.onResume();
|
|
406
|
-
if (this.isDestroyed(activity)) {
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
activity.lifecycle = ActivityLifecycle.Resumed;
|
|
410
|
-
activity.requestUpdate();
|
|
370
|
+
switch (activity.lifecycle) {
|
|
371
|
+
case ActivityLifecycle.Paused:
|
|
372
|
+
if (!(await this.transitionStartedToResumed(activity))) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
break;
|
|
376
|
+
case ActivityLifecycle.Stopped:
|
|
377
|
+
await this.safeExecuteLifecycleMethod(() => activity.onRestart(), activity, 'onRestart');
|
|
378
|
+
activity.lifecycle = ActivityLifecycle.Resumed;
|
|
379
|
+
break;
|
|
380
|
+
case ActivityLifecycle.Destroyed:
|
|
381
|
+
throw new Error(`Invalid state. The activity is already destroyed.`);
|
|
382
|
+
case ActivityLifecycle.Created:
|
|
383
|
+
if (!(await this.transitionCreatedToResumed(activity))) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
break;
|
|
387
|
+
case ActivityLifecycle.Started:
|
|
388
|
+
if (!(await this.transitionStartedToResumed(activity))) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
break;
|
|
392
|
+
case ActivityLifecycle.Initialized:
|
|
393
|
+
if (!(await this.transitionInitializedToResumed(activity, intent))) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
break;
|
|
411
397
|
}
|
|
412
398
|
if (!isModal) {
|
|
413
399
|
// With a modal activity, it renders directly to the `<body>` and renders outside
|
|
@@ -415,17 +401,104 @@ export class ActivityManager {
|
|
|
415
401
|
this.currentActivity = activity;
|
|
416
402
|
}
|
|
417
403
|
}
|
|
404
|
+
/**
|
|
405
|
+
* Safely executes an async lifecycle method with error handling.
|
|
406
|
+
* @param lifecycleMethod The lifecycle method to execute.
|
|
407
|
+
* @param activity The activity instance.
|
|
408
|
+
* @param methodName The name of the method for error reporting.
|
|
409
|
+
* @returns Promise that resolves when the method completes or rejects with wrapped error.
|
|
410
|
+
*/
|
|
411
|
+
async safeExecuteLifecycleMethod(lifecycleMethod, activity, methodName) {
|
|
412
|
+
try {
|
|
413
|
+
await lifecycleMethod();
|
|
414
|
+
}
|
|
415
|
+
catch (error) {
|
|
416
|
+
const activityName = activity.constructor.name;
|
|
417
|
+
const wrappedError = new Error(`Error in ${activityName}.${methodName}(): ${error instanceof Error ? error.message : String(error)}`);
|
|
418
|
+
wrappedError.cause = error;
|
|
419
|
+
// Allow the activity to handle its own errors, but still propagate
|
|
420
|
+
this.#parent.dispatchEvent(new CustomEvent('activity-lifecycle-error', {
|
|
421
|
+
detail: { activity, methodName, error: wrappedError },
|
|
422
|
+
}));
|
|
423
|
+
throw wrappedError;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
418
426
|
/**
|
|
419
427
|
* Allows navigating back through the activity stack.
|
|
420
428
|
* Call `ActivityManager.navigateBack()` when the user performs a back action
|
|
421
429
|
* (e.g., clicks a back button).
|
|
430
|
+
* @returns Promise that resolves to true if navigation was successful, false if at root.
|
|
422
431
|
*/
|
|
423
432
|
async navigateBack() {
|
|
433
|
+
// Handle modal activities first
|
|
434
|
+
if (this.modalActivityStack.length > 0) {
|
|
435
|
+
const topModal = this.modalActivityStack[this.modalActivityStack.length - 1];
|
|
436
|
+
await this.finishActivity(topModal.activity);
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
// Handle regular activities
|
|
424
440
|
if (this.activityStack.length > 1 && this.currentActivity) {
|
|
425
|
-
// Finish current, which will resume previous
|
|
426
441
|
await this.finishActivity(this.currentActivity);
|
|
442
|
+
return true;
|
|
427
443
|
}
|
|
428
|
-
//
|
|
444
|
+
// At root, cannot navigate back
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Navigates to a specific activity in the stack, clearing activities above it.
|
|
449
|
+
* @param action The action of the activity to navigate to.
|
|
450
|
+
* @returns Promise that resolves to true if navigation was successful.
|
|
451
|
+
*/
|
|
452
|
+
async navigateToActivity(action) {
|
|
453
|
+
const index = this.activityStack.findIndex((entry) => entry.action === action);
|
|
454
|
+
if (index === -1) {
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
457
|
+
// Finish all activities above the target
|
|
458
|
+
const activitiesToFinish = this.activityStack.slice(index + 1);
|
|
459
|
+
for (const entry of activitiesToFinish) {
|
|
460
|
+
await this.finishActivity(entry.activity);
|
|
461
|
+
}
|
|
462
|
+
// Bring the target activity to the front
|
|
463
|
+
const targetEntry = this.activityStack[index];
|
|
464
|
+
await this.updateCurrentActivity(targetEntry.activity, false, targetEntry.intent);
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Clears the entire activity stack and starts a new root activity.
|
|
469
|
+
* @param intent The intent for the new root activity.
|
|
470
|
+
*/
|
|
471
|
+
async clearStackAndStart(intent) {
|
|
472
|
+
// Finish all activities
|
|
473
|
+
const allActivities = [...this.activityStack, ...this.modalActivityStack];
|
|
474
|
+
for (const entry of allActivities) {
|
|
475
|
+
if (entry.activity.lifecycle !== ActivityLifecycle.Destroyed) {
|
|
476
|
+
await this.finishActivity(entry.activity);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
// Clear stacks
|
|
480
|
+
this.activityStack.length = 0;
|
|
481
|
+
this.modalActivityStack.length = 0;
|
|
482
|
+
this.currentActivity = undefined;
|
|
483
|
+
// Start new root activity
|
|
484
|
+
await this.startActivity(intent);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Creates an intent with better type safety and validation.
|
|
488
|
+
* @param action The action name.
|
|
489
|
+
* @param data Optional intent data.
|
|
490
|
+
* @param options Optional intent configuration.
|
|
491
|
+
* @returns A properly formed intent.
|
|
492
|
+
*/
|
|
493
|
+
createIntent(action, data, options = {}) {
|
|
494
|
+
if (!this.isActionRegistered(action)) {
|
|
495
|
+
throw new Error(`Cannot create intent for unregistered action: ${action}`);
|
|
496
|
+
}
|
|
497
|
+
return {
|
|
498
|
+
action,
|
|
499
|
+
data,
|
|
500
|
+
...options,
|
|
501
|
+
};
|
|
429
502
|
}
|
|
430
503
|
createRequestCode() {
|
|
431
504
|
const min = 1000;
|
|
@@ -435,5 +508,121 @@ export class ActivityManager {
|
|
|
435
508
|
findActiveActivity(action) {
|
|
436
509
|
return this.activityStack.find((entry) => entry.action === action)?.activity;
|
|
437
510
|
}
|
|
511
|
+
/**
|
|
512
|
+
* Transitions an activity from Created to Resumed state.
|
|
513
|
+
* @param activity The activity to transition.
|
|
514
|
+
* @returns true if successful, false if activity was destroyed during transition.
|
|
515
|
+
*/
|
|
516
|
+
async transitionCreatedToResumed(activity) {
|
|
517
|
+
await this.safeExecuteLifecycleMethod(() => activity.onStart(), activity, 'onStart');
|
|
518
|
+
if (this.isDestroyed(activity)) {
|
|
519
|
+
return false;
|
|
520
|
+
}
|
|
521
|
+
activity.lifecycle = ActivityLifecycle.Started;
|
|
522
|
+
await this.safeExecuteLifecycleMethod(() => activity.onResume(), activity, 'onResume');
|
|
523
|
+
if (this.isDestroyed(activity)) {
|
|
524
|
+
return false;
|
|
525
|
+
}
|
|
526
|
+
activity.lifecycle = ActivityLifecycle.Resumed;
|
|
527
|
+
activity.requestUpdate();
|
|
528
|
+
return true;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Transitions an activity from Started to Resumed state.
|
|
532
|
+
* @param activity The activity to transition.
|
|
533
|
+
* @returns true if successful, false if activity was destroyed during transition.
|
|
534
|
+
*/
|
|
535
|
+
async transitionStartedToResumed(activity) {
|
|
536
|
+
await this.safeExecuteLifecycleMethod(() => activity.onResume(), activity, 'onResume');
|
|
537
|
+
if (this.isDestroyed(activity)) {
|
|
538
|
+
return false;
|
|
539
|
+
}
|
|
540
|
+
activity.lifecycle = ActivityLifecycle.Resumed;
|
|
541
|
+
activity.requestUpdate();
|
|
542
|
+
return true;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Transitions an activity from Initialized to Resumed state.
|
|
546
|
+
* @param activity The activity to transition.
|
|
547
|
+
* @param intent The intent used to create the activity.
|
|
548
|
+
* @returns true if successful, false if activity was destroyed during transition.
|
|
549
|
+
*/
|
|
550
|
+
async transitionInitializedToResumed(activity, intent) {
|
|
551
|
+
await this.safeExecuteLifecycleMethod(() => activity.onCreate(intent), activity, 'onCreate');
|
|
552
|
+
activity.lifecycle = ActivityLifecycle.Created;
|
|
553
|
+
if (this.isDestroyed(activity)) {
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
return await this.transitionCreatedToResumed(activity);
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Cleans up resources for destroyed activities to prevent memory leaks.
|
|
560
|
+
* @param activity The activity to clean up.
|
|
561
|
+
*/
|
|
562
|
+
cleanupActivity(activity) {
|
|
563
|
+
// Remove any event listeners that might create memory leaks
|
|
564
|
+
// This changes the entire prototype chain of the activity,
|
|
565
|
+
// effectively making it a new object. We can't do that as the activity will loose some
|
|
566
|
+
// of its properties, so we don't do it for now.
|
|
567
|
+
// if (activity instanceof EventTarget) {
|
|
568
|
+
// // Clear all event listeners by replacing the activity with a new EventTarget
|
|
569
|
+
// // This is a defensive approach since we can't enumerate listeners
|
|
570
|
+
// Object.setPrototypeOf(activity, EventTarget.prototype)
|
|
571
|
+
// }
|
|
572
|
+
// Clear any references that might prevent garbage collection
|
|
573
|
+
activity.renderRoot = undefined;
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Validates that an activity stack is in a consistent state.
|
|
577
|
+
* @param stack The activity stack to validate.
|
|
578
|
+
* @returns Array of validation errors, empty if valid.
|
|
579
|
+
*/
|
|
580
|
+
validateStackConsistency(stack) {
|
|
581
|
+
const errors = [];
|
|
582
|
+
const seenIds = new Set();
|
|
583
|
+
for (const entry of stack) {
|
|
584
|
+
if (seenIds.has(entry.id)) {
|
|
585
|
+
errors.push(`Duplicate activity ID found: ${entry.id}`);
|
|
586
|
+
}
|
|
587
|
+
seenIds.add(entry.id);
|
|
588
|
+
if (!entry.activity || !entry.id || !entry.action) {
|
|
589
|
+
errors.push(`Invalid stack entry: missing required properties`);
|
|
590
|
+
}
|
|
591
|
+
if (entry.activity.lifecycle === ActivityLifecycle.Destroyed) {
|
|
592
|
+
errors.push(`Destroyed activity found in stack: ${entry.id}`);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
return errors;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Gets diagnostic information about the current state of the activity manager.
|
|
599
|
+
* Useful for debugging and monitoring.
|
|
600
|
+
* @returns Object containing current state information.
|
|
601
|
+
*/
|
|
602
|
+
getDiagnostics() {
|
|
603
|
+
return {
|
|
604
|
+
totalActivities: this.activityStack.length,
|
|
605
|
+
modalActivities: this.modalActivityStack.length,
|
|
606
|
+
currentActivity: this.currentActivity?.constructor.name,
|
|
607
|
+
topActivity: this.getTopActivity()?.constructor.name,
|
|
608
|
+
stackValidation: this.validateStackConsistency(this.activityStack),
|
|
609
|
+
modalStackValidation: this.validateStackConsistency(this.modalActivityStack),
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Gets all registered activity actions.
|
|
614
|
+
* @returns Array of registered action names.
|
|
615
|
+
*/
|
|
616
|
+
getRegisteredActions() {
|
|
617
|
+
return Array.from(this.activityClasses.keys());
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Checks if an action is registered.
|
|
621
|
+
* @param action The action to check.
|
|
622
|
+
* @returns true if the action is registered, false otherwise.
|
|
623
|
+
*/
|
|
624
|
+
isActionRegistered(action) {
|
|
625
|
+
return this.activityClasses.has(action);
|
|
626
|
+
}
|
|
438
627
|
}
|
|
439
628
|
//# sourceMappingURL=ActivityManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ActivityManager.js","sourceRoot":"","sources":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,CAAC;IAED,iBAAiB;QACf,MAAM,GAAG,GAAG,IAAI,CAAA;QAChB,MAAM,GAAG,GAAG,IAAI,CAAA;QAChB,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAA;IACtD,CAAC;IAED,kBAAkB,CAAC,MAAc;QAC/B,OAAO,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,KAAK,MAAM,CAAC,EAAE,QAAQ,CAAA;IAC9E,CAAC;CACF","sourcesContent":["import type { Activity } from './Activity.js'\nimport type { Application } from './Application.js'\nimport { navigateScreen } from './ApplicationRoute.js'\n\nexport enum ActivityLifecycle {\n /**\n * The activity is initialized, but not yet created.\n * This is the initial state of the activity.\n */\n Initialized,\n /**\n * The activity is created, but not yet started.\n * This is the state after the `onCreate()` method is called.\n */\n Created,\n /**\n * The activity is started, but not yet resumed.\n * This is the state after the `onStart()` method is called.\n */\n Started,\n /**\n * The activity is resumed and visible to the user.\n * This is the state after the `onResume()` method is called.\n */\n Resumed,\n /**\n * The activity is paused, but still visible to the user.\n * This is the state after the `onPause()` method is called.\n */\n Paused,\n /**\n * The activity is stopped and no longer visible to the user.\n * This is the state after the `onStop()` method is called.\n */\n Stopped,\n /**\n * The activity is destroyed and no longer exists.\n * This is the state after the `onDestroy()` method is called.\n */\n Destroyed,\n}\n\nexport enum IntentFlags {\n /**\n * Marks that the activity should be rendered as a modal.\n */\n Modal = 1 << 0,\n /**\n * The activity is started for result.\n */\n ForResult = 1 << 1,\n /**\n * When set, the intent data is passed by reference.\n * By default the activity manager makes a copy of the intent data.\n * This is useful when the data is large and you want to avoid copying it\n * or when you want to share the same data between activities.\n */\n ByReference = 1 << 2,\n}\n\nexport interface Intent {\n action: string\n data?: unknown\n category?: string[]\n flags?: number\n /**\n * The request code used to distinguish between different activities.\n */\n requestCode?: number\n}\n\nexport enum IntentResult {\n RESULT_CANCELED,\n RESULT_OK,\n}\n\n/**\n * Defines the information stored for each activity on the stack:\n * the Activity instance, a unique id, and optionally the Intent that started the activity\n * (useful for restoring state).\n */\ninterface ActivityStackEntry {\n /**\n * The instance of the activity.\n */\n activity: Activity\n /**\n * Activity's unique ID.\n */\n id: string\n /**\n * The intent that started the activity.\n */\n intent: Intent\n /**\n * The action name the activity is registered for.\n */\n action: string\n}\n\n/**\n * Manages application activities.\n *\n * An activity is a screen or a portion of a rendered screen that has its own\n * lifecycle methods and routing.\n *\n * An activity is registered by its class definition. Because of that, activities\n * cannot have constructors that require own parameters. Activities have to be\n * self contained or use the event system to request data.\n */\nexport class ActivityManager {\n private currentActivity?: Activity\n\n /**\n * Stores activity *classes* by unique ID (action name).\n */\n private activityClasses = new Map<string, typeof Activity>()\n\n /**\n * Represents the activity stack. It is used to manage\n * the UI state after calling the back action.\n */\n private activityStack: ActivityStackEntry[] = []\n\n /**\n * A stack of activities that are rendered over other activities\n * in a dialog.\n */\n private modalActivityStack: ActivityStackEntry[] = []\n\n private generateActivityId(): string {\n return Math.random().toString(36).substring(2, 15)\n }\n\n #parent: Application\n\n constructor(parent: Application) {\n this.#parent = parent\n // this.reflectEvent = this.reflectEvent.bind(this)\n }\n\n registerActivity(action: string, activityClass: typeof Activity): void {\n if (this.activityClasses.has(action)) {\n // eslint-disable-next-line no-console\n console.warn(`Activity with action \"${action}\" already registered.`, new Error().stack)\n }\n this.activityClasses.set(action, activityClass)\n }\n\n // protected reflectEvent<T extends Event>(event: T): T {\n // const copy = Reflect.construct(event.constructor, [event.type, event])\n // const dispatched = this.#parent.events.dispatchEvent(copy)\n // if (!dispatched) {\n // event.preventDefault()\n // }\n // return copy\n // }\n\n /**\n * Creates an activity and pushes it into the stack, but it does not initialize any of the lifecycle methods.\n * It is a way to put an activity at some place of the stack,\n * but it will be only initialized when other activities finis.\n *\n * Note, if you add an uninitialized activity and you won't add more activities then this activity may never run.\n * Make sure that there's always another activity.\n *\n * @param intent The intent that created this activity.\n */\n async createActivity(intent: Intent): Promise<void> {\n const activity = this.buildActivity(intent)\n const activityId = this.generateActivityId()\n const stackEntry: ActivityStackEntry = { activity, id: activityId, intent, action: intent.action }\n this.activityStack.push(stackEntry)\n }\n\n setupRoute(intent: Intent): void {\n if (intent.data) {\n const typed = intent.data as { uri?: string }\n if (typed.uri) {\n navigateScreen(typed.uri)\n }\n }\n }\n\n /**\n * Starts a new activity and brings it to the foreground.\n *\n * @param intent The intent that created this activity.\n */\n async startActivity(intent: Intent): Promise<void> {\n const { currentActivity, activityStack } = this\n const lastIndex = activityStack.findLastIndex((entry) => entry.action === intent.action)\n // if the activity is in the history list, we bring it up and update intent.\n if (lastIndex > -1) {\n const info = activityStack[lastIndex]\n activityStack.splice(lastIndex, 1)\n activityStack.push({\n id: info.id,\n activity: info.activity,\n action: info.action,\n intent,\n })\n await info.activity.onNewIntent(intent)\n // TODO: Check if the activity is destroyed.\n this.setupRoute(intent)\n await this.updateCurrentActivity(info.activity, false)\n return\n }\n const activity = this.buildActivity(intent)\n if (currentActivity) {\n await currentActivity.onPause()\n currentActivity.lifecycle = ActivityLifecycle.Paused\n }\n\n const activityId = this.generateActivityId()\n const stackEntry: ActivityStackEntry = { activity, id: activityId, intent, action: intent.action }\n // Modal activities are pretty much the same as regular activities but are rendered on top\n // of other activities. We keep track of them on another stack as all modal activities must\n // finish before returning to the regular stack.\n const isModal = (intent.flags ?? 0) & IntentFlags.Modal\n if (isModal) {\n this.modalActivityStack.push(stackEntry)\n } else {\n this.activityStack.push(stackEntry)\n }\n await activity.onCreate(intent)\n if (this.isDestroyed(activity)) {\n // the activity finished and the manager already took care of this situation.\n return\n }\n activity.lifecycle = ActivityLifecycle.Created\n\n await this.updateCurrentActivity(activity, !!isModal)\n if (this.isDestroyed(activity)) {\n // the activity finished and the manager already took care of this situation.\n return\n }\n this.setupRoute(intent)\n }\n\n private buildActivity(intent: Intent): Activity {\n const action = intent.action\n if (!this.activityClasses.has(action)) {\n throw new Error(`No activity class registered for action: ${action}`)\n }\n\n const ActivityClass = this.activityClasses.get(action)\n if (!ActivityClass) {\n throw new Error(`No activity class registered for action: ${action}`)\n }\n\n // We use the container to resolve dependencies, if any.\n // const activity = (await container.make(ActivityClass, [this.#parent])) as Activity\n return new ActivityClass(this.#parent)\n }\n\n /**\n * Starts an activity that should return a result to the calling activity.\n * The result should be a unique number across the application.\n * @param intent The intent to start the activity.\n * @param requestCode The request code used to match the activity result.\n */\n async startActivityForResult(intent: Intent, requestCode: number): Promise<void> {\n intent.requestCode = requestCode\n const byReference = (intent.flags ?? 0) & IntentFlags.ByReference\n const shallowCopy = { ...intent }\n const data = shallowCopy.data\n if (byReference) {\n delete shallowCopy.data\n }\n const deepCopy = structuredClone(shallowCopy)\n if (data && byReference) {\n deepCopy.data = data\n }\n if (deepCopy.flags) {\n deepCopy.flags = deepCopy.flags | IntentFlags.ForResult\n } else {\n deepCopy.flags = IntentFlags.ForResult\n }\n await this.startActivity(deepCopy)\n }\n\n isDestroyed(activity: Activity): boolean {\n return activity.lifecycle === ActivityLifecycle.Destroyed\n }\n\n /**\n * @param activity The activity to finish.\n */\n async finishActivity(activity: Activity): Promise<void> {\n this.setupStyles(undefined, activity)\n const isModal = this.modalActivityStack.some((entry) => entry.activity === activity)\n const stack = isModal ? this.modalActivityStack : this.activityStack\n const index = stack.findIndex((entry) => entry.activity === activity)\n if (index !== -1) {\n const stackEntry = stack.splice(index, 1)[0]\n\n await stackEntry.activity.onPause()\n stackEntry.activity.lifecycle = ActivityLifecycle.Paused\n await stackEntry.activity.onStop()\n stackEntry.activity.lifecycle = ActivityLifecycle.Stopped\n await stackEntry.activity.onDestroy()\n stackEntry.activity.lifecycle = ActivityLifecycle.Destroyed\n\n await this.manageActivityResult(stackEntry.activity, stackEntry.intent)\n\n // Resume the previous activity\n await this.bringLastActivityToFront()\n } else {\n if (activity.lifecycle === ActivityLifecycle.Created) {\n await activity.onStop()\n activity.lifecycle = ActivityLifecycle.Stopped\n }\n await activity.onDestroy()\n activity.lifecycle = ActivityLifecycle.Destroyed\n // This can happen when an activity finishes in one of the callback methods,\n // before it is added to the stack. In that case, we bring the last activity back to the front.\n await this.bringLastActivityToFront()\n }\n this.#parent.requestUpdate()\n }\n\n private async manageActivityResult(activity: Activity, intent: Intent): Promise<void> {\n const { flags = 0, requestCode = -1 } = intent\n if (flags & IntentFlags.ForResult) {\n const all = [...this.activityStack, ...this.modalActivityStack]\n const target = all.find((entry) => entry.activity.hasRequestCode(requestCode))\n if (target) {\n if (target.activity.lifecycle === ActivityLifecycle.Destroyed) {\n return\n }\n if (target.activity.lifecycle === ActivityLifecycle.Resumed) {\n await target.activity.onPause()\n target.activity.lifecycle = ActivityLifecycle.Paused\n }\n const intentCopy = structuredClone(intent)\n intentCopy.data = activity.getResult()\n await target.activity.onActivityResult(requestCode, activity.resultCode, intentCopy)\n }\n }\n }\n\n private async bringLastActivityToFront(): Promise<void> {\n if (this.modalActivityStack.length > 0) {\n const previousEntry = this.modalActivityStack[this.activityStack.length - 1]\n await this.updateCurrentActivity(previousEntry.activity, false)\n } else if (this.activityStack.length > 0) {\n const previousEntry = this.activityStack[this.activityStack.length - 1]\n await this.updateCurrentActivity(previousEntry.activity, false)\n } else {\n this.currentActivity = undefined // No more activities\n }\n }\n\n getTopActivity(): Activity | undefined {\n const topModal = this.modalActivityStack.findLast((a) => a.activity.lifecycle === ActivityLifecycle.Resumed)\n if (topModal) {\n return topModal.activity\n }\n const top = this.activityStack.findLast((a) => a.activity.lifecycle === ActivityLifecycle.Resumed)\n return top ? top.activity : undefined\n }\n\n getCurrentActivity(): Activity | undefined {\n return this.currentActivity\n }\n\n /**\n * Sets the styles for the current activity.\n * This method adds or removes CSS classes based on the activity's class name\n * and action, allowing for dynamic styling based on the active activity.\n * @param activity The activity to set styles for.\n * If not provided, it will not set any styles.\n * If the activity is the same as the current one, it does nothing.\n */\n setupStyles(activity?: Activity, oldActivity?: Activity): void {\n if (activity === oldActivity) {\n return\n }\n const root = document?.documentElement\n if (!root) {\n return\n }\n if (oldActivity) {\n const ctor = oldActivity.constructor as typeof Activity\n root.classList.remove(ctor.name.toLowerCase())\n if (ctor.action) {\n root.classList.remove(ctor.action.replace(/\\//g, '-'))\n }\n }\n if (activity) {\n const ctor = activity.constructor as typeof Activity\n root.classList.add(ctor.name.toLowerCase())\n if (ctor.action) {\n root.classList.add(ctor.action.replace(/\\//g, '-'))\n }\n }\n }\n\n /**\n * A helper method to handle the transition between activities, ensuring\n * lifecycle methods are called in the correct order (pause the previous activity,\n * then start/resume the new one). This centralizes lifecycle management.\n *\n * @param activity The activity to bring to the foreground.\n */\n private async updateCurrentActivity(activity: Activity, isModal: boolean): Promise<void> {\n this.setupStyles(activity, this.currentActivity)\n if (\n this.currentActivity &&\n this.currentActivity !== activity &&\n this.currentActivity.lifecycle !== ActivityLifecycle.Destroyed &&\n // Make sure we don't pause the current activity if it's already paused.\n this.currentActivity.lifecycle !== ActivityLifecycle.Paused\n ) {\n await this.currentActivity.onPause()\n this.currentActivity.lifecycle = ActivityLifecycle.Paused\n }\n if (activity.lifecycle === ActivityLifecycle.Paused) {\n await activity.onResume()\n if (this.isDestroyed(activity)) {\n return\n }\n activity.lifecycle = ActivityLifecycle.Resumed\n activity.requestUpdate()\n } else if (activity.lifecycle === ActivityLifecycle.Stopped) {\n await activity.onRestart()\n activity.lifecycle = ActivityLifecycle.Resumed\n } else if (activity.lifecycle === ActivityLifecycle.Destroyed) {\n throw new Error(`Invalid state. The activity is already destroyed.`)\n } else if (activity.lifecycle === ActivityLifecycle.Created) {\n await activity.onStart()\n if (this.isDestroyed(activity)) {\n return\n }\n activity.lifecycle = ActivityLifecycle.Started\n await activity.onResume()\n if (this.isDestroyed(activity)) {\n return\n }\n activity.lifecycle = ActivityLifecycle.Resumed\n activity.requestUpdate()\n } else if (activity.lifecycle === ActivityLifecycle.Started) {\n await activity.onResume()\n if (this.isDestroyed(activity)) {\n return\n }\n activity.lifecycle = ActivityLifecycle.Resumed\n activity.requestUpdate()\n } else if (activity.lifecycle === ActivityLifecycle.Initialized) {\n // TODO: Figure out intent passing here.\n await activity.onCreate()\n activity.lifecycle = ActivityLifecycle.Created\n if (this.isDestroyed(activity)) {\n // the activity finished and the manager already took care of this situation.\n return\n }\n await activity.onStart()\n if (this.isDestroyed(activity)) {\n return\n }\n activity.lifecycle = ActivityLifecycle.Started\n await activity.onResume()\n if (this.isDestroyed(activity)) {\n return\n }\n activity.lifecycle = ActivityLifecycle.Resumed\n activity.requestUpdate()\n }\n if (!isModal) {\n // With a modal activity, it renders directly to the `<body>` and renders outside\n // of the normal rendering flow. We keep the previous activity rendered.\n this.currentActivity = activity\n }\n }\n\n /**\n * Allows navigating back through the activity stack.\n * Call `ActivityManager.navigateBack()` when the user performs a back action\n * (e.g., clicks a back button).\n */\n async navigateBack(): Promise<void> {\n if (this.activityStack.length > 1 && this.currentActivity) {\n // Finish current, which will resume previous\n await this.finishActivity(this.currentActivity)\n }\n // Else do nothing (or display a message, or exit the app)\n }\n\n createRequestCode(): number {\n const min = 1000\n const max = 9999\n return Math.floor(Math.random() * (max - min) + min)\n }\n\n findActiveActivity(action: string): Activity | undefined {\n return this.activityStack.find((entry) => entry.action === action)?.activity\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ActivityManager.js","sourceRoot":"","sources":["../../../src/core/ActivityManager.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAEtD,MAAM,CAAN,IAAY,iBAoCX;AApCD,WAAY,iBAAiB;IAC3B;;;OAGG;IACH,uEAAW,CAAA;IACX;;;OAGG;IACH,+DAAO,CAAA;IACP;;;OAGG;IACH,+DAAO,CAAA;IACP;;;OAGG;IACH,+DAAO,CAAA;IACP;;;OAGG;IACH,6DAAM,CAAA;IACN;;;OAGG;IACH,+DAAO,CAAA;IACP;;;OAGG;IACH,mEAAS,CAAA;AACX,CAAC,EApCW,iBAAiB,KAAjB,i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;;OAGG;IACH,oBAAoB;QAClB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,EAAE,CAAC,CAAA;IAChD,CAAC;IAED;;;;OAIG;IACH,kBAAkB,CAAC,MAAc;QAC/B,OAAO,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IACzC,CAAC;CACF","sourcesContent":["import type { Activity } from './Activity.js'\nimport type { Application } from './Application.js'\nimport { navigateScreen } from './ApplicationRoute.js'\n\nexport enum ActivityLifecycle {\n /**\n * The activity is initialized, but not yet created.\n * This is the initial state of the activity.\n */\n Initialized,\n /**\n * The activity is created, but not yet started.\n * This is the state after the `onCreate()` method is called.\n */\n Created,\n /**\n * The activity is started, but not yet resumed.\n * This is the state after the `onStart()` method is called.\n */\n Started,\n /**\n * The activity is resumed and visible to the user.\n * This is the state after the `onResume()` method is called.\n */\n Resumed,\n /**\n * The activity is paused, but still visible to the user.\n * This is the state after the `onPause()` method is called.\n */\n Paused,\n /**\n * The activity is stopped and no longer visible to the user.\n * This is the state after the `onStop()` method is called.\n */\n Stopped,\n /**\n * The activity is destroyed and no longer exists.\n * This is the state after the `onDestroy()` method is called.\n */\n Destroyed,\n}\n\nexport enum IntentFlags {\n /**\n * Marks that the activity should be rendered as a modal.\n */\n Modal = 1 << 0,\n /**\n * The activity is started for result.\n */\n ForResult = 1 << 1,\n /**\n * When set, the intent data is passed by reference.\n * By default the activity manager makes a copy of the intent data.\n * This is useful when the data is large and you want to avoid copying it\n * or when you want to share the same data between activities.\n */\n ByReference = 1 << 2,\n}\n\nexport interface Intent<T = unknown> {\n action: string\n data?: T\n category?: string[]\n flags?: number\n /**\n * The request code used to distinguish between different activities.\n */\n requestCode?: number\n}\n\nexport enum IntentResult {\n RESULT_CANCELED,\n RESULT_OK,\n}\n\n/**\n * Defines the information stored for each activity on the stack:\n * the Activity instance, a unique id, and optionally the Intent that started the activity\n * (useful for restoring state).\n */\ninterface ActivityStackEntry {\n /**\n * The instance of the activity.\n */\n activity: Activity\n /**\n * Activity's unique ID.\n */\n id: string\n /**\n * The intent that started the activity.\n */\n intent: Intent\n /**\n * The action name the activity is registered for.\n */\n action: string\n}\n\n/**\n * Type for activity constructor with static action property.\n */\nexport interface ActivityConstructor {\n new (parent: Application): Activity\n action?: string\n}\n\n/**\n * Enhanced activity registration options.\n */\nexport interface ActivityRegistrationOptions {\n /** Whether to override existing registrations */\n override?: boolean\n /** Optional validation function for intents */\n validateIntent?: (intent: Intent) => boolean\n}\n\n/**\n * Manages application activities.\n *\n * An activity is a screen or a portion of a rendered screen that has its own\n * lifecycle methods and routing.\n *\n * An activity is registered by its class definition. Because of that, activities\n * cannot have constructors that require own parameters. Activities have to be\n * self contained or use the event system to request data.\n */\nexport class ActivityManager {\n private currentActivity?: Activity\n\n /**\n * Stores activity *classes* by unique ID (action name).\n */\n private activityClasses = new Map<string, typeof Activity>()\n\n /**\n * Represents the activity stack. It is used to manage\n * the UI state after calling the back action.\n */\n private activityStack: ActivityStackEntry[] = []\n\n /**\n * A stack of activities that are rendered over other activities\n * in a dialog.\n */\n private modalActivityStack: ActivityStackEntry[] = []\n\n private generateActivityId(): string {\n return Math.random().toString(36).substring(2, 15)\n }\n\n #parent: Application\n\n constructor(parent: Application) {\n this.#parent = parent\n // this.reflectEvent = this.reflectEvent.bind(this)\n }\n\n /**\n * Registers an activity class with enhanced options.\n * @param action The action name to register the activity for.\n * @param activityClass The activity class constructor.\n * @param options Optional registration configuration.\n */\n registerActivity(\n action: string,\n activityClass: ActivityConstructor,\n options: ActivityRegistrationOptions = {}\n ): void {\n const { override = false } = options\n\n if (this.activityClasses.has(action) && !override) {\n // eslint-disable-next-line no-console\n console.warn(`Activity with action \"${action}\" already registered.`, new Error().stack)\n return\n }\n\n // Validate that the class extends Activity\n if (typeof activityClass !== 'function') {\n throw new Error(`Invalid activity class for action \"${action}\": must be a constructor function`)\n }\n\n this.activityClasses.set(action, activityClass as typeof Activity)\n }\n\n // protected reflectEvent<T extends Event>(event: T): T {\n // const copy = Reflect.construct(event.constructor, [event.type, event])\n // const dispatched = this.#parent.events.dispatchEvent(copy)\n // if (!dispatched) {\n // event.preventDefault()\n // }\n // return copy\n // }\n\n /**\n * Creates an activity and pushes it into the stack, but it does not initialize any of the lifecycle methods.\n * It is a way to put an activity at some place of the stack,\n * but it will be only initialized when other activities finis.\n *\n * Note, if you add an uninitialized activity and you won't add more activities then this activity may never run.\n * Make sure that there's always another activity.\n *\n * @param intent The intent that created this activity.\n */\n async createActivity(intent: Intent): Promise<void> {\n const activity = this.buildActivity(intent)\n const activityId = this.generateActivityId()\n const stackEntry: ActivityStackEntry = { activity, id: activityId, intent, action: intent.action }\n this.activityStack.push(stackEntry)\n }\n\n setupRoute(intent: Intent): void {\n if (intent.data) {\n const typed = intent.data as { uri?: string }\n if (typed.uri) {\n navigateScreen(typed.uri)\n }\n }\n }\n\n /**\n * Starts a new activity and brings it to the foreground.\n *\n * @param intent The intent that created this activity.\n */\n async startActivity(intent: Intent): Promise<void> {\n const { currentActivity, activityStack } = this\n const lastIndex = activityStack.findLastIndex((entry) => entry.action === intent.action)\n // if the activity is in the history list, we bring it up and update intent.\n if (lastIndex > -1) {\n const info = activityStack[lastIndex]\n activityStack.splice(lastIndex, 1)\n activityStack.push({\n id: info.id,\n activity: info.activity,\n action: info.action,\n intent,\n })\n await info.activity.onNewIntent(intent)\n // TODO: Check if the activity is destroyed.\n this.setupRoute(intent)\n await this.updateCurrentActivity(info.activity, false)\n return\n }\n const activity = this.buildActivity(intent)\n if (currentActivity) {\n await currentActivity.onPause()\n currentActivity.lifecycle = ActivityLifecycle.Paused\n }\n\n const activityId = this.generateActivityId()\n const stackEntry: ActivityStackEntry = { activity, id: activityId, intent, action: intent.action }\n // Modal activities are pretty much the same as regular activities but are rendered on top\n // of other activities. We keep track of them on another stack as all modal activities must\n // finish before returning to the regular stack.\n const isModal = (intent.flags ?? 0) & IntentFlags.Modal\n if (isModal) {\n this.modalActivityStack.push(stackEntry)\n } else {\n this.activityStack.push(stackEntry)\n }\n await activity.onCreate(intent)\n if (this.isDestroyed(activity)) {\n // the activity finished and the manager already took care of this situation.\n return\n }\n activity.lifecycle = ActivityLifecycle.Created\n\n await this.updateCurrentActivity(activity, !!isModal)\n if (this.isDestroyed(activity)) {\n // the activity finished and the manager already took care of this situation.\n return\n }\n this.setupRoute(intent)\n }\n\n private buildActivity(intent: Intent): Activity {\n const action = intent.action\n if (!this.activityClasses.has(action)) {\n throw new Error(`No activity class registered for action: ${action}`)\n }\n\n const ActivityClass = this.activityClasses.get(action)\n if (!ActivityClass) {\n throw new Error(`No activity class registered for action: ${action}`)\n }\n\n // We use the container to resolve dependencies, if any.\n // const activity = (await container.make(ActivityClass, [this.#parent])) as Activity\n return new ActivityClass(this.#parent)\n }\n\n /**\n * Starts an activity that should return a result to the calling activity.\n * The result should be a unique number across the application.\n * @param intent The intent to start the activity.\n * @param requestCode The request code used to match the activity result.\n */\n async startActivityForResult(intent: Intent, requestCode: number): Promise<void> {\n intent.requestCode = requestCode\n const byReference = (intent.flags ?? 0) & IntentFlags.ByReference\n const shallowCopy = { ...intent }\n const data = shallowCopy.data\n if (byReference) {\n delete shallowCopy.data\n }\n const deepCopy = structuredClone(shallowCopy)\n if (data && byReference) {\n deepCopy.data = data\n }\n if (deepCopy.flags) {\n deepCopy.flags = deepCopy.flags | IntentFlags.ForResult\n } else {\n deepCopy.flags = IntentFlags.ForResult\n }\n await this.startActivity(deepCopy)\n }\n\n isDestroyed(activity: Activity): boolean {\n return activity.lifecycle === ActivityLifecycle.Destroyed\n }\n\n /**\n * @param activity The activity to finish.\n */\n async finishActivity(activity: Activity): Promise<void> {\n this.setupStyles(undefined, activity)\n const isModal = this.modalActivityStack.some((entry) => entry.activity === activity)\n const stack = isModal ? this.modalActivityStack : this.activityStack\n const index = stack.findIndex((entry) => entry.activity === activity)\n if (index !== -1) {\n const stackEntry = stack.splice(index, 1)[0]\n\n await stackEntry.activity.onPause()\n stackEntry.activity.lifecycle = ActivityLifecycle.Paused\n await stackEntry.activity.onStop()\n stackEntry.activity.lifecycle = ActivityLifecycle.Stopped\n await stackEntry.activity.onDestroy()\n stackEntry.activity.lifecycle = ActivityLifecycle.Destroyed\n\n // Clean up resources to prevent memory leaks\n this.cleanupActivity(stackEntry.activity)\n\n await this.manageActivityResult(stackEntry.activity, stackEntry.intent)\n\n // Resume the previous activity\n await this.bringLastActivityToFront()\n } else {\n if (activity.lifecycle === ActivityLifecycle.Created) {\n await activity.onStop()\n activity.lifecycle = ActivityLifecycle.Stopped\n }\n await activity.onDestroy()\n activity.lifecycle = ActivityLifecycle.Destroyed\n // This can happen when an activity finishes in one of the callback methods,\n // before it is added to the stack. In that case, we bring the last activity back to the front.\n await this.bringLastActivityToFront()\n }\n this.#parent.requestUpdate()\n }\n\n private async manageActivityResult(activity: Activity, intent: Intent): Promise<void> {\n const { flags = 0, requestCode = -1 } = intent\n if (flags & IntentFlags.ForResult) {\n const all = [...this.activityStack, ...this.modalActivityStack]\n const target = all.find((entry) => entry.activity.hasRequestCode(requestCode))\n if (target) {\n if (target.activity.lifecycle === ActivityLifecycle.Destroyed) {\n return\n }\n if (target.activity.lifecycle === ActivityLifecycle.Resumed) {\n await target.activity.onPause()\n target.activity.lifecycle = ActivityLifecycle.Paused\n }\n const intentCopy = structuredClone(intent)\n intentCopy.data = activity.getResult()\n await target.activity.onActivityResult(requestCode, activity.resultCode, intentCopy)\n }\n }\n }\n\n private async bringLastActivityToFront(): Promise<void> {\n if (this.modalActivityStack.length > 0) {\n const previousEntry = this.modalActivityStack[this.activityStack.length - 1]\n await this.updateCurrentActivity(previousEntry.activity, false, previousEntry.intent)\n } else if (this.activityStack.length > 0) {\n const previousEntry = this.activityStack[this.activityStack.length - 1]\n await this.updateCurrentActivity(previousEntry.activity, false, previousEntry.intent)\n } else {\n this.currentActivity = undefined // No more activities\n }\n }\n\n getTopActivity(): Activity | undefined {\n const topModal = this.modalActivityStack.findLast((a) => a.activity.lifecycle === ActivityLifecycle.Resumed)\n if (topModal) {\n return topModal.activity\n }\n const top = this.activityStack.findLast((a) => a.activity.lifecycle === ActivityLifecycle.Resumed)\n return top ? top.activity : undefined\n }\n\n getCurrentActivity(): Activity | undefined {\n return this.currentActivity\n }\n\n /**\n * Sets the styles for the current activity.\n * This method adds or removes CSS classes based on the activity's class name\n * and action, allowing for dynamic styling based on the active activity.\n * @param activity The activity to set styles for.\n * If not provided, it will not set any styles.\n * If the activity is the same as the current one, it does nothing.\n */\n setupStyles(activity?: Activity, oldActivity?: Activity): void {\n if (activity === oldActivity) {\n return\n }\n const root = document?.documentElement\n if (!root) {\n return\n }\n if (oldActivity) {\n const ctor = oldActivity.constructor as typeof Activity\n root.classList.remove(ctor.name.toLowerCase())\n if (ctor.action) {\n root.classList.remove(ctor.action.replace(/\\//g, '-'))\n }\n }\n if (activity) {\n const ctor = activity.constructor as typeof Activity\n root.classList.add(ctor.name.toLowerCase())\n if (ctor.action) {\n root.classList.add(ctor.action.replace(/\\//g, '-'))\n }\n }\n }\n\n /**\n * A helper method to handle the transition between activities, ensuring\n * lifecycle methods are called in the correct order (pause the previous activity,\n * then start/resume the new one). This centralizes lifecycle management.\n *\n * @param activity The activity to bring to the foreground.\n */\n private async updateCurrentActivity(activity: Activity, isModal: boolean, intent?: Intent): Promise<void> {\n this.setupStyles(activity, this.currentActivity)\n if (\n this.currentActivity &&\n this.currentActivity !== activity &&\n this.currentActivity.lifecycle !== ActivityLifecycle.Destroyed &&\n // Make sure we don't pause the current activity if it's already paused.\n this.currentActivity.lifecycle !== ActivityLifecycle.Paused\n ) {\n await this.currentActivity.onPause()\n this.currentActivity.lifecycle = ActivityLifecycle.Paused\n }\n switch (activity.lifecycle) {\n case ActivityLifecycle.Paused:\n if (!(await this.transitionStartedToResumed(activity))) {\n return\n }\n break\n\n case ActivityLifecycle.Stopped:\n await this.safeExecuteLifecycleMethod(() => activity.onRestart(), activity, 'onRestart')\n activity.lifecycle = ActivityLifecycle.Resumed\n break\n\n case ActivityLifecycle.Destroyed:\n throw new Error(`Invalid state. The activity is already destroyed.`)\n\n case ActivityLifecycle.Created:\n if (!(await this.transitionCreatedToResumed(activity))) {\n return\n }\n break\n\n case ActivityLifecycle.Started:\n if (!(await this.transitionStartedToResumed(activity))) {\n return\n }\n break\n\n case ActivityLifecycle.Initialized:\n if (!(await this.transitionInitializedToResumed(activity, intent))) {\n return\n }\n break\n }\n if (!isModal) {\n // With a modal activity, it renders directly to the `<body>` and renders outside\n // of the normal rendering flow. We keep the previous activity rendered.\n this.currentActivity = activity\n }\n }\n\n /**\n * Safely executes an async lifecycle method with error handling.\n * @param lifecycleMethod The lifecycle method to execute.\n * @param activity The activity instance.\n * @param methodName The name of the method for error reporting.\n * @returns Promise that resolves when the method completes or rejects with wrapped error.\n */\n private async safeExecuteLifecycleMethod(\n lifecycleMethod: () => void | Promise<void>,\n activity: Activity,\n methodName: string\n ): Promise<void> {\n try {\n await lifecycleMethod()\n } catch (error) {\n const activityName = activity.constructor.name\n const wrappedError = new Error(\n `Error in ${activityName}.${methodName}(): ${error instanceof Error ? error.message : String(error)}`\n )\n wrappedError.cause = error\n // Allow the activity to handle its own errors, but still propagate\n this.#parent.dispatchEvent(\n new CustomEvent('activity-lifecycle-error', {\n detail: { activity, methodName, error: wrappedError },\n })\n )\n throw wrappedError\n }\n }\n\n /**\n * Allows navigating back through the activity stack.\n * Call `ActivityManager.navigateBack()` when the user performs a back action\n * (e.g., clicks a back button).\n * @returns Promise that resolves to true if navigation was successful, false if at root.\n */\n async navigateBack(): Promise<boolean> {\n // Handle modal activities first\n if (this.modalActivityStack.length > 0) {\n const topModal = this.modalActivityStack[this.modalActivityStack.length - 1]\n await this.finishActivity(topModal.activity)\n return true\n }\n\n // Handle regular activities\n if (this.activityStack.length > 1 && this.currentActivity) {\n await this.finishActivity(this.currentActivity)\n return true\n }\n\n // At root, cannot navigate back\n return false\n }\n\n /**\n * Navigates to a specific activity in the stack, clearing activities above it.\n * @param action The action of the activity to navigate to.\n * @returns Promise that resolves to true if navigation was successful.\n */\n async navigateToActivity(action: string): Promise<boolean> {\n const index = this.activityStack.findIndex((entry) => entry.action === action)\n if (index === -1) {\n return false\n }\n\n // Finish all activities above the target\n const activitiesToFinish = this.activityStack.slice(index + 1)\n for (const entry of activitiesToFinish) {\n await this.finishActivity(entry.activity)\n }\n\n // Bring the target activity to the front\n const targetEntry = this.activityStack[index]\n await this.updateCurrentActivity(targetEntry.activity, false, targetEntry.intent)\n return true\n }\n\n /**\n * Clears the entire activity stack and starts a new root activity.\n * @param intent The intent for the new root activity.\n */\n async clearStackAndStart(intent: Intent): Promise<void> {\n // Finish all activities\n const allActivities = [...this.activityStack, ...this.modalActivityStack]\n for (const entry of allActivities) {\n if (entry.activity.lifecycle !== ActivityLifecycle.Destroyed) {\n await this.finishActivity(entry.activity)\n }\n }\n\n // Clear stacks\n this.activityStack.length = 0\n this.modalActivityStack.length = 0\n this.currentActivity = undefined\n\n // Start new root activity\n await this.startActivity(intent)\n }\n\n /**\n * Creates an intent with better type safety and validation.\n * @param action The action name.\n * @param data Optional intent data.\n * @param options Optional intent configuration.\n * @returns A properly formed intent.\n */\n createIntent<T = unknown>(\n action: string,\n data?: T,\n options: {\n category?: string[]\n flags?: number\n requestCode?: number\n } = {}\n ): Intent<T> {\n if (!this.isActionRegistered(action)) {\n throw new Error(`Cannot create intent for unregistered action: ${action}`)\n }\n\n return {\n action,\n data,\n ...options,\n }\n }\n\n createRequestCode(): number {\n const min = 1000\n const max = 9999\n return Math.floor(Math.random() * (max - min) + min)\n }\n\n findActiveActivity(action: string): Activity | undefined {\n return this.activityStack.find((entry) => entry.action === action)?.activity\n }\n\n /**\n * Transitions an activity from Created to Resumed state.\n * @param activity The activity to transition.\n * @returns true if successful, false if activity was destroyed during transition.\n */\n private async transitionCreatedToResumed(activity: Activity): Promise<boolean> {\n await this.safeExecuteLifecycleMethod(() => activity.onStart(), activity, 'onStart')\n if (this.isDestroyed(activity)) {\n return false\n }\n activity.lifecycle = ActivityLifecycle.Started\n\n await this.safeExecuteLifecycleMethod(() => activity.onResume(), activity, 'onResume')\n if (this.isDestroyed(activity)) {\n return false\n }\n activity.lifecycle = ActivityLifecycle.Resumed\n activity.requestUpdate()\n return true\n }\n\n /**\n * Transitions an activity from Started to Resumed state.\n * @param activity The activity to transition.\n * @returns true if successful, false if activity was destroyed during transition.\n */\n private async transitionStartedToResumed(activity: Activity): Promise<boolean> {\n await this.safeExecuteLifecycleMethod(() => activity.onResume(), activity, 'onResume')\n if (this.isDestroyed(activity)) {\n return false\n }\n activity.lifecycle = ActivityLifecycle.Resumed\n activity.requestUpdate()\n return true\n }\n\n /**\n * Transitions an activity from Initialized to Resumed state.\n * @param activity The activity to transition.\n * @param intent The intent used to create the activity.\n * @returns true if successful, false if activity was destroyed during transition.\n */\n private async transitionInitializedToResumed(activity: Activity, intent?: Intent): Promise<boolean> {\n await this.safeExecuteLifecycleMethod(() => activity.onCreate(intent), activity, 'onCreate')\n activity.lifecycle = ActivityLifecycle.Created\n if (this.isDestroyed(activity)) {\n return false\n }\n\n return await this.transitionCreatedToResumed(activity)\n }\n\n /**\n * Cleans up resources for destroyed activities to prevent memory leaks.\n * @param activity The activity to clean up.\n */\n private cleanupActivity(activity: Activity): void {\n // Remove any event listeners that might create memory leaks\n // This changes the entire prototype chain of the activity,\n // effectively making it a new object. We can't do that as the activity will loose some\n // of its properties, so we don't do it for now.\n // if (activity instanceof EventTarget) {\n // // Clear all event listeners by replacing the activity with a new EventTarget\n // // This is a defensive approach since we can't enumerate listeners\n // Object.setPrototypeOf(activity, EventTarget.prototype)\n // }\n\n // Clear any references that might prevent garbage collection\n activity.renderRoot = undefined\n }\n\n /**\n * Validates that an activity stack is in a consistent state.\n * @param stack The activity stack to validate.\n * @returns Array of validation errors, empty if valid.\n */\n private validateStackConsistency(stack: ActivityStackEntry[]): string[] {\n const errors: string[] = []\n const seenIds = new Set<string>()\n\n for (const entry of stack) {\n if (seenIds.has(entry.id)) {\n errors.push(`Duplicate activity ID found: ${entry.id}`)\n }\n seenIds.add(entry.id)\n\n if (!entry.activity || !entry.id || !entry.action) {\n errors.push(`Invalid stack entry: missing required properties`)\n }\n\n if (entry.activity.lifecycle === ActivityLifecycle.Destroyed) {\n errors.push(`Destroyed activity found in stack: ${entry.id}`)\n }\n }\n\n return errors\n }\n\n /**\n * Gets diagnostic information about the current state of the activity manager.\n * Useful for debugging and monitoring.\n * @returns Object containing current state information.\n */\n getDiagnostics(): {\n totalActivities: number\n modalActivities: number\n currentActivity?: string\n topActivity?: string\n stackValidation: string[]\n modalStackValidation: string[]\n } {\n return {\n totalActivities: this.activityStack.length,\n modalActivities: this.modalActivityStack.length,\n currentActivity: this.currentActivity?.constructor.name,\n topActivity: this.getTopActivity()?.constructor.name,\n stackValidation: this.validateStackConsistency(this.activityStack),\n modalStackValidation: this.validateStackConsistency(this.modalActivityStack),\n }\n }\n\n /**\n * Gets all registered activity actions.\n * @returns Array of registered action names.\n */\n getRegisteredActions(): string[] {\n return Array.from(this.activityClasses.keys())\n }\n\n /**\n * Checks if an action is registered.\n * @param action The action to check.\n * @returns true if the action is registered, false otherwise.\n */\n isActionRegistered(action: string): boolean {\n return this.activityClasses.has(action)\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -58,9 +58,9 @@ export enum IntentFlags {
|
|
|
58
58
|
ByReference = 1 << 2,
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
export interface Intent {
|
|
61
|
+
export interface Intent<T = unknown> {
|
|
62
62
|
action: string
|
|
63
|
-
data?:
|
|
63
|
+
data?: T
|
|
64
64
|
category?: string[]
|
|
65
65
|
flags?: number
|
|
66
66
|
/**
|
|
@@ -98,6 +98,24 @@ interface ActivityStackEntry {
|
|
|
98
98
|
action: string
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Type for activity constructor with static action property.
|
|
103
|
+
*/
|
|
104
|
+
export interface ActivityConstructor {
|
|
105
|
+
new (parent: Application): Activity
|
|
106
|
+
action?: string
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Enhanced activity registration options.
|
|
111
|
+
*/
|
|
112
|
+
export interface ActivityRegistrationOptions {
|
|
113
|
+
/** Whether to override existing registrations */
|
|
114
|
+
override?: boolean
|
|
115
|
+
/** Optional validation function for intents */
|
|
116
|
+
validateIntent?: (intent: Intent) => boolean
|
|
117
|
+
}
|
|
118
|
+
|
|
101
119
|
/**
|
|
102
120
|
* Manages application activities.
|
|
103
121
|
*
|
|
@@ -139,12 +157,31 @@ export class ActivityManager {
|
|
|
139
157
|
// this.reflectEvent = this.reflectEvent.bind(this)
|
|
140
158
|
}
|
|
141
159
|
|
|
142
|
-
|
|
143
|
-
|
|
160
|
+
/**
|
|
161
|
+
* Registers an activity class with enhanced options.
|
|
162
|
+
* @param action The action name to register the activity for.
|
|
163
|
+
* @param activityClass The activity class constructor.
|
|
164
|
+
* @param options Optional registration configuration.
|
|
165
|
+
*/
|
|
166
|
+
registerActivity(
|
|
167
|
+
action: string,
|
|
168
|
+
activityClass: ActivityConstructor,
|
|
169
|
+
options: ActivityRegistrationOptions = {}
|
|
170
|
+
): void {
|
|
171
|
+
const { override = false } = options
|
|
172
|
+
|
|
173
|
+
if (this.activityClasses.has(action) && !override) {
|
|
144
174
|
// eslint-disable-next-line no-console
|
|
145
175
|
console.warn(`Activity with action "${action}" already registered.`, new Error().stack)
|
|
176
|
+
return
|
|
146
177
|
}
|
|
147
|
-
|
|
178
|
+
|
|
179
|
+
// Validate that the class extends Activity
|
|
180
|
+
if (typeof activityClass !== 'function') {
|
|
181
|
+
throw new Error(`Invalid activity class for action "${action}": must be a constructor function`)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
this.activityClasses.set(action, activityClass as typeof Activity)
|
|
148
185
|
}
|
|
149
186
|
|
|
150
187
|
// protected reflectEvent<T extends Event>(event: T): T {
|
|
@@ -302,6 +339,9 @@ export class ActivityManager {
|
|
|
302
339
|
await stackEntry.activity.onDestroy()
|
|
303
340
|
stackEntry.activity.lifecycle = ActivityLifecycle.Destroyed
|
|
304
341
|
|
|
342
|
+
// Clean up resources to prevent memory leaks
|
|
343
|
+
this.cleanupActivity(stackEntry.activity)
|
|
344
|
+
|
|
305
345
|
await this.manageActivityResult(stackEntry.activity, stackEntry.intent)
|
|
306
346
|
|
|
307
347
|
// Resume the previous activity
|
|
@@ -343,10 +383,10 @@ export class ActivityManager {
|
|
|
343
383
|
private async bringLastActivityToFront(): Promise<void> {
|
|
344
384
|
if (this.modalActivityStack.length > 0) {
|
|
345
385
|
const previousEntry = this.modalActivityStack[this.activityStack.length - 1]
|
|
346
|
-
await this.updateCurrentActivity(previousEntry.activity, false)
|
|
386
|
+
await this.updateCurrentActivity(previousEntry.activity, false, previousEntry.intent)
|
|
347
387
|
} else if (this.activityStack.length > 0) {
|
|
348
388
|
const previousEntry = this.activityStack[this.activityStack.length - 1]
|
|
349
|
-
await this.updateCurrentActivity(previousEntry.activity, false)
|
|
389
|
+
await this.updateCurrentActivity(previousEntry.activity, false, previousEntry.intent)
|
|
350
390
|
} else {
|
|
351
391
|
this.currentActivity = undefined // No more activities
|
|
352
392
|
}
|
|
@@ -404,7 +444,7 @@ export class ActivityManager {
|
|
|
404
444
|
*
|
|
405
445
|
* @param activity The activity to bring to the foreground.
|
|
406
446
|
*/
|
|
407
|
-
private async updateCurrentActivity(activity: Activity, isModal: boolean): Promise<void> {
|
|
447
|
+
private async updateCurrentActivity(activity: Activity, isModal: boolean, intent?: Intent): Promise<void> {
|
|
408
448
|
this.setupStyles(activity, this.currentActivity)
|
|
409
449
|
if (
|
|
410
450
|
this.currentActivity &&
|
|
@@ -416,56 +456,38 @@ export class ActivityManager {
|
|
|
416
456
|
await this.currentActivity.onPause()
|
|
417
457
|
this.currentActivity.lifecycle = ActivityLifecycle.Paused
|
|
418
458
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
// TODO: Figure out intent passing here.
|
|
452
|
-
await activity.onCreate()
|
|
453
|
-
activity.lifecycle = ActivityLifecycle.Created
|
|
454
|
-
if (this.isDestroyed(activity)) {
|
|
455
|
-
// the activity finished and the manager already took care of this situation.
|
|
456
|
-
return
|
|
457
|
-
}
|
|
458
|
-
await activity.onStart()
|
|
459
|
-
if (this.isDestroyed(activity)) {
|
|
460
|
-
return
|
|
461
|
-
}
|
|
462
|
-
activity.lifecycle = ActivityLifecycle.Started
|
|
463
|
-
await activity.onResume()
|
|
464
|
-
if (this.isDestroyed(activity)) {
|
|
465
|
-
return
|
|
466
|
-
}
|
|
467
|
-
activity.lifecycle = ActivityLifecycle.Resumed
|
|
468
|
-
activity.requestUpdate()
|
|
459
|
+
switch (activity.lifecycle) {
|
|
460
|
+
case ActivityLifecycle.Paused:
|
|
461
|
+
if (!(await this.transitionStartedToResumed(activity))) {
|
|
462
|
+
return
|
|
463
|
+
}
|
|
464
|
+
break
|
|
465
|
+
|
|
466
|
+
case ActivityLifecycle.Stopped:
|
|
467
|
+
await this.safeExecuteLifecycleMethod(() => activity.onRestart(), activity, 'onRestart')
|
|
468
|
+
activity.lifecycle = ActivityLifecycle.Resumed
|
|
469
|
+
break
|
|
470
|
+
|
|
471
|
+
case ActivityLifecycle.Destroyed:
|
|
472
|
+
throw new Error(`Invalid state. The activity is already destroyed.`)
|
|
473
|
+
|
|
474
|
+
case ActivityLifecycle.Created:
|
|
475
|
+
if (!(await this.transitionCreatedToResumed(activity))) {
|
|
476
|
+
return
|
|
477
|
+
}
|
|
478
|
+
break
|
|
479
|
+
|
|
480
|
+
case ActivityLifecycle.Started:
|
|
481
|
+
if (!(await this.transitionStartedToResumed(activity))) {
|
|
482
|
+
return
|
|
483
|
+
}
|
|
484
|
+
break
|
|
485
|
+
|
|
486
|
+
case ActivityLifecycle.Initialized:
|
|
487
|
+
if (!(await this.transitionInitializedToResumed(activity, intent))) {
|
|
488
|
+
return
|
|
489
|
+
}
|
|
490
|
+
break
|
|
469
491
|
}
|
|
470
492
|
if (!isModal) {
|
|
471
493
|
// With a modal activity, it renders directly to the `<body>` and renders outside
|
|
@@ -474,17 +496,130 @@ export class ActivityManager {
|
|
|
474
496
|
}
|
|
475
497
|
}
|
|
476
498
|
|
|
499
|
+
/**
|
|
500
|
+
* Safely executes an async lifecycle method with error handling.
|
|
501
|
+
* @param lifecycleMethod The lifecycle method to execute.
|
|
502
|
+
* @param activity The activity instance.
|
|
503
|
+
* @param methodName The name of the method for error reporting.
|
|
504
|
+
* @returns Promise that resolves when the method completes or rejects with wrapped error.
|
|
505
|
+
*/
|
|
506
|
+
private async safeExecuteLifecycleMethod(
|
|
507
|
+
lifecycleMethod: () => void | Promise<void>,
|
|
508
|
+
activity: Activity,
|
|
509
|
+
methodName: string
|
|
510
|
+
): Promise<void> {
|
|
511
|
+
try {
|
|
512
|
+
await lifecycleMethod()
|
|
513
|
+
} catch (error) {
|
|
514
|
+
const activityName = activity.constructor.name
|
|
515
|
+
const wrappedError = new Error(
|
|
516
|
+
`Error in ${activityName}.${methodName}(): ${error instanceof Error ? error.message : String(error)}`
|
|
517
|
+
)
|
|
518
|
+
wrappedError.cause = error
|
|
519
|
+
// Allow the activity to handle its own errors, but still propagate
|
|
520
|
+
this.#parent.dispatchEvent(
|
|
521
|
+
new CustomEvent('activity-lifecycle-error', {
|
|
522
|
+
detail: { activity, methodName, error: wrappedError },
|
|
523
|
+
})
|
|
524
|
+
)
|
|
525
|
+
throw wrappedError
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
477
529
|
/**
|
|
478
530
|
* Allows navigating back through the activity stack.
|
|
479
531
|
* Call `ActivityManager.navigateBack()` when the user performs a back action
|
|
480
532
|
* (e.g., clicks a back button).
|
|
533
|
+
* @returns Promise that resolves to true if navigation was successful, false if at root.
|
|
481
534
|
*/
|
|
482
|
-
async navigateBack(): Promise<
|
|
535
|
+
async navigateBack(): Promise<boolean> {
|
|
536
|
+
// Handle modal activities first
|
|
537
|
+
if (this.modalActivityStack.length > 0) {
|
|
538
|
+
const topModal = this.modalActivityStack[this.modalActivityStack.length - 1]
|
|
539
|
+
await this.finishActivity(topModal.activity)
|
|
540
|
+
return true
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// Handle regular activities
|
|
483
544
|
if (this.activityStack.length > 1 && this.currentActivity) {
|
|
484
|
-
// Finish current, which will resume previous
|
|
485
545
|
await this.finishActivity(this.currentActivity)
|
|
546
|
+
return true
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// At root, cannot navigate back
|
|
550
|
+
return false
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Navigates to a specific activity in the stack, clearing activities above it.
|
|
555
|
+
* @param action The action of the activity to navigate to.
|
|
556
|
+
* @returns Promise that resolves to true if navigation was successful.
|
|
557
|
+
*/
|
|
558
|
+
async navigateToActivity(action: string): Promise<boolean> {
|
|
559
|
+
const index = this.activityStack.findIndex((entry) => entry.action === action)
|
|
560
|
+
if (index === -1) {
|
|
561
|
+
return false
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// Finish all activities above the target
|
|
565
|
+
const activitiesToFinish = this.activityStack.slice(index + 1)
|
|
566
|
+
for (const entry of activitiesToFinish) {
|
|
567
|
+
await this.finishActivity(entry.activity)
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// Bring the target activity to the front
|
|
571
|
+
const targetEntry = this.activityStack[index]
|
|
572
|
+
await this.updateCurrentActivity(targetEntry.activity, false, targetEntry.intent)
|
|
573
|
+
return true
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Clears the entire activity stack and starts a new root activity.
|
|
578
|
+
* @param intent The intent for the new root activity.
|
|
579
|
+
*/
|
|
580
|
+
async clearStackAndStart(intent: Intent): Promise<void> {
|
|
581
|
+
// Finish all activities
|
|
582
|
+
const allActivities = [...this.activityStack, ...this.modalActivityStack]
|
|
583
|
+
for (const entry of allActivities) {
|
|
584
|
+
if (entry.activity.lifecycle !== ActivityLifecycle.Destroyed) {
|
|
585
|
+
await this.finishActivity(entry.activity)
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// Clear stacks
|
|
590
|
+
this.activityStack.length = 0
|
|
591
|
+
this.modalActivityStack.length = 0
|
|
592
|
+
this.currentActivity = undefined
|
|
593
|
+
|
|
594
|
+
// Start new root activity
|
|
595
|
+
await this.startActivity(intent)
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Creates an intent with better type safety and validation.
|
|
600
|
+
* @param action The action name.
|
|
601
|
+
* @param data Optional intent data.
|
|
602
|
+
* @param options Optional intent configuration.
|
|
603
|
+
* @returns A properly formed intent.
|
|
604
|
+
*/
|
|
605
|
+
createIntent<T = unknown>(
|
|
606
|
+
action: string,
|
|
607
|
+
data?: T,
|
|
608
|
+
options: {
|
|
609
|
+
category?: string[]
|
|
610
|
+
flags?: number
|
|
611
|
+
requestCode?: number
|
|
612
|
+
} = {}
|
|
613
|
+
): Intent<T> {
|
|
614
|
+
if (!this.isActionRegistered(action)) {
|
|
615
|
+
throw new Error(`Cannot create intent for unregistered action: ${action}`)
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
return {
|
|
619
|
+
action,
|
|
620
|
+
data,
|
|
621
|
+
...options,
|
|
486
622
|
}
|
|
487
|
-
// Else do nothing (or display a message, or exit the app)
|
|
488
623
|
}
|
|
489
624
|
|
|
490
625
|
createRequestCode(): number {
|
|
@@ -496,4 +631,142 @@ export class ActivityManager {
|
|
|
496
631
|
findActiveActivity(action: string): Activity | undefined {
|
|
497
632
|
return this.activityStack.find((entry) => entry.action === action)?.activity
|
|
498
633
|
}
|
|
634
|
+
|
|
635
|
+
/**
|
|
636
|
+
* Transitions an activity from Created to Resumed state.
|
|
637
|
+
* @param activity The activity to transition.
|
|
638
|
+
* @returns true if successful, false if activity was destroyed during transition.
|
|
639
|
+
*/
|
|
640
|
+
private async transitionCreatedToResumed(activity: Activity): Promise<boolean> {
|
|
641
|
+
await this.safeExecuteLifecycleMethod(() => activity.onStart(), activity, 'onStart')
|
|
642
|
+
if (this.isDestroyed(activity)) {
|
|
643
|
+
return false
|
|
644
|
+
}
|
|
645
|
+
activity.lifecycle = ActivityLifecycle.Started
|
|
646
|
+
|
|
647
|
+
await this.safeExecuteLifecycleMethod(() => activity.onResume(), activity, 'onResume')
|
|
648
|
+
if (this.isDestroyed(activity)) {
|
|
649
|
+
return false
|
|
650
|
+
}
|
|
651
|
+
activity.lifecycle = ActivityLifecycle.Resumed
|
|
652
|
+
activity.requestUpdate()
|
|
653
|
+
return true
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Transitions an activity from Started to Resumed state.
|
|
658
|
+
* @param activity The activity to transition.
|
|
659
|
+
* @returns true if successful, false if activity was destroyed during transition.
|
|
660
|
+
*/
|
|
661
|
+
private async transitionStartedToResumed(activity: Activity): Promise<boolean> {
|
|
662
|
+
await this.safeExecuteLifecycleMethod(() => activity.onResume(), activity, 'onResume')
|
|
663
|
+
if (this.isDestroyed(activity)) {
|
|
664
|
+
return false
|
|
665
|
+
}
|
|
666
|
+
activity.lifecycle = ActivityLifecycle.Resumed
|
|
667
|
+
activity.requestUpdate()
|
|
668
|
+
return true
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Transitions an activity from Initialized to Resumed state.
|
|
673
|
+
* @param activity The activity to transition.
|
|
674
|
+
* @param intent The intent used to create the activity.
|
|
675
|
+
* @returns true if successful, false if activity was destroyed during transition.
|
|
676
|
+
*/
|
|
677
|
+
private async transitionInitializedToResumed(activity: Activity, intent?: Intent): Promise<boolean> {
|
|
678
|
+
await this.safeExecuteLifecycleMethod(() => activity.onCreate(intent), activity, 'onCreate')
|
|
679
|
+
activity.lifecycle = ActivityLifecycle.Created
|
|
680
|
+
if (this.isDestroyed(activity)) {
|
|
681
|
+
return false
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
return await this.transitionCreatedToResumed(activity)
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Cleans up resources for destroyed activities to prevent memory leaks.
|
|
689
|
+
* @param activity The activity to clean up.
|
|
690
|
+
*/
|
|
691
|
+
private cleanupActivity(activity: Activity): void {
|
|
692
|
+
// Remove any event listeners that might create memory leaks
|
|
693
|
+
// This changes the entire prototype chain of the activity,
|
|
694
|
+
// effectively making it a new object. We can't do that as the activity will loose some
|
|
695
|
+
// of its properties, so we don't do it for now.
|
|
696
|
+
// if (activity instanceof EventTarget) {
|
|
697
|
+
// // Clear all event listeners by replacing the activity with a new EventTarget
|
|
698
|
+
// // This is a defensive approach since we can't enumerate listeners
|
|
699
|
+
// Object.setPrototypeOf(activity, EventTarget.prototype)
|
|
700
|
+
// }
|
|
701
|
+
|
|
702
|
+
// Clear any references that might prevent garbage collection
|
|
703
|
+
activity.renderRoot = undefined
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Validates that an activity stack is in a consistent state.
|
|
708
|
+
* @param stack The activity stack to validate.
|
|
709
|
+
* @returns Array of validation errors, empty if valid.
|
|
710
|
+
*/
|
|
711
|
+
private validateStackConsistency(stack: ActivityStackEntry[]): string[] {
|
|
712
|
+
const errors: string[] = []
|
|
713
|
+
const seenIds = new Set<string>()
|
|
714
|
+
|
|
715
|
+
for (const entry of stack) {
|
|
716
|
+
if (seenIds.has(entry.id)) {
|
|
717
|
+
errors.push(`Duplicate activity ID found: ${entry.id}`)
|
|
718
|
+
}
|
|
719
|
+
seenIds.add(entry.id)
|
|
720
|
+
|
|
721
|
+
if (!entry.activity || !entry.id || !entry.action) {
|
|
722
|
+
errors.push(`Invalid stack entry: missing required properties`)
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
if (entry.activity.lifecycle === ActivityLifecycle.Destroyed) {
|
|
726
|
+
errors.push(`Destroyed activity found in stack: ${entry.id}`)
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
return errors
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* Gets diagnostic information about the current state of the activity manager.
|
|
735
|
+
* Useful for debugging and monitoring.
|
|
736
|
+
* @returns Object containing current state information.
|
|
737
|
+
*/
|
|
738
|
+
getDiagnostics(): {
|
|
739
|
+
totalActivities: number
|
|
740
|
+
modalActivities: number
|
|
741
|
+
currentActivity?: string
|
|
742
|
+
topActivity?: string
|
|
743
|
+
stackValidation: string[]
|
|
744
|
+
modalStackValidation: string[]
|
|
745
|
+
} {
|
|
746
|
+
return {
|
|
747
|
+
totalActivities: this.activityStack.length,
|
|
748
|
+
modalActivities: this.modalActivityStack.length,
|
|
749
|
+
currentActivity: this.currentActivity?.constructor.name,
|
|
750
|
+
topActivity: this.getTopActivity()?.constructor.name,
|
|
751
|
+
stackValidation: this.validateStackConsistency(this.activityStack),
|
|
752
|
+
modalStackValidation: this.validateStackConsistency(this.modalActivityStack),
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* Gets all registered activity actions.
|
|
758
|
+
* @returns Array of registered action names.
|
|
759
|
+
*/
|
|
760
|
+
getRegisteredActions(): string[] {
|
|
761
|
+
return Array.from(this.activityClasses.keys())
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* Checks if an action is registered.
|
|
766
|
+
* @param action The action to check.
|
|
767
|
+
* @returns true if the action is registered, false otherwise.
|
|
768
|
+
*/
|
|
769
|
+
isActionRegistered(action: string): boolean {
|
|
770
|
+
return this.activityClasses.has(action)
|
|
771
|
+
}
|
|
499
772
|
}
|
|
@@ -354,10 +354,10 @@ describe('ActivityManager', () => {
|
|
|
354
354
|
|
|
355
355
|
assert.isTrue(activity1.onActivityResult.calledOnce)
|
|
356
356
|
const [calledRequestCode, calledResultCode, calledIntent] = activity1.onActivityResult.firstCall.args
|
|
357
|
-
assert.equal(calledRequestCode, requestCode)
|
|
358
|
-
assert.equal(calledResultCode, IntentResult.RESULT_OK)
|
|
359
|
-
assert.deepEqual(calledIntent.data, resultData)
|
|
360
|
-
assert.equal(calledIntent.action, TestActivity2.action)
|
|
357
|
+
assert.equal(calledRequestCode, requestCode, 'has the request code from activity1')
|
|
358
|
+
assert.equal(calledResultCode, IntentResult.RESULT_OK, 'has the OK result code')
|
|
359
|
+
assert.deepEqual(calledIntent.data, resultData, 'has the result data from activity2')
|
|
360
|
+
assert.equal(calledIntent.action, TestActivity2.action, 'has the action of activity2')
|
|
361
361
|
})
|
|
362
362
|
|
|
363
363
|
it('handles finishing an activity that was only created (not fully started)', async () => {
|