@api-client/ui 0.6.4 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,15 @@
1
1
  import { nothing, TemplateResult } from 'lit';
2
- import { ActivityLifecycle, IntentResult, type Intent } from './ActivityManager.js';
2
+ import { ActivityLifecycle, IntentResult, type Intent, type ResolvedIntent } from './ActivityManager.js';
3
3
  import { FragmentManager } from './FragmentManager.js';
4
4
  import type { Fragment } from './Fragment.js';
5
5
  import type { Application, UpdateRequest } from './Application.js';
6
6
  import type { ActivityDetail, ActivityWithResultDetail } from '../events/IntentEvents.js';
7
+ export interface ActivityResult {
8
+ requestCode: number;
9
+ resultCode: IntentResult;
10
+ data?: unknown;
11
+ intent: Intent;
12
+ }
7
13
  /**
8
14
  * ## Activity
9
15
  *
@@ -70,13 +76,16 @@ export declare class Activity extends EventTarget {
70
76
  protected exitCode: IntentResult;
71
77
  /** Gets the current result code for the activity. */
72
78
  get resultCode(): IntentResult;
73
- /** Tracks pending request codes for activities started for result. */
74
- protected pendingRequestCodes: number[];
75
79
  /**
76
80
  * Constructs a new Activity.
77
81
  * @param parent The parent application instance.
78
82
  */
79
83
  constructor(parent: Application);
84
+ /**
85
+ * Returns the current activity instance.
86
+ * @returns This activity.
87
+ */
88
+ getActivity(): Activity;
80
89
  /**
81
90
  * Checks if the activity is in the `Destroyed` state.
82
91
  * @returns `true` if destroyed, `false` otherwise.
@@ -200,17 +209,22 @@ export declare class Activity extends EventTarget {
200
209
  * await this.startActivity({ action: 'login' });
201
210
  * ```
202
211
  */
203
- startActivity(intent: Intent): Promise<void>;
212
+ startActivity(intent: Intent): Promise<Activity>;
204
213
  /**
205
214
  * Starts a new activity for a result.
215
+ * @template T The type of the result data expected.
206
216
  * @param intent The intent to start.
207
- * @returns The request code for the started activity.
217
+ * @returns A promise that resolves when the started activity finishes, returning the result Intent with
218
+ * data of type T.
208
219
  * @example
209
220
  * ```typescript
210
- * const code = await this.startActivityForResult({ action: 'pickFile' });
221
+ * const resultIntent = await this.startActivityForResult<{ userId: string }>({ action: 'pickUser' });
222
+ * if (resultIntent.resultCode === IntentResult.RESULT_OK) {
223
+ * console.log(resultIntent.data?.userId);
224
+ * }
211
225
  * ```
212
226
  */
213
- startActivityForResult(intent: Intent): Promise<number>;
227
+ startActivityForResult<T>(intent: Intent): Promise<ResolvedIntent<T | undefined>>;
214
228
  /**
215
229
  * Sets the result to be returned to the calling activity.
216
230
  * @param resultCode The result code.
@@ -221,33 +235,6 @@ export declare class Activity extends EventTarget {
221
235
  * ```
222
236
  */
223
237
  setResult(resultCode: IntentResult, data?: unknown): void;
224
- /**
225
- * Called when an activity you launched exits, giving you the request code, result code, and intent.
226
- * Override to handle results from started activities.
227
- * @param requestCode The request code.
228
- * @param resultCode The result code.
229
- * @param intent The intent that was used to start the activity.
230
- * @example
231
- * ```typescript
232
- * async onActivityResult(requestCode, resultCode, intent) {
233
- * if (resultCode === IntentResult.RESULT_OK) {
234
- * // handle result
235
- * }
236
- * }
237
- * ```
238
- */
239
- onActivityResult(requestCode: number, resultCode: IntentResult, intent: Intent): Promise<void>;
240
- /**
241
- * Returns the current activity instance.
242
- * @returns This activity.
243
- */
244
- getActivity(): Activity;
245
- /**
246
- * Checks if this activity initiated the activity for result with the given code.
247
- * @param code The request code.
248
- * @returns `true` if the code is pending, `false` otherwise.
249
- */
250
- hasRequestCode(code: number): boolean;
251
238
  /**
252
239
  * Gets the result data set by `setResult`.
253
240
  * @returns The result data or `undefined`.
@@ -1 +1 @@
1
- {"version":3,"file":"Activity.d.ts","sourceRoot":"","sources":["../../../src/core/Activity.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,KAAK,CAAA;AAC7C,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,KAAK,MAAM,EAAE,MAAM,sBAAsB,CAAA;AACnF,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;A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,CAAC;IAI7B;;;;;;;OAOG;IACH,aAAa,CAAC,IAAI,GAAE,aAAkB,GAAG,IAAI;IAQ7C;;;;;;;;;OASG;IACG,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAIjF;;;;OAIG;IACG,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlE;;;OAGG;IACG,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAO9C;;;OAGG;IACH,cAAc,IAAI,WAAW;IAI7B;;;;;;;OAOG;IACG,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAKtD;;;;;;;;;;;;;OAaG;IACG,sBAAsB,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAKvF;;;;;;;;OAQG;IACH,SAAS,CAAC,UAAU,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,IAAI;IAKzD;;;OAGG;IACH,SAAS,IAAI,OAAO,GAAG,SAAS;IAIhC;;;;;;;;;;;OAWG;IAEG,iBAAiB,CAAC,KAAK,EAAE,WAAW,CAAC,cAAc,GAAG,wBAAwB,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAWtG"}
@@ -83,8 +83,6 @@ let Activity = (() => {
83
83
  get resultCode() {
84
84
  return this.exitCode;
85
85
  }
86
- /** Tracks pending request codes for activities started for result. */
87
- pendingRequestCodes = [];
88
86
  /**
89
87
  * Constructs a new Activity.
90
88
  * @param parent The parent application instance.
@@ -94,6 +92,13 @@ let Activity = (() => {
94
92
  this.parent = parent;
95
93
  this.manager = new FragmentManager(this);
96
94
  }
95
+ /**
96
+ * Returns the current activity instance.
97
+ * @returns This activity.
98
+ */
99
+ getActivity() {
100
+ return this;
101
+ }
97
102
  /**
98
103
  * Checks if the activity is in the `Destroyed` state.
99
104
  * @returns `true` if destroyed, `false` otherwise.
@@ -261,24 +266,27 @@ let Activity = (() => {
261
266
  * await this.startActivity({ action: 'login' });
262
267
  * ```
263
268
  */
264
- startActivity(intent) {
265
- return this.parent.manager.startActivity(intent);
269
+ async startActivity(intent) {
270
+ const { manager } = this.getApplication();
271
+ return manager.startActivity(intent);
266
272
  }
267
273
  /**
268
274
  * Starts a new activity for a result.
275
+ * @template T The type of the result data expected.
269
276
  * @param intent The intent to start.
270
- * @returns The request code for the started activity.
277
+ * @returns A promise that resolves when the started activity finishes, returning the result Intent with
278
+ * data of type T.
271
279
  * @example
272
280
  * ```typescript
273
- * const code = await this.startActivityForResult({ action: 'pickFile' });
281
+ * const resultIntent = await this.startActivityForResult<{ userId: string }>({ action: 'pickUser' });
282
+ * if (resultIntent.resultCode === IntentResult.RESULT_OK) {
283
+ * console.log(resultIntent.data?.userId);
284
+ * }
274
285
  * ```
275
286
  */
276
287
  async startActivityForResult(intent) {
277
288
  const { manager } = this.getApplication();
278
- const code = manager.createRequestCode();
279
- this.pendingRequestCodes.push(code);
280
- await manager.startActivityForResult(intent, code);
281
- return code;
289
+ return manager.startActivityForResult(intent);
282
290
  }
283
291
  /**
284
292
  * Sets the result to be returned to the calling activity.
@@ -293,51 +301,6 @@ let Activity = (() => {
293
301
  this.exitCode = resultCode;
294
302
  this.resultData = data;
295
303
  }
296
- /**
297
- * Called when an activity you launched exits, giving you the request code, result code, and intent.
298
- * Override to handle results from started activities.
299
- * @param requestCode The request code.
300
- * @param resultCode The result code.
301
- * @param intent The intent that was used to start the activity.
302
- * @example
303
- * ```typescript
304
- * async onActivityResult(requestCode, resultCode, intent) {
305
- * if (resultCode === IntentResult.RESULT_OK) {
306
- * // handle result
307
- * }
308
- * }
309
- * ```
310
- */
311
- async onActivityResult(requestCode, resultCode, intent) {
312
- const index = this.pendingRequestCodes.indexOf(requestCode);
313
- if (index !== -1) {
314
- this.pendingRequestCodes.splice(index, 1);
315
- }
316
- // First we check whether any of the fragments has the code.
317
- const fragment = this.manager.findByRequestCode(requestCode);
318
- if (fragment) {
319
- return fragment.onActivityResult(requestCode, resultCode, intent);
320
- }
321
- if (this.constructor === Activity) {
322
- // eslint-disable-next-line no-console
323
- console.info(`Activity#onActivityResult not implemented. Request code: ${requestCode}, result code: ${resultCode}`, intent);
324
- }
325
- }
326
- /**
327
- * Returns the current activity instance.
328
- * @returns This activity.
329
- */
330
- getActivity() {
331
- return this;
332
- }
333
- /**
334
- * Checks if this activity initiated the activity for result with the given code.
335
- * @param code The request code.
336
- * @returns `true` if the code is pending, `false` otherwise.
337
- */
338
- hasRequestCode(code) {
339
- return this.pendingRequestCodes.includes(code);
340
- }
341
304
  /**
342
305
  * Gets the result data set by `setResult`.
343
306
  * @returns The result data or `undefined`.
@@ -360,7 +323,8 @@ let Activity = (() => {
360
323
  async handleIntentEvent(event) {
361
324
  if (event.type === EventTypes.Intent.startActivityForResult) {
362
325
  const info = event.detail;
363
- await this.startActivityForResult(info.intent);
326
+ const result = await this.startActivityForResult(info.intent);
327
+ info.onResult(result.result, result);
364
328
  }
365
329
  else if (event.type === EventTypes.Intent.startActivity) {
366
330
  await this.startActivity(event.detail.intent);
@@ -1 +1 @@
1
- {"version":3,"file":"Activity.js","sourceRoot":"","sources":["../../../src/core/Activity.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,OAAO,EAAE,OAAO,EAAkB,MAAM,KAAK,CAAA;AAC7C,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAe,MAAM,sBAAsB,CAAA;AACnF,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAGtD,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAEpD;;;;;;;;;;;;;;;;;;;;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H,KAAK,CAAC,iBAAiB,CAAC,KAA6D;YACnF,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU,CAAC,MAAM,CAAC,sBAAsB,EAAE,CAAC;gBAC5D,MAAM,IAAI,GAAG,KAAK,CAAC,MAAkC,CAAA;gBACrD,MAAM,IAAI,CAAC,sBAAsB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;YAChD,CAAC;iBAAM,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;gBAC1D,MAAM,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;YAC/C,CAAC;iBAAM,CAAC;gBACN,MAAM,IAAI,KAAK,CAAC,8BAA8B,KAAK,CAAC,IAAI,EAAE,CAAC,CAAA;YAC7D,CAAC;QACH,CAAC;;;SAlWU,QAAQ","sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\nimport { nothing, TemplateResult } from 'lit'\nimport { ActivityLifecycle, IntentResult, type Intent } from './ActivityManager.js'\nimport { FragmentManager } from './FragmentManager.js'\nimport type { Fragment } from './Fragment.js'\nimport type { Application, UpdateRequest } from './Application.js'\nimport { bound } from '../decorators/bound.js'\nimport type { ActivityDetail, ActivityWithResultDetail } from '../events/IntentEvents.js'\nimport { EventTypes } from '../events/EventTypes.js'\n\n/**\n * ## Activity\n *\n * `Activity` is a core building block of your application, representing a single,\n * focused operation that a user can perform.\n * Activities manage their own lifecycle, UI, and fragments, and communicate with the application\n * and other activities via intents.\n *\n * Activities are managed by the application and can be started, paused, resumed, stopped,\n * and destroyed according to the app's navigation and user actions.\n * They can also start other activities for results, manage fragments, and handle intent\n * events dispatched by hosted web components.\n *\n * ### Lifecycle Methods\n * - `onCreate(intent?)` - Called when the activity is first created.\n * - `onStart()` - Called when the activity becomes visible to the user.\n * - `onResume()` - Called when the activity gains focus and becomes interactive.\n * - `onPause()` - Called when the activity loses focus but is still visible.\n * - `onStop()` - Called when the activity is no longer visible.\n * - `onDestroy()` - Called before the activity is destroyed.\n * - `onRestart()` - Called after the activity has been stopped, just prior to it being started again.\n *\n * ### Example\n * ```typescript\n * class MyActivity extends Activity {\n * async onCreate(intent?: Intent) {\n * super.onCreate(intent);\n * // Initialization logic here\n * }\n *\n * render() {\n * return html`<h1>Hello from MyActivity!</h1>`;\n * }\n * }\n * ```\n */\nexport class Activity extends EventTarget {\n /**\n * The root element where the activity's content should be rendered.\n * Set this if you want to render outside the application's default render root (e.g., for modals).\n */\n renderRoot?: HTMLElement\n\n /**\n * An optional static action name for the activity.\n * Use this as the registration key for referencing the activity in intents.\n *\n * @example\n * ```typescript\n * class LoginActivity extends Activity {\n * static action = 'login';\n * }\n * ```\n */\n static action?: string\n\n /** The current lifecycle state of the activity. */\n public lifecycle: ActivityLifecycle = ActivityLifecycle.Initialized\n\n /** The parent application instance. */\n protected parent: Application\n\n /** The fragment manager for managing fragments within this activity. */\n protected manager: FragmentManager\n\n /** Data to be returned as a result to the calling activity. */\n protected resultData?: unknown\n\n /** The exit code for the activity result. */\n protected exitCode = IntentResult.RESULT_CANCELED\n\n /** Gets the current result code for the activity. */\n get resultCode(): IntentResult {\n return this.exitCode\n }\n\n /** Tracks pending request codes for activities started for result. */\n protected pendingRequestCodes: number[] = []\n\n /**\n * Constructs a new Activity.\n * @param parent The parent application instance.\n */\n constructor(parent: Application) {\n super()\n this.parent = parent\n this.manager = new FragmentManager(this)\n }\n\n /**\n * Checks if the activity is in the `Destroyed` state.\n * @returns `true` if destroyed, `false` otherwise.\n */\n isDestroyed(): boolean {\n return this.lifecycle === ActivityLifecycle.Destroyed\n }\n\n /**\n * Checks if the activity is in the `Resumed` state.\n * @returns `true` if resumed, `false` otherwise.\n */\n isResumed(): boolean {\n return this.lifecycle === ActivityLifecycle.Resumed\n }\n\n /**\n * Called when the activity is starting. Override to perform initialization logic.\n * @param intent Optional intent data.\n * @example\n * ```typescript\n * async onCreate(intent?: Intent) {\n * await fetchData();\n * }\n * ```\n */\n onCreate(intent?: Intent): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:create', { detail: intent }))\n }\n\n /**\n * Called when the activity becomes visible to the user.\n * Override to start animations or prepare UI.\n */\n onStart(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:start', { detail: null }))\n }\n\n /**\n * Called when the activity gains focus and becomes interactive.\n * Override to start input handling or resume tasks.\n */\n onResume(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:resume', { detail: null }))\n }\n\n /**\n * Called when the activity loses focus but is still visible.\n * Override to pause ongoing tasks or save state.\n */\n onPause(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:pause', { detail: null }))\n }\n\n /**\n * Called when the activity is no longer visible to the user.\n * Override to stop UI updates or animations.\n */\n onStop(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:stop', { detail: null }))\n }\n\n /**\n * Called after the activity has been stopped, just prior to it being started again.\n */\n onRestart(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:restart', { detail: null }))\n }\n\n /**\n * Called before the activity is destroyed. Override to clean up resources.\n */\n onDestroy(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:destroy', { detail: null }))\n this.manager.onDestroy()\n }\n\n /**\n * Called when a new intent is delivered to the activity.\n * Override to handle new intents.\n * @param intent New intent data.\n */\n onNewIntent(intent: Intent): void | Promise<void> {\n // Override in subclass if needed\n }\n\n /**\n * Called by the renderer when the activity is rendered for the first time.\n * Override to perform actions after first render.\n */\n onFirstRender(): void {\n // Override in subclass if needed\n }\n\n /**\n * Renders the activity's view. Override to provide UI.\n * @returns A Lit TemplateResult or `nothing` if nothing should be rendered.\n * @example\n * ```typescript\n * render() {\n * return html`<div>Hello, world!</div>`;\n * }\n * ```\n */\n render(): TemplateResult | typeof nothing {\n return nothing\n }\n\n /**\n * Finishes the activity and notifies the application.\n * @example\n * ```typescript\n * await this.finish();\n * ```\n */\n async finish(): Promise<void> {\n await this.getApplication().manager.finishActivity(this)\n }\n\n /**\n * Requests the parent application to update the UI.\n * @param opts Update options.\n * @example\n * ```typescript\n * this.requestUpdate();\n * ```\n */\n requestUpdate(opts: UpdateRequest = {}): void {\n const { activity = true } = opts\n if (activity) {\n this.manager.updateActiveFragments()\n }\n this.parent.requestUpdate(opts)\n }\n\n /**\n * Registers a new fragment with this activity.\n * @param key The fragment's key.\n * @param fragment The fragment instance.\n * @param data Optional data to pass to the fragment.\n * @example\n * ```typescript\n * await this.addFragment('profile', new ProfileFragment());\n * ```\n */\n async addFragment(key: string, fragment: Fragment, data?: unknown): Promise<void> {\n await this.manager.attachFragment(key, fragment, this, data)\n }\n\n /**\n * Shows a registered fragment.\n * @param key The fragment's key.\n * @param root Optional root element to render into.\n */\n async showFragment(key: string, root?: HTMLElement): Promise<void> {\n await this.manager.showFragment(key, root)\n }\n\n /**\n * Hides a registered fragment.\n * @param key The fragment's key.\n */\n async hideFragment(key: string): Promise<void> {\n const fragment = this.manager.findFragment(key)\n if (fragment) {\n await this.manager.hideFragment(fragment)\n }\n }\n\n /**\n * Returns the parent application instance.\n * @returns The application.\n */\n getApplication(): Application {\n return this.parent\n }\n\n /**\n * Starts a new activity.\n * @param intent The intent to start.\n * @example\n * ```typescript\n * await this.startActivity({ action: 'login' });\n * ```\n */\n startActivity(intent: Intent): Promise<void> {\n return this.parent.manager.startActivity(intent)\n }\n\n /**\n * Starts a new activity for a result.\n * @param intent The intent to start.\n * @returns The request code for the started activity.\n * @example\n * ```typescript\n * const code = await this.startActivityForResult({ action: 'pickFile' });\n * ```\n */\n async startActivityForResult(intent: Intent): Promise<number> {\n const { manager } = this.getApplication()\n const code = manager.createRequestCode()\n this.pendingRequestCodes.push(code)\n await manager.startActivityForResult(intent, code)\n return code\n }\n\n /**\n * Sets the result to be returned to the calling activity.\n * @param resultCode The result code.\n * @param data Optional result data.\n * @example\n * ```typescript\n * this.setResult(IntentResult.RESULT_OK, { userId: 123 });\n * ```\n */\n setResult(resultCode: IntentResult, data?: unknown): void {\n this.exitCode = resultCode\n this.resultData = data\n }\n\n /**\n * Called when an activity you launched exits, giving you the request code, result code, and intent.\n * Override to handle results from started activities.\n * @param requestCode The request code.\n * @param resultCode The result code.\n * @param intent The intent that was used to start the activity.\n * @example\n * ```typescript\n * async onActivityResult(requestCode, resultCode, intent) {\n * if (resultCode === IntentResult.RESULT_OK) {\n * // handle result\n * }\n * }\n * ```\n */\n async onActivityResult(requestCode: number, resultCode: IntentResult, intent: Intent): Promise<void> {\n const index = this.pendingRequestCodes.indexOf(requestCode)\n if (index !== -1) {\n this.pendingRequestCodes.splice(index, 1)\n }\n // First we check whether any of the fragments has the code.\n const fragment = this.manager.findByRequestCode(requestCode)\n if (fragment) {\n return fragment.onActivityResult(requestCode, resultCode, intent)\n }\n if (this.constructor === Activity) {\n // eslint-disable-next-line no-console\n console.info(\n `Activity#onActivityResult not implemented. Request code: ${requestCode}, result code: ${resultCode}`,\n intent\n )\n }\n }\n\n /**\n * Returns the current activity instance.\n * @returns This activity.\n */\n getActivity(): Activity {\n return this\n }\n\n /**\n * Checks if this activity initiated the activity for result with the given code.\n * @param code The request code.\n * @returns `true` if the code is pending, `false` otherwise.\n */\n hasRequestCode(code: number): boolean {\n return this.pendingRequestCodes.includes(code)\n }\n\n /**\n * Gets the result data set by `setResult`.\n * @returns The result data or `undefined`.\n */\n getResult(): unknown | undefined {\n return this.resultData\n }\n\n /**\n * Handles intent events dispatched by web components hosted by this activity.\n *\n * Usage example:\n * ```html\n * <custom-element @startactivity=\"${this.handleIntentEvent}\"></custom-element>\n * <custom-element @startactivityforresult=\"${this.handleIntentEvent}\"></custom-element>\n * ```\n *\n * @param event The intent event.\n * @throws Error if the event type is not recognized.\n */\n @bound\n async handleIntentEvent(event: CustomEvent<ActivityDetail | ActivityWithResultDetail>): Promise<void> {\n if (event.type === EventTypes.Intent.startActivityForResult) {\n const info = event.detail as ActivityWithResultDetail\n await this.startActivityForResult(info.intent)\n } else if (event.type === EventTypes.Intent.startActivity) {\n await this.startActivity(event.detail.intent)\n } else {\n throw new Error(`Unrecognized intent event: ${event.type}`)\n }\n }\n}\n"]}
1
+ {"version":3,"file":"Activity.js","sourceRoot":"","sources":["../../../src/core/Activity.ts"],"names":[],"mappings":";AAAA,sDAAsD;AACtD,OAAO,EAAE,OAAO,EAAkB,MAAM,KAAK,CAAA;AAC7C,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAoC,MAAM,sBAAsB,CAAA;AACxG,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAGtD,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AASpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;IACU,QAAQ;sBAAS,WAAW;;;iBAA5B,QAAS,SAAQ,WAAW;;;6CA6StC,KAAK;YACN,sMAAM,iBAAiB,6DAUtB;;;QAvTD;;;WAGG;QACH,UAAU,GALC,mDAAQ,CAKK;QAExB;;;;;;;;;;WAUG;QACH,MAAM,CAAC,MAAM,CAAS;QAEtB,mDAAmD;QAC5C,SAAS,GAAsB,iBAAiB,CAAC,WAAW,CAAA;QAEnE,uCAAuC;QAC7B,MAAM,CAAa;QAE7B,wEAAwE;QAC9D,OAAO,CAAiB;QAElC,+DAA+D;QACrD,UAAU,CAAU;QAE9B,6CAA6C;QACnC,QAAQ,GAAG,YAAY,CAAC,eAAe,CAAA;QAEjD,qDAAqD;QACrD,IAAI,UAAU;YACZ,OAAO,IAAI,CAAC,QAAQ,CAAA;QACtB,CAAC;QAED;;;WAGG;QACH,YAAY,MAAmB;YAC7B,KAAK,EAAE,CAAA;YACP,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;YACpB,IAAI,CAAC,OAAO,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,CAAA;QAC1C,CAAC;QAED;;;WAGG;QACH,WAAW;YACT,OAAO,IAAI,CAAA;QACb,CA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AC/C,CAAC;iBAAM,CAAC;gBACN,MAAM,IAAI,KAAK,CAAC,8BAA8B,KAAK,CAAC,IAAI,EAAE,CAAC,CAAA;YAC7D,CAAC;QACH,CAAC;;;SAxTU,QAAQ","sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\nimport { nothing, TemplateResult } from 'lit'\nimport { ActivityLifecycle, IntentResult, type Intent, type ResolvedIntent } from './ActivityManager.js'\nimport { FragmentManager } from './FragmentManager.js'\nimport type { Fragment } from './Fragment.js'\nimport type { Application, UpdateRequest } from './Application.js'\nimport { bound } from '../decorators/bound.js'\nimport type { ActivityDetail, ActivityWithResultDetail } from '../events/IntentEvents.js'\nimport { EventTypes } from '../events/EventTypes.js'\n\nexport interface ActivityResult {\n requestCode: number\n resultCode: IntentResult\n data?: unknown\n intent: Intent\n}\n\n/**\n * ## Activity\n *\n * `Activity` is a core building block of your application, representing a single,\n * focused operation that a user can perform.\n * Activities manage their own lifecycle, UI, and fragments, and communicate with the application\n * and other activities via intents.\n *\n * Activities are managed by the application and can be started, paused, resumed, stopped,\n * and destroyed according to the app's navigation and user actions.\n * They can also start other activities for results, manage fragments, and handle intent\n * events dispatched by hosted web components.\n *\n * ### Lifecycle Methods\n * - `onCreate(intent?)` - Called when the activity is first created.\n * - `onStart()` - Called when the activity becomes visible to the user.\n * - `onResume()` - Called when the activity gains focus and becomes interactive.\n * - `onPause()` - Called when the activity loses focus but is still visible.\n * - `onStop()` - Called when the activity is no longer visible.\n * - `onDestroy()` - Called before the activity is destroyed.\n * - `onRestart()` - Called after the activity has been stopped, just prior to it being started again.\n *\n * ### Example\n * ```typescript\n * class MyActivity extends Activity {\n * async onCreate(intent?: Intent) {\n * super.onCreate(intent);\n * // Initialization logic here\n * }\n *\n * render() {\n * return html`<h1>Hello from MyActivity!</h1>`;\n * }\n * }\n * ```\n */\nexport class Activity extends EventTarget {\n /**\n * The root element where the activity's content should be rendered.\n * Set this if you want to render outside the application's default render root (e.g., for modals).\n */\n renderRoot?: HTMLElement\n\n /**\n * An optional static action name for the activity.\n * Use this as the registration key for referencing the activity in intents.\n *\n * @example\n * ```typescript\n * class LoginActivity extends Activity {\n * static action = 'login';\n * }\n * ```\n */\n static action?: string\n\n /** The current lifecycle state of the activity. */\n public lifecycle: ActivityLifecycle = ActivityLifecycle.Initialized\n\n /** The parent application instance. */\n protected parent: Application\n\n /** The fragment manager for managing fragments within this activity. */\n protected manager: FragmentManager\n\n /** Data to be returned as a result to the calling activity. */\n protected resultData?: unknown\n\n /** The exit code for the activity result. */\n protected exitCode = IntentResult.RESULT_CANCELED\n\n /** Gets the current result code for the activity. */\n get resultCode(): IntentResult {\n return this.exitCode\n }\n\n /**\n * Constructs a new Activity.\n * @param parent The parent application instance.\n */\n constructor(parent: Application) {\n super()\n this.parent = parent\n this.manager = new FragmentManager(this)\n }\n\n /**\n * Returns the current activity instance.\n * @returns This activity.\n */\n getActivity(): Activity {\n return this\n }\n\n /**\n * Checks if the activity is in the `Destroyed` state.\n * @returns `true` if destroyed, `false` otherwise.\n */\n isDestroyed(): boolean {\n return this.lifecycle === ActivityLifecycle.Destroyed\n }\n\n /**\n * Checks if the activity is in the `Resumed` state.\n * @returns `true` if resumed, `false` otherwise.\n */\n isResumed(): boolean {\n return this.lifecycle === ActivityLifecycle.Resumed\n }\n\n /**\n * Called when the activity is starting. Override to perform initialization logic.\n * @param intent Optional intent data.\n * @example\n * ```typescript\n * async onCreate(intent?: Intent) {\n * await fetchData();\n * }\n * ```\n */\n onCreate(intent?: Intent): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:create', { detail: intent }))\n }\n\n /**\n * Called when the activity becomes visible to the user.\n * Override to start animations or prepare UI.\n */\n onStart(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:start', { detail: null }))\n }\n\n /**\n * Called when the activity gains focus and becomes interactive.\n * Override to start input handling or resume tasks.\n */\n onResume(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:resume', { detail: null }))\n }\n\n /**\n * Called when the activity loses focus but is still visible.\n * Override to pause ongoing tasks or save state.\n */\n onPause(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:pause', { detail: null }))\n }\n\n /**\n * Called when the activity is no longer visible to the user.\n * Override to stop UI updates or animations.\n */\n onStop(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:stop', { detail: null }))\n }\n\n /**\n * Called after the activity has been stopped, just prior to it being started again.\n */\n onRestart(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:restart', { detail: null }))\n }\n\n /**\n * Called before the activity is destroyed. Override to clean up resources.\n */\n onDestroy(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:destroy', { detail: null }))\n this.manager.onDestroy()\n }\n\n /**\n * Called when a new intent is delivered to the activity.\n * Override to handle new intents.\n * @param intent New intent data.\n */\n onNewIntent(intent: Intent): void | Promise<void> {\n // Override in subclass if needed\n }\n\n /**\n * Called by the renderer when the activity is rendered for the first time.\n * Override to perform actions after first render.\n */\n onFirstRender(): void {\n // Override in subclass if needed\n }\n\n /**\n * Renders the activity's view. Override to provide UI.\n * @returns A Lit TemplateResult or `nothing` if nothing should be rendered.\n * @example\n * ```typescript\n * render() {\n * return html`<div>Hello, world!</div>`;\n * }\n * ```\n */\n render(): TemplateResult | typeof nothing {\n return nothing\n }\n\n /**\n * Finishes the activity and notifies the application.\n * @example\n * ```typescript\n * await this.finish();\n * ```\n */\n async finish(): Promise<void> {\n await this.getApplication().manager.finishActivity(this)\n }\n\n /**\n * Requests the parent application to update the UI.\n * @param opts Update options.\n * @example\n * ```typescript\n * this.requestUpdate();\n * ```\n */\n requestUpdate(opts: UpdateRequest = {}): void {\n const { activity = true } = opts\n if (activity) {\n this.manager.updateActiveFragments()\n }\n this.parent.requestUpdate(opts)\n }\n\n /**\n * Registers a new fragment with this activity.\n * @param key The fragment's key.\n * @param fragment The fragment instance.\n * @param data Optional data to pass to the fragment.\n * @example\n * ```typescript\n * await this.addFragment('profile', new ProfileFragment());\n * ```\n */\n async addFragment(key: string, fragment: Fragment, data?: unknown): Promise<void> {\n await this.manager.attachFragment(key, fragment, this, data)\n }\n\n /**\n * Shows a registered fragment.\n * @param key The fragment's key.\n * @param root Optional root element to render into.\n */\n async showFragment(key: string, root?: HTMLElement): Promise<void> {\n await this.manager.showFragment(key, root)\n }\n\n /**\n * Hides a registered fragment.\n * @param key The fragment's key.\n */\n async hideFragment(key: string): Promise<void> {\n const fragment = this.manager.findFragment(key)\n if (fragment) {\n await this.manager.hideFragment(fragment)\n }\n }\n\n /**\n * Returns the parent application instance.\n * @returns The application.\n */\n getApplication(): Application {\n return this.parent\n }\n\n /**\n * Starts a new activity.\n * @param intent The intent to start.\n * @example\n * ```typescript\n * await this.startActivity({ action: 'login' });\n * ```\n */\n async startActivity(intent: Intent): Promise<Activity> {\n const { manager } = this.getApplication()\n return manager.startActivity(intent)\n }\n\n /**\n * Starts a new activity for a result.\n * @template T The type of the result data expected.\n * @param intent The intent to start.\n * @returns A promise that resolves when the started activity finishes, returning the result Intent with\n * data of type T.\n * @example\n * ```typescript\n * const resultIntent = await this.startActivityForResult<{ userId: string }>({ action: 'pickUser' });\n * if (resultIntent.resultCode === IntentResult.RESULT_OK) {\n * console.log(resultIntent.data?.userId);\n * }\n * ```\n */\n async startActivityForResult<T>(intent: Intent): Promise<ResolvedIntent<T | undefined>> {\n const { manager } = this.getApplication()\n return manager.startActivityForResult(intent)\n }\n\n /**\n * Sets the result to be returned to the calling activity.\n * @param resultCode The result code.\n * @param data Optional result data.\n * @example\n * ```typescript\n * this.setResult(IntentResult.RESULT_OK, { userId: 123 });\n * ```\n */\n setResult(resultCode: IntentResult, data?: unknown): void {\n this.exitCode = resultCode\n this.resultData = data\n }\n\n /**\n * Gets the result data set by `setResult`.\n * @returns The result data or `undefined`.\n */\n getResult(): unknown | undefined {\n return this.resultData\n }\n\n /**\n * Handles intent events dispatched by web components hosted by this activity.\n *\n * Usage example:\n * ```html\n * <custom-element @startactivity=\"${this.handleIntentEvent}\"></custom-element>\n * <custom-element @startactivityforresult=\"${this.handleIntentEvent}\"></custom-element>\n * ```\n *\n * @param event The intent event.\n * @throws Error if the event type is not recognized.\n */\n @bound\n async handleIntentEvent(event: CustomEvent<ActivityDetail | ActivityWithResultDetail>): Promise<void> {\n if (event.type === EventTypes.Intent.startActivityForResult) {\n const info = event.detail as ActivityWithResultDetail\n const result = await this.startActivityForResult(info.intent)\n info.onResult(result.result, result)\n } else if (event.type === EventTypes.Intent.startActivity) {\n await this.startActivity(event.detail.intent)\n } else {\n throw new Error(`Unrecognized intent event: ${event.type}`)\n }\n }\n}\n"]}
@@ -55,15 +55,38 @@ export declare enum IntentFlags {
55
55
  ByReference = 4
56
56
  }
57
57
  export interface Intent<T = unknown> {
58
+ /**
59
+ * The action name the activity is registered for.
60
+ */
58
61
  action: string;
62
+ /**
63
+ * The data passed to the activity.
64
+ */
59
65
  data?: T;
66
+ /**
67
+ * The category of the activity.
68
+ * Optional and currently not used.
69
+ */
60
70
  category?: string[];
71
+ /**
72
+ * The flags that control the behavior of the activity.
73
+ */
61
74
  flags?: number;
62
75
  /**
63
76
  * The request code used to distinguish between different activities.
64
77
  */
65
78
  requestCode?: number;
66
79
  }
80
+ /**
81
+ * An intent returned by an activity after the activity result is ready.
82
+ * This object is only created when the activity is started with the `ForResult` flag.
83
+ */
84
+ export interface ResolvedIntent<T = unknown> extends Intent<T> {
85
+ /**
86
+ * The result code returned by the activity.
87
+ */
88
+ result: IntentResult;
89
+ }
67
90
  export declare enum IntentResult {
68
91
  RESULT_CANCELED = 0,
69
92
  RESULT_OK = 1
@@ -101,6 +124,10 @@ export declare class ActivityManager {
101
124
  * Stores activity *classes* by unique ID (action name).
102
125
  */
103
126
  private activityClasses;
127
+ /**
128
+ * Stores the resolvers for activities started for result.
129
+ */
130
+ private resultResolvers;
104
131
  /**
105
132
  * Represents the activity stack. It is used to manage
106
133
  * the UI state after calling the back action.
@@ -131,27 +158,35 @@ export declare class ActivityManager {
131
158
  * @param intent The intent that created this activity.
132
159
  */
133
160
  createActivity(intent: Intent): Promise<void>;
134
- setupRoute(intent: Intent): void;
135
161
  /**
136
162
  * Starts a new activity and brings it to the foreground.
137
163
  *
138
164
  * @param intent The intent that created this activity.
139
165
  */
140
- startActivity(intent: Intent): Promise<void>;
166
+ startActivity(intent: Intent): Promise<Activity>;
141
167
  private buildActivity;
142
168
  /**
143
169
  * Starts an activity that should return a result to the calling activity.
144
- * The result should be a unique number across the application.
170
+ *
171
+ * @template T The type of the result data expected.
145
172
  * @param intent The intent to start the activity.
146
- * @param requestCode The request code used to match the activity result.
147
- */
148
- startActivityForResult(intent: Intent, requestCode: number): Promise<void>;
173
+ * @returns A promise that resolves when the started activity finishes, returning the result Intent with
174
+ * data of type T.
175
+ * @example
176
+ * ```typescript
177
+ * const resultIntent = await mgr.startActivityForResult<{ userId: string }>({ action: 'pickUser' });
178
+ * if (resultIntent.resultCode === IntentResult.RESULT_OK) {
179
+ * console.log(resultIntent.data?.userId);
180
+ * }
181
+ * ```
182
+ */
183
+ startActivityForResult<T>(intent: Intent): Promise<ResolvedIntent<T | undefined>>;
149
184
  isDestroyed(activity: Activity): boolean;
150
185
  /**
151
186
  * @param activity The activity to finish.
152
187
  */
153
188
  finishActivity(activity: Activity): Promise<void>;
154
- private manageActivityResult;
189
+ private resolveActivityResult;
155
190
  private bringLastActivityToFront;
156
191
  getTopActivity(): Activity | undefined;
157
192
  getCurrentActivity(): Activity | undefined;
@@ -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;;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M,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"}
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;A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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"}
@@ -1,4 +1,4 @@
1
- import { navigateScreen } from './ApplicationRoute.js';
1
+ // import { navigateScreen } from './ApplicationRoute.js'
2
2
  export var ActivityLifecycle;
3
3
  (function (ActivityLifecycle) {
4
4
  /**
@@ -76,6 +76,10 @@ export class ActivityManager {
76
76
  * Stores activity *classes* by unique ID (action name).
77
77
  */
78
78
  activityClasses = new Map();
79
+ /**
80
+ * Stores the resolvers for activities started for result.
81
+ */
82
+ resultResolvers = new Map();
79
83
  /**
80
84
  * Represents the activity stack. It is used to manage
81
85
  * the UI state after calling the back action.
@@ -113,14 +117,6 @@ export class ActivityManager {
113
117
  }
114
118
  this.activityClasses.set(action, activityClass);
115
119
  }
116
- // protected reflectEvent<T extends Event>(event: T): T {
117
- // const copy = Reflect.construct(event.constructor, [event.type, event])
118
- // const dispatched = this.#parent.events.dispatchEvent(copy)
119
- // if (!dispatched) {
120
- // event.preventDefault()
121
- // }
122
- // return copy
123
- // }
124
120
  /**
125
121
  * Creates an activity and pushes it into the stack, but it does not initialize any of the lifecycle methods.
126
122
  * It is a way to put an activity at some place of the stack,
@@ -137,14 +133,14 @@ export class ActivityManager {
137
133
  const stackEntry = { activity, id: activityId, intent, action: intent.action };
138
134
  this.activityStack.push(stackEntry);
139
135
  }
140
- setupRoute(intent) {
141
- if (intent.data) {
142
- const typed = intent.data;
143
- if (typed.uri) {
144
- navigateScreen(typed.uri);
145
- }
146
- }
147
- }
136
+ // setupRoute(intent: Intent): void {
137
+ // if (intent.data) {
138
+ // const typed = intent.data as { uri?: string }
139
+ // if (typed.uri) {
140
+ // navigateScreen(typed.uri)
141
+ // }
142
+ // }
143
+ // }
148
144
  /**
149
145
  * Starts a new activity and brings it to the foreground.
150
146
  *
@@ -165,9 +161,9 @@ export class ActivityManager {
165
161
  });
166
162
  await info.activity.onNewIntent(intent);
167
163
  // TODO: Check if the activity is destroyed.
168
- this.setupRoute(intent);
164
+ // this.setupRoute(intent)
169
165
  await this.updateCurrentActivity(info.activity, false);
170
- return;
166
+ return info.activity;
171
167
  }
172
168
  const activity = this.buildActivity(intent);
173
169
  if (currentActivity) {
@@ -189,15 +185,16 @@ export class ActivityManager {
189
185
  await activity.onCreate(intent);
190
186
  if (this.isDestroyed(activity)) {
191
187
  // the activity finished and the manager already took care of this situation.
192
- return;
188
+ return activity;
193
189
  }
194
190
  activity.lifecycle = ActivityLifecycle.Created;
195
191
  await this.updateCurrentActivity(activity, !!isModal);
196
192
  if (this.isDestroyed(activity)) {
197
193
  // the activity finished and the manager already took care of this situation.
198
- return;
194
+ return activity;
199
195
  }
200
- this.setupRoute(intent);
196
+ // this.setupRoute(intent)
197
+ return activity;
201
198
  }
202
199
  buildActivity(intent) {
203
200
  const action = intent.action;
@@ -214,12 +211,20 @@ export class ActivityManager {
214
211
  }
215
212
  /**
216
213
  * Starts an activity that should return a result to the calling activity.
217
- * The result should be a unique number across the application.
214
+ *
215
+ * @template T The type of the result data expected.
218
216
  * @param intent The intent to start the activity.
219
- * @param requestCode The request code used to match the activity result.
220
- */
221
- async startActivityForResult(intent, requestCode) {
222
- intent.requestCode = requestCode;
217
+ * @returns A promise that resolves when the started activity finishes, returning the result Intent with
218
+ * data of type T.
219
+ * @example
220
+ * ```typescript
221
+ * const resultIntent = await mgr.startActivityForResult<{ userId: string }>({ action: 'pickUser' });
222
+ * if (resultIntent.resultCode === IntentResult.RESULT_OK) {
223
+ * console.log(resultIntent.data?.userId);
224
+ * }
225
+ * ```
226
+ */
227
+ async startActivityForResult(intent) {
223
228
  const byReference = (intent.flags ?? 0) & IntentFlags.ByReference;
224
229
  const shallowCopy = { ...intent };
225
230
  const data = shallowCopy.data;
@@ -236,7 +241,13 @@ export class ActivityManager {
236
241
  else {
237
242
  deepCopy.flags = IntentFlags.ForResult;
238
243
  }
239
- await this.startActivity(deepCopy);
244
+ return new Promise((resolve, reject) => {
245
+ this.resultResolvers.set(deepCopy, resolve);
246
+ this.startActivity(deepCopy).catch((e) => {
247
+ this.resultResolvers.delete(deepCopy);
248
+ reject(e);
249
+ });
250
+ });
240
251
  }
241
252
  isDestroyed(activity) {
242
253
  return activity.lifecycle === ActivityLifecycle.Destroyed;
@@ -259,7 +270,7 @@ export class ActivityManager {
259
270
  stackEntry.activity.lifecycle = ActivityLifecycle.Destroyed;
260
271
  // Clean up resources to prevent memory leaks
261
272
  this.cleanupActivity(stackEntry.activity);
262
- await this.manageActivityResult(stackEntry.activity, stackEntry.intent);
273
+ this.resolveActivityResult(stackEntry.activity, stackEntry.intent);
263
274
  // Resume the previous activity
264
275
  await this.bringLastActivityToFront();
265
276
  }
@@ -270,31 +281,28 @@ export class ActivityManager {
270
281
  }
271
282
  await activity.onDestroy();
272
283
  activity.lifecycle = ActivityLifecycle.Destroyed;
284
+ this.resolveActivityResult(activity, { action: '' });
273
285
  // This can happen when an activity finishes in one of the callback methods,
274
286
  // before it is added to the stack. In that case, we bring the last activity back to the front.
275
287
  await this.bringLastActivityToFront();
276
288
  }
277
289
  this.#parent.requestUpdate();
278
290
  }
279
- async manageActivityResult(activity, intent) {
280
- const { flags = 0, requestCode = -1 } = intent;
281
- if (flags & IntentFlags.ForResult) {
282
- const all = [...this.activityStack, ...this.modalActivityStack];
283
- const target = all.find((entry) => entry.activity.hasRequestCode(requestCode));
284
- if (target) {
285
- if (target.activity.lifecycle === ActivityLifecycle.Destroyed) {
286
- return;
287
- }
288
- if (target.activity.lifecycle === ActivityLifecycle.Resumed) {
289
- await target.activity.onPause();
290
- target.activity.lifecycle = ActivityLifecycle.Paused;
291
- }
292
- // Let's create a shallow copy of the intent. We can disregard the ByReference flag here,
293
- // as we override the data with the result data.
294
- const intentCopy = { ...intent };
295
- intentCopy.data = activity.getResult();
296
- await target.activity.onActivityResult(requestCode, activity.resultCode, intentCopy);
297
- }
291
+ resolveActivityResult(activity, originalIntent) {
292
+ const resolver = this.resultResolvers.get(originalIntent);
293
+ // The activity might have not been started with startActivityForResult.
294
+ // A thing to consider for the future is whether we should throw an error in a case where
295
+ // the activity was started for result but the resolver was not found.
296
+ if (resolver) {
297
+ this.resultResolvers.delete(originalIntent);
298
+ // We construct the result intent.
299
+ // Ideally we would like to preserve the original intent structure but with new data.
300
+ const resultIntent = {
301
+ ...originalIntent,
302
+ data: activity.getResult(),
303
+ result: activity.resultCode,
304
+ };
305
+ resolver(resultIntent);
298
306
  }
299
307
  }
300
308
  async bringLastActivityToFront() {