@api-client/ui 0.1.8 → 0.1.9
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/Activity.d.ts +170 -109
- package/build/src/core/Activity.d.ts.map +1 -1
- package/build/src/core/Activity.js +176 -111
- package/build/src/core/Activity.js.map +1 -1
- package/package.json +1 -1
- package/src/core/Activity.ts +183 -112
|
@@ -6,18 +6,40 @@ import { FragmentManager } from './FragmentManager.js';
|
|
|
6
6
|
import { bound } from '../decorators/bound.js';
|
|
7
7
|
import { EventTypes } from '../events/EventTypes.js';
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
* to bring the activity. If it's found, the activity gets to the foreground and start its activity.
|
|
9
|
+
* ## Activity
|
|
11
10
|
*
|
|
12
|
-
*
|
|
11
|
+
* `Activity` is a core building block of your application, representing a single,
|
|
12
|
+
* focused operation that a user can perform.
|
|
13
|
+
* Activities manage their own lifecycle, UI, and fragments, and communicate with the application
|
|
14
|
+
* and other activities via intents.
|
|
13
15
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* - `
|
|
16
|
+
* Activities are managed by the application and can be started, paused, resumed, stopped,
|
|
17
|
+
* and destroyed according to the app's navigation and user actions.
|
|
18
|
+
* They can also start other activities for results, manage fragments, and handle intent
|
|
19
|
+
* events dispatched by hosted web components.
|
|
20
|
+
*
|
|
21
|
+
* ### Lifecycle Methods
|
|
22
|
+
* - `onCreate(intent?)` - Called when the activity is first created.
|
|
23
|
+
* - `onStart()` - Called when the activity becomes visible to the user.
|
|
24
|
+
* - `onResume()` - Called when the activity gains focus and becomes interactive.
|
|
25
|
+
* - `onPause()` - Called when the activity loses focus but is still visible.
|
|
26
|
+
* - `onStop()` - Called when the activity is no longer visible.
|
|
27
|
+
* - `onDestroy()` - Called before the activity is destroyed.
|
|
28
|
+
* - `onRestart()` - Called after the activity has been stopped, just prior to it being started again.
|
|
29
|
+
*
|
|
30
|
+
* ### Example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* class MyActivity extends Activity {
|
|
33
|
+
* async onCreate(intent?: Intent) {
|
|
34
|
+
* super.onCreate(intent);
|
|
35
|
+
* // Initialization logic here
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* render() {
|
|
39
|
+
* return html`<h1>Hello from MyActivity!</h1>`;
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
21
43
|
*/
|
|
22
44
|
let Activity = (() => {
|
|
23
45
|
let _classSuper = EventTarget;
|
|
@@ -31,151 +53,160 @@ let Activity = (() => {
|
|
|
31
53
|
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
32
54
|
}
|
|
33
55
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* and the content should be rendered outside the application's render root.
|
|
56
|
+
* The root element where the activity's content should be rendered.
|
|
57
|
+
* Set this if you want to render outside the application's default render root (e.g., for modals).
|
|
37
58
|
*/
|
|
38
59
|
renderRoot = __runInitializers(this, _instanceExtraInitializers);
|
|
39
60
|
/**
|
|
40
|
-
* An
|
|
41
|
-
*
|
|
42
|
-
*
|
|
61
|
+
* An optional static action name for the activity.
|
|
62
|
+
* Use this as the registration key for referencing the activity in intents.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* class LoginActivity extends Activity {
|
|
67
|
+
* static action = 'login';
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
43
70
|
*/
|
|
44
71
|
static action;
|
|
72
|
+
/** The current lifecycle state of the activity. */
|
|
45
73
|
lifecycle = ActivityLifecycle.Initialized;
|
|
74
|
+
/** The parent application instance. */
|
|
46
75
|
parent;
|
|
47
|
-
/**
|
|
48
|
-
* The fragment manager that manages fragments in this activity.
|
|
49
|
-
*/
|
|
76
|
+
/** The fragment manager for managing fragments within this activity. */
|
|
50
77
|
manager;
|
|
78
|
+
/** Data to be returned as a result to the calling activity. */
|
|
51
79
|
resultData;
|
|
80
|
+
/** The exit code for the activity result. */
|
|
52
81
|
exitCode = IntentResult.RESULT_CANCELED;
|
|
82
|
+
/** Gets the current result code for the activity. */
|
|
53
83
|
get resultCode() {
|
|
54
84
|
return this.exitCode;
|
|
55
85
|
}
|
|
86
|
+
/** Tracks pending request codes for activities started for result. */
|
|
87
|
+
pendingRequestCodes = [];
|
|
56
88
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* activity for result.
|
|
89
|
+
* Constructs a new Activity.
|
|
90
|
+
* @param parent The parent application instance.
|
|
60
91
|
*/
|
|
61
|
-
pendingRequestCodes = [];
|
|
62
92
|
constructor(parent) {
|
|
63
93
|
super();
|
|
64
94
|
this.parent = parent;
|
|
65
95
|
this.manager = new FragmentManager(this);
|
|
66
96
|
}
|
|
67
97
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
98
|
+
* Checks if the activity is in the `Destroyed` state.
|
|
99
|
+
* @returns `true` if destroyed, `false` otherwise.
|
|
100
|
+
*/
|
|
101
|
+
isDestroyed() {
|
|
102
|
+
return this.lifecycle === ActivityLifecycle.Destroyed;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Checks if the activity is in the `Resumed` state.
|
|
106
|
+
* @returns `true` if resumed, `false` otherwise.
|
|
107
|
+
*/
|
|
108
|
+
isResumed() {
|
|
109
|
+
return this.lifecycle === ActivityLifecycle.Resumed;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Called when the activity is starting. Override to perform initialization logic.
|
|
75
113
|
* @param intent Optional intent data.
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* async onCreate(intent?: Intent) {
|
|
117
|
+
* await fetchData();
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
76
120
|
*/
|
|
77
121
|
onCreate(intent) {
|
|
78
122
|
this.dispatchEvent(new CustomEvent('activity:create', { detail: intent }));
|
|
79
123
|
}
|
|
80
124
|
/**
|
|
81
|
-
* Called when the activity becomes visible.
|
|
82
|
-
*
|
|
83
|
-
* to the user. This callback contains what amounts to the activity’s final preparations for coming
|
|
84
|
-
* to the foreground and becoming interactive.
|
|
85
|
-
*
|
|
86
|
-
* Called after `onCreate()`. It will usually be followed by onResume().
|
|
87
|
-
* This is a good place to begin running animations, etc.
|
|
88
|
-
* You can call `finish()` from within this function, in which case `onStop()` will be
|
|
89
|
-
* immediately called after `onStart()` without the lifecycle transitions
|
|
90
|
-
* in-between (`onResume()`, `onPause()`, etc) executing.
|
|
125
|
+
* Called when the activity becomes visible to the user.
|
|
126
|
+
* Override to start animations or prepare UI.
|
|
91
127
|
*/
|
|
92
128
|
onStart() {
|
|
93
129
|
this.dispatchEvent(new CustomEvent('activity:start', { detail: null }));
|
|
94
130
|
}
|
|
95
131
|
/**
|
|
96
|
-
* Called when the activity gains focus.
|
|
97
|
-
*
|
|
98
|
-
* with the user, which is a good indicator that the activity became active and ready to receive input.
|
|
99
|
-
* This sometimes could also be a transit state toward another resting state.
|
|
100
|
-
* For instance, an activity may be relaunched to `onPause()` due to configuration changes and
|
|
101
|
-
* the activity was visible, but wasn't the top-most activity of an activity task.
|
|
102
|
-
* The `render()` function is always called after this callback.
|
|
132
|
+
* Called when the activity gains focus and becomes interactive.
|
|
133
|
+
* Override to start input handling or resume tasks.
|
|
103
134
|
*/
|
|
104
135
|
onResume() {
|
|
105
136
|
this.dispatchEvent(new CustomEvent('activity:resume', { detail: null }));
|
|
106
137
|
}
|
|
107
138
|
/**
|
|
108
|
-
* Called when the activity loses focus.
|
|
109
|
-
*
|
|
110
|
-
* but it is still visible on screen. The counterpart to `onResume()`.
|
|
111
|
-
*
|
|
112
|
-
* When activity B is launched in front of activity A, this callback will be invoked on A.
|
|
113
|
-
* B will not be created until A's `onPause()` returns, so be sure to not do anything lengthy here.
|
|
114
|
-
*
|
|
115
|
-
* This callback is mostly used for saving any persistent state the activity is editing,
|
|
116
|
-
* to present a "edit in place" model to the user and making sure nothing is lost if there are
|
|
117
|
-
* not enough resources to start the new activity without first killing this one.
|
|
118
|
-
* This is also a good place to stop things that consume a noticeable amount of CPU in order
|
|
119
|
-
* to make the switch to the next activity as fast as possible.
|
|
139
|
+
* Called when the activity loses focus but is still visible.
|
|
140
|
+
* Override to pause ongoing tasks or save state.
|
|
120
141
|
*/
|
|
121
142
|
onPause() {
|
|
122
143
|
this.dispatchEvent(new CustomEvent('activity:pause', { detail: null }));
|
|
123
144
|
}
|
|
124
145
|
/**
|
|
125
|
-
* Called when
|
|
126
|
-
*
|
|
127
|
-
* This is a good place to stop refreshing UI, running animations and other visual things.
|
|
146
|
+
* Called when the activity is no longer visible to the user.
|
|
147
|
+
* Override to stop UI updates or animations.
|
|
128
148
|
*/
|
|
129
149
|
onStop() {
|
|
130
150
|
this.dispatchEvent(new CustomEvent('activity:stop', { detail: null }));
|
|
131
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Called after the activity has been stopped, just prior to it being started again.
|
|
154
|
+
*/
|
|
132
155
|
onRestart() {
|
|
133
156
|
this.dispatchEvent(new CustomEvent('activity:restart', { detail: null }));
|
|
134
157
|
}
|
|
135
158
|
/**
|
|
136
|
-
*
|
|
137
|
-
* This can happen because the activity is finishing (someone called finish() on it).
|
|
159
|
+
* Called before the activity is destroyed. Override to clean up resources.
|
|
138
160
|
*/
|
|
139
161
|
onDestroy() {
|
|
140
162
|
this.dispatchEvent(new CustomEvent('activity:destroy', { detail: null }));
|
|
141
163
|
this.manager.onDestroy();
|
|
142
164
|
}
|
|
143
165
|
/**
|
|
144
|
-
* Called when a new intent is delivered
|
|
166
|
+
* Called when a new intent is delivered to the activity.
|
|
167
|
+
* Override to handle new intents.
|
|
145
168
|
* @param intent New intent data.
|
|
146
169
|
*/
|
|
147
170
|
onNewIntent(intent) {
|
|
148
|
-
//
|
|
171
|
+
// Override in subclass if needed
|
|
149
172
|
}
|
|
150
173
|
/**
|
|
151
174
|
* Called by the renderer when the activity is rendered for the first time.
|
|
175
|
+
* Override to perform actions after first render.
|
|
152
176
|
*/
|
|
153
177
|
onFirstRender() {
|
|
154
|
-
//
|
|
178
|
+
// Override in subclass if needed
|
|
155
179
|
}
|
|
156
180
|
/**
|
|
157
|
-
*
|
|
181
|
+
* Renders the activity's view. Override to provide UI.
|
|
182
|
+
* @returns A Lit TemplateResult or `nothing` if nothing should be rendered.
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* render() {
|
|
186
|
+
* return html`<div>Hello, world!</div>`;
|
|
187
|
+
* }
|
|
188
|
+
* ```
|
|
158
189
|
*/
|
|
159
190
|
render() {
|
|
160
191
|
return nothing;
|
|
161
192
|
}
|
|
162
193
|
/**
|
|
163
|
-
* Finishes the activity.
|
|
194
|
+
* Finishes the activity and notifies the application.
|
|
195
|
+
* @example
|
|
196
|
+
* ```typescript
|
|
197
|
+
* await this.finish();
|
|
198
|
+
* ```
|
|
164
199
|
*/
|
|
165
200
|
async finish() {
|
|
166
201
|
await this.getApplication().manager.finishActivity(this);
|
|
167
202
|
}
|
|
168
203
|
/**
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* Note, the application may choose to not call the render procedure
|
|
176
|
-
* (for example, when the activity is not visible). The rendering
|
|
177
|
-
* function is asynchronous. The only way to know the activity is being rendered,
|
|
178
|
-
* is when the `render()` method if called.
|
|
204
|
+
* Requests the parent application to update the UI.
|
|
205
|
+
* @param opts Update options.
|
|
206
|
+
* @example
|
|
207
|
+
* ```typescript
|
|
208
|
+
* this.requestUpdate();
|
|
209
|
+
* ```
|
|
179
210
|
*/
|
|
180
211
|
requestUpdate(opts = {}) {
|
|
181
212
|
const { activity = true } = opts;
|
|
@@ -185,16 +216,30 @@ let Activity = (() => {
|
|
|
185
216
|
this.parent.requestUpdate(opts);
|
|
186
217
|
}
|
|
187
218
|
/**
|
|
188
|
-
* Registers a new fragment.
|
|
189
|
-
* @param key The
|
|
190
|
-
* @param fragment The fragment
|
|
219
|
+
* Registers a new fragment with this activity.
|
|
220
|
+
* @param key The fragment's key.
|
|
221
|
+
* @param fragment The fragment instance.
|
|
222
|
+
* @param data Optional data to pass to the fragment.
|
|
223
|
+
* @example
|
|
224
|
+
* ```typescript
|
|
225
|
+
* await this.addFragment('profile', new ProfileFragment());
|
|
226
|
+
* ```
|
|
191
227
|
*/
|
|
192
228
|
async addFragment(key, fragment, data) {
|
|
193
229
|
await this.manager.attachFragment(key, fragment, this, data);
|
|
194
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Shows a registered fragment.
|
|
233
|
+
* @param key The fragment's key.
|
|
234
|
+
* @param root Optional root element to render into.
|
|
235
|
+
*/
|
|
195
236
|
async showFragment(key, root) {
|
|
196
237
|
await this.manager.showFragment(key, root);
|
|
197
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Hides a registered fragment.
|
|
241
|
+
* @param key The fragment's key.
|
|
242
|
+
*/
|
|
198
243
|
async hideFragment(key) {
|
|
199
244
|
const fragment = this.manager.findFragment(key);
|
|
200
245
|
if (fragment) {
|
|
@@ -202,23 +247,31 @@ let Activity = (() => {
|
|
|
202
247
|
}
|
|
203
248
|
}
|
|
204
249
|
/**
|
|
205
|
-
*
|
|
206
|
-
* @returns
|
|
250
|
+
* Returns the parent application instance.
|
|
251
|
+
* @returns The application.
|
|
207
252
|
*/
|
|
208
253
|
getApplication() {
|
|
209
254
|
return this.parent;
|
|
210
255
|
}
|
|
211
256
|
/**
|
|
212
|
-
*
|
|
213
|
-
* @param intent The intent to start
|
|
257
|
+
* Starts a new activity.
|
|
258
|
+
* @param intent The intent to start.
|
|
259
|
+
* @example
|
|
260
|
+
* ```typescript
|
|
261
|
+
* await this.startActivity({ action: 'login' });
|
|
262
|
+
* ```
|
|
214
263
|
*/
|
|
215
264
|
startActivity(intent) {
|
|
216
265
|
return this.parent.manager.startActivity(intent);
|
|
217
266
|
}
|
|
218
267
|
/**
|
|
219
268
|
* Starts a new activity for a result.
|
|
220
|
-
* @param intent The intent
|
|
221
|
-
* @returns The request code
|
|
269
|
+
* @param intent The intent to start.
|
|
270
|
+
* @returns The request code for the started activity.
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* const code = await this.startActivityForResult({ action: 'pickFile' });
|
|
274
|
+
* ```
|
|
222
275
|
*/
|
|
223
276
|
async startActivityForResult(intent) {
|
|
224
277
|
const { manager } = this.getApplication();
|
|
@@ -228,25 +281,32 @@ let Activity = (() => {
|
|
|
228
281
|
return code;
|
|
229
282
|
}
|
|
230
283
|
/**
|
|
231
|
-
*
|
|
232
|
-
* @param
|
|
284
|
+
* Sets the result to be returned to the calling activity.
|
|
285
|
+
* @param resultCode The result code.
|
|
286
|
+
* @param data Optional result data.
|
|
287
|
+
* @example
|
|
288
|
+
* ```typescript
|
|
289
|
+
* this.setResult(IntentResult.RESULT_OK, { userId: 123 });
|
|
290
|
+
* ```
|
|
233
291
|
*/
|
|
234
292
|
setResult(resultCode, data) {
|
|
235
293
|
this.exitCode = resultCode;
|
|
236
294
|
this.resultData = data;
|
|
237
295
|
}
|
|
238
296
|
/**
|
|
239
|
-
* Called when an activity you launched exits, giving you the
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
* An activity can never receive a result in the resumed state. You can count on `onResume()` being called
|
|
244
|
-
* after this method, though not necessarily immediately after. If the activity was resumed,
|
|
245
|
-
* it will be paused and the result will be delivered, followed by `onResume()`.
|
|
246
|
-
*
|
|
247
|
-
* @param requestCode The request code passed to startActivityForResult.
|
|
248
|
-
* @param data The result from the activity.
|
|
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.
|
|
249
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
|
+
* ```
|
|
250
310
|
*/
|
|
251
311
|
async onActivityResult(requestCode, resultCode, intent) {
|
|
252
312
|
const index = this.pendingRequestCodes.indexOf(requestCode);
|
|
@@ -263,34 +323,39 @@ let Activity = (() => {
|
|
|
263
323
|
console.info(`Activity#onActivityResult not implemented. Request code: ${requestCode}, result code: ${resultCode}`, intent);
|
|
264
324
|
}
|
|
265
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* Returns the current activity instance.
|
|
328
|
+
* @returns This activity.
|
|
329
|
+
*/
|
|
266
330
|
getActivity() {
|
|
267
331
|
return this;
|
|
268
332
|
}
|
|
269
333
|
/**
|
|
270
|
-
* Checks
|
|
271
|
-
* @param code The request code
|
|
272
|
-
* @returns `true` if the
|
|
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.
|
|
273
337
|
*/
|
|
274
338
|
hasRequestCode(code) {
|
|
275
339
|
return this.pendingRequestCodes.includes(code);
|
|
276
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* Gets the result data set by `setResult`.
|
|
343
|
+
* @returns The result data or `undefined`.
|
|
344
|
+
*/
|
|
277
345
|
getResult() {
|
|
278
346
|
return this.resultData;
|
|
279
347
|
}
|
|
280
348
|
/**
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
* **Usage example:**
|
|
349
|
+
* Handles intent events dispatched by web components hosted by this activity.
|
|
284
350
|
*
|
|
285
|
-
*
|
|
351
|
+
* Usage example:
|
|
352
|
+
* ```html
|
|
286
353
|
* <custom-element @startactivity="${this.handleIntentEvent}"></custom-element>
|
|
287
354
|
* <custom-element @startactivityforresult="${this.handleIntentEvent}"></custom-element>
|
|
288
355
|
* ```
|
|
289
356
|
*
|
|
290
|
-
* @param event The event
|
|
291
|
-
* @
|
|
292
|
-
* @throws An error if the activity has no activity.
|
|
293
|
-
* @throws An error if the event type is not recognized.
|
|
357
|
+
* @param event The intent event.
|
|
358
|
+
* @throws Error if the event type is not recognized.
|
|
294
359
|
*/
|
|
295
360
|
async handleIntentEvent(event) {
|
|
296
361
|
if (event.type === EventTypes.Intent.startActivityForResult) {
|
|
@@ -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;;;;;;;;;;;;;GAaG;IACU,QAAQ;sBAAS,WAAW;;;iBAA5B,QAAS,SAAQ,WAAW;;;6CAuStC,KAAK;YACN,sMAAM,iBAAiB,6DAStB;;;QAhTD;;;;WAIG;QACH,UAAU,GANC,mDAAQ,CAMK;QACxB;;;;WAIG;QACH,MAAM,CAAC,MAAM,CAAS;QAEf,SAAS,GAAsB,iBAAiB,CAAC,WAAW,CAAA;QACzD,MAAM,CAAa;QAC7B;;WAEG;QACO,OAAO,CAAiB;QACxB,UAAU,CAAU;QAEpB,QAAQ,GAAG,YAAY,CAAC,eAAe,CAAA;QAEjD,IAAI,UAAU;YACZ,OAAO,IAAI,CAAC,QAAQ,CAAA;QACtB,CAAC;QAED;;;;WAIG;QACO,mBAAmB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Q,EAAE,CAAC;gBAClC,sCAAsC;gBACtC,OAAO,CAAC,IAAI,CACV,4DAA4D,WAAW,kBAAkB,UAAU,EAAE,EACrG,MAAM,CACP,CAAA;YACH,CAAC;QACH,CAAC;QAED,WAAW;YACT,OAAO,IAAI,CAAA;QACb,CAAC;QAED;;;;WAIG;QACH,cAAc,CAAC,IAAY;YACzB,OAAO,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;QAChD,CAAC;QAED,SAAS;YACP,OAAO,IAAI,CAAC,UAAU,CAAA;QACxB,CAAC;QAED;;;;;;;;;;;;;;WAcG;QAEH,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;;;SAjTU,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` is a crucial component of your app. An application sends an intent\n * to bring the activity. If it's found, the activity gets to the foreground and start its activity.\n *\n * **Lifecycle**\n *\n * - `onCreated()` - fires when the system first creates the activity.\n * - `onStart()` - the activity becomes visible to the user as the app prepares for\n * the activity to enter the foreground and become interactive.\n * - `onResume()` - the state in which the app interacts with the user.\n * - `onPause()` - the first indication that the user is leaving your activity.\n * - `onStop()` - your activity is no longer visible to the user.\n * - `onDestroy()`- is called before the activity is destroyed\n */\nexport class Activity extends EventTarget {\n /**\n * To be set by an activity to replace the default render root.\n * This is particularly useful when the activity is a modal dialog\n * and the content should be rendered outside the application's render root.\n */\n renderRoot?: HTMLElement\n /**\n * An activity can define its action name that does not change.\n * It then should be used as the activity registration key.\n * This way other activities/fragments can use it to call the activity.\n */\n static action?: string\n\n public lifecycle: ActivityLifecycle = ActivityLifecycle.Initialized\n protected parent: Application\n /**\n * The fragment manager that manages fragments in this activity.\n */\n protected manager: FragmentManager\n protected resultData?: unknown\n\n protected exitCode = IntentResult.RESULT_CANCELED\n\n get resultCode(): IntentResult {\n return this.exitCode\n }\n\n /**\n * When starting an new activity for result, we add the request code here\n * so that the manager can track which activity originally called the\n * activity for result.\n */\n protected pendingRequestCodes: number[] = []\n\n constructor(parent: Application) {\n super()\n this.parent = parent\n this.manager = new FragmentManager(this)\n }\n\n /**\n * Called when the activity is starting. This is where most initialization should go.\n * You can call `finish()` from within this function, in which case `onDestroy()` will be\n * immediately called after `onCreate()` without any of the rest of the activity lifecycle\n * (`onStart()`, `onResume()`, `onPause()`, etc) executing.\n *\n * In the `onCreate()` method, perform basic activity startup logic that happens only once\n * for the entire life of the activity.\n * @param intent Optional intent data.\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.\n * As `onCreate()` exits, the activity enters the `Started` state, and the activity becomes visible\n * to the user. This callback contains what amounts to the activity’s final preparations for coming\n * to the foreground and becoming interactive.\n *\n * Called after `onCreate()`. It will usually be followed by onResume().\n * This is a good place to begin running animations, etc.\n * You can call `finish()` from within this function, in which case `onStop()` will be\n * immediately called after `onStart()` without the lifecycle transitions\n * in-between (`onResume()`, `onPause()`, etc) executing.\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.\n * Called after the `onPause()`. This is usually a hint for your activity to start interacting\n * with the user, which is a good indicator that the activity became active and ready to receive input.\n * This sometimes could also be a transit state toward another resting state.\n * For instance, an activity may be relaunched to `onPause()` due to configuration changes and\n * the activity was visible, but wasn't the top-most activity of an activity task.\n * The `render()` function is always called after this callback.\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.\n * Called as part of the activity lifecycle when the user no longer actively interacts with the activity,\n * but it is still visible on screen. The counterpart to `onResume()`.\n *\n * When activity B is launched in front of activity A, this callback will be invoked on A.\n * B will not be created until A's `onPause()` returns, so be sure to not do anything lengthy here.\n *\n * This callback is mostly used for saving any persistent state the activity is editing,\n * to present a \"edit in place\" model to the user and making sure nothing is lost if there are\n * not enough resources to start the new activity without first killing this one.\n * This is also a good place to stop things that consume a noticeable amount of CPU in order\n * to make the switch to the next activity as fast as possible.\n */\n onPause(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:pause', { detail: null }))\n }\n\n /**\n * Called when you are no longer visible to the user.\n * You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.\n * This is a good place to stop refreshing UI, running animations and other visual things.\n */\n onStop(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:stop', { detail: null }))\n }\n\n onRestart(): void | Promise<void> {\n this.dispatchEvent(new CustomEvent('activity:restart', { detail: null }))\n }\n\n /**\n * Perform any final cleanup before an activity is destroyed.\n * This can happen because the activity is finishing (someone called finish() on it).\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\n * @param intent New intent data.\n */\n onNewIntent(intent: Intent): void | Promise<void> {\n //\n }\n\n /**\n * Called by the renderer when the activity is rendered for the first time.\n */\n onFirstRender(): void {\n //\n }\n\n /**\n * A function called when it should render the view.\n */\n render(): TemplateResult | typeof nothing {\n return nothing\n }\n\n /**\n * Finishes the activity.\n */\n async finish(): Promise<void> {\n await this.getApplication().manager.finishActivity(this)\n }\n\n /**\n * When the activity changes its state it should call the\n * `requestUpdate()` method to inform the parent application to\n * perform the render operation.\n * The function dispatches the `activity:update` custom event\n * that is handled by the parent application.\n *\n * Note, the application may choose to not call the render procedure\n * (for example, when the activity is not visible). The rendering\n * function is asynchronous. The only way to know the activity is being rendered,\n * is when the `render()` method if called.\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.\n * @param key The name of the fragment\n * @param fragment The fragment to register.\n */\n async addFragment(key: string, fragment: Fragment, data?: unknown): Promise<void> {\n await this.manager.attachFragment(key, fragment, this, data)\n }\n\n async showFragment(key: string, root?: HTMLElement): Promise<void> {\n await this.manager.showFragment(key, root)\n }\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 * Unifies Fragment and Activity interfaces.\n * @returns `this`\n */\n getApplication(): Application {\n return this.parent\n }\n\n /**\n * A shortcut to the `manager` function.\n * @param intent The intent to start\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 that created this activity.\n * @returns The request code created with this call.\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 * Call this method to return data to the activity that started this activity.\n * @param data The data to return.\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 `requestCode` you started it with,\n * the `resultCode` it returned, and any additional data from it. The `resultCode` will be `RESULT_CANCELED`\n * if the activity explicitly returned that, didn't return any result, or crashed during its operation.\n *\n * An activity can never receive a result in the resumed state. You can count on `onResume()` being called\n * after this method, though not necessarily immediately after. If the activity was resumed,\n * it will be paused and the result will be delivered, followed by `onResume()`.\n *\n * @param requestCode The request code passed to startActivityForResult.\n * @param data The result from the activity.\n * @param intent The intent that was used to start the activity.\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 getActivity(): Activity {\n return this\n }\n\n /**\n * Checks whether this activity initiated the activity for result.\n * @param code The request code to look for.\n * @returns `true` if the activity has the request code.\n */\n hasRequestCode(code: number): boolean {\n return this.pendingRequestCodes.includes(code)\n }\n\n getResult(): unknown | undefined {\n return this.resultData\n }\n\n /**\n * A handler for the intent event dispatched by web components hosted by this activity.\n *\n * **Usage example:**\n *\n * ```ts\n * <custom-element @startactivity=\"${this.handleIntentEvent}\"></custom-element>\n * <custom-element @startactivityforresult=\"${this.handleIntentEvent}\"></custom-element>\n * ```\n *\n * @param event The event that was dispatched.\n * @returns A promise that resolves when the intent is handled.\n * @throws An error if the activity has no activity.\n * @throws An 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,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;IACU,QAAQ;sBAAS,WAAW;;;iBAA5B,QAAS,SAAQ,WAAW;;;6CAwVtC,KAAK;YACN,sMAAM,iBAAiB,6DAStB;;;QAjWD;;;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,sEAAsE;QAC5D,mBAAmB,GAAa,EAAE,CAAA;QAE5C;;;WAGG;QACH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ing, 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"]}
|