@api-client/ui 0.6.5 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@api-client/ui",
3
- "version": "0.6.5",
3
+ "version": "0.6.6",
4
4
  "description": "Internal UI component library for the API Client ecosystem.",
5
5
  "license": "UNLICENSED",
6
6
  "main": "build/src/index.js",
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
2
  import { nothing, TemplateResult } from 'lit'
3
- import { ActivityLifecycle, IntentResult, type Intent } from './ActivityManager.js'
3
+ import { ActivityLifecycle, IntentResult, type Intent, type ResolvedIntent } from './ActivityManager.js'
4
4
  import { FragmentManager } from './FragmentManager.js'
5
5
  import type { Fragment } from './Fragment.js'
6
6
  import type { Application, UpdateRequest } from './Application.js'
@@ -301,17 +301,19 @@ export class Activity extends EventTarget {
301
301
 
302
302
  /**
303
303
  * Starts a new activity for a result.
304
+ * @template T The type of the result data expected.
304
305
  * @param intent The intent to start.
305
- * @returns A promise that resolves when the started activity finishes, returning the result Intent.
306
+ * @returns A promise that resolves when the started activity finishes, returning the result Intent with
307
+ * data of type T.
306
308
  * @example
307
309
  * ```typescript
308
- * const resultIntent = await this.startActivityForResult({ action: 'pickFile' });
310
+ * const resultIntent = await this.startActivityForResult<{ userId: string }>({ action: 'pickUser' });
309
311
  * if (resultIntent.resultCode === IntentResult.RESULT_OK) {
310
- * // handle result
312
+ * console.log(resultIntent.data?.userId);
311
313
  * }
312
314
  * ```
313
315
  */
314
- async startActivityForResult(intent: Intent): Promise<Intent> {
316
+ async startActivityForResult<T>(intent: Intent): Promise<ResolvedIntent<T | undefined>> {
315
317
  const { manager } = this.getApplication()
316
318
  return manager.startActivityForResult(intent)
317
319
  }
@@ -355,7 +357,7 @@ export class Activity extends EventTarget {
355
357
  if (event.type === EventTypes.Intent.startActivityForResult) {
356
358
  const info = event.detail as ActivityWithResultDetail
357
359
  const result = await this.startActivityForResult(info.intent)
358
- info.onResult(IntentResult.RESULT_OK, result)
360
+ info.onResult(result.result, result)
359
361
  } else if (event.type === EventTypes.Intent.startActivity) {
360
362
  await this.startActivity(event.detail.intent)
361
363
  } else {
@@ -59,9 +59,22 @@ export enum IntentFlags {
59
59
  }
60
60
 
61
61
  export interface Intent<T = unknown> {
62
+ /**
63
+ * The action name the activity is registered for.
64
+ */
62
65
  action: string
66
+ /**
67
+ * The data passed to the activity.
68
+ */
63
69
  data?: T
70
+ /**
71
+ * The category of the activity.
72
+ * Optional and currently not used.
73
+ */
64
74
  category?: string[]
75
+ /**
76
+ * The flags that control the behavior of the activity.
77
+ */
65
78
  flags?: number
66
79
  /**
67
80
  * The request code used to distinguish between different activities.
@@ -69,6 +82,17 @@ export interface Intent<T = unknown> {
69
82
  requestCode?: number
70
83
  }
71
84
 
85
+ /**
86
+ * An intent returned by an activity after the activity result is ready.
87
+ * This object is only created when the activity is started with the `ForResult` flag.
88
+ */
89
+ export interface ResolvedIntent<T = unknown> extends Intent<T> {
90
+ /**
91
+ * The result code returned by the activity.
92
+ */
93
+ result: IntentResult
94
+ }
95
+
72
96
  export enum IntentResult {
73
97
  RESULT_CANCELED,
74
98
  RESULT_OK,
@@ -137,7 +161,7 @@ export class ActivityManager {
137
161
  /**
138
162
  * Stores the resolvers for activities started for result.
139
163
  */
140
- private resultResolvers = new Map<Intent, (intent: Intent) => void>()
164
+ private resultResolvers = new Map<Intent, (intent: ResolvedIntent) => void>()
141
165
 
142
166
  /**
143
167
  * Represents the activity stack. It is used to manage
@@ -291,9 +315,19 @@ export class ActivityManager {
291
315
  /**
292
316
  * Starts an activity that should return a result to the calling activity.
293
317
  *
318
+ * @template T The type of the result data expected.
294
319
  * @param intent The intent to start the activity.
295
- */
296
- async startActivityForResult(intent: Intent): Promise<Intent> {
320
+ * @returns A promise that resolves when the started activity finishes, returning the result Intent with
321
+ * data of type T.
322
+ * @example
323
+ * ```typescript
324
+ * const resultIntent = await mgr.startActivityForResult<{ userId: string }>({ action: 'pickUser' });
325
+ * if (resultIntent.resultCode === IntentResult.RESULT_OK) {
326
+ * console.log(resultIntent.data?.userId);
327
+ * }
328
+ * ```
329
+ */
330
+ async startActivityForResult<T>(intent: Intent): Promise<ResolvedIntent<T | undefined>> {
297
331
  const byReference = (intent.flags ?? 0) & IntentFlags.ByReference
298
332
  const shallowCopy = { ...intent }
299
333
  const data = shallowCopy.data
@@ -310,8 +344,8 @@ export class ActivityManager {
310
344
  deepCopy.flags = IntentFlags.ForResult
311
345
  }
312
346
 
313
- return new Promise<Intent>((resolve, reject) => {
314
- this.resultResolvers.set(deepCopy, resolve)
347
+ return new Promise<ResolvedIntent<T>>((resolve, reject) => {
348
+ this.resultResolvers.set(deepCopy, resolve as (intent: ResolvedIntent<unknown>) => void)
315
349
  this.startActivity(deepCopy).catch((e) => {
316
350
  this.resultResolvers.delete(deepCopy)
317
351
  reject(e)
@@ -374,9 +408,10 @@ export class ActivityManager {
374
408
  this.resultResolvers.delete(originalIntent)
375
409
  // We construct the result intent.
376
410
  // Ideally we would like to preserve the original intent structure but with new data.
377
- const resultIntent: Intent = {
411
+ const resultIntent: ResolvedIntent = {
378
412
  ...originalIntent,
379
413
  data: activity.getResult(),
414
+ result: activity.resultCode,
380
415
  }
381
416
  resolver(resultIntent)
382
417
  }
@@ -3,7 +3,7 @@ import { Activity } from './Activity.js'
3
3
  import { FragmentState, type FragmentOptions, FragmentManager } from './FragmentManager.js'
4
4
  import type { Application, UpdateRequest } from './Application.js'
5
5
  import { FragmentRenderer } from './renderer/FragmentRenderer.js'
6
- import { Intent, IntentResult } from './ActivityManager.js'
6
+ import { Intent, ResolvedIntent } from './ActivityManager.js'
7
7
  import { bound } from '../decorators/bound.js'
8
8
  import type { ActivityDetail, ActivityWithResultDetail } from '../events/IntentEvents.js'
9
9
  import { EventTypes } from '../events/EventTypes.js'
@@ -250,14 +250,24 @@ export class Fragment extends EventTarget {
250
250
 
251
251
  /**
252
252
  * Starts an activity for result.
253
+ * @template T The type of the result data expected.
253
254
  * @param intent The intent to start.
255
+ * @returns A promise that resolves when the started activity finishes, returning the result Intent with
256
+ * data of type T.
257
+ * @example
258
+ * ```typescript
259
+ * const resultIntent = await this.startActivityForResult<{ userId: string }>({ action: 'pickUser' });
260
+ * if (resultIntent.resultCode === IntentResult.RESULT_OK) {
261
+ * console.log(resultIntent.data?.userId);
262
+ * }
263
+ * ```
254
264
  */
255
- async startActivityForResult(intent: Intent): Promise<Intent> {
265
+ async startActivityForResult<T>(intent: Intent): Promise<ResolvedIntent<T | undefined>> {
256
266
  const activity = this.getActivity()
257
267
  if (!activity) {
258
268
  throw new Error('Fragment is not attached to an activity')
259
269
  }
260
- return activity.startActivityForResult(intent)
270
+ return activity.startActivityForResult<T>(intent)
261
271
  }
262
272
 
263
273
  /**
@@ -296,7 +306,7 @@ export class Fragment extends EventTarget {
296
306
  if (event.type === EventTypes.Intent.startActivityForResult) {
297
307
  const info = event.detail as ActivityWithResultDetail
298
308
  const result = await this.startActivityForResult(info.intent)
299
- info.onResult(IntentResult.RESULT_OK, result)
309
+ info.onResult(result.result, result)
300
310
  } else if (event.type === EventTypes.Intent.startActivity) {
301
311
  await activity.startActivity(event.detail.intent)
302
312
  } else {