@ama-mfe/ng-utils 14.0.0-next.0 → 14.0.0-next.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +160 -6
- package/fesm2022/ama-mfe-ng-utils.mjs +831 -325
- package/fesm2022/ama-mfe-ng-utils.mjs.map +1 -1
- package/index.d.ts +534 -191
- package/index.d.ts.map +1 -1
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -59,7 +59,7 @@ import {inject, runInInjectionContext} from '@angular/core';
|
|
|
59
59
|
import {bootstrapApplication} from '@angular/platform-browser';
|
|
60
60
|
import {ConnectionService, NavigationConsumerService} from '@ama-mfe/ng-utils';
|
|
61
61
|
|
|
62
|
-
bootstrapApplication(
|
|
62
|
+
bootstrapApplication(App, appConfig)
|
|
63
63
|
.then((m) => {
|
|
64
64
|
runInInjectionContext(m.injector, () => {
|
|
65
65
|
if (window.top !== window.self) {
|
|
@@ -94,12 +94,12 @@ If you are a consumer of the message, call the `start` and `stop` methods to res
|
|
|
94
94
|
```typescript
|
|
95
95
|
import {Component, inject} from '@angular/core';
|
|
96
96
|
import {NavigationConsumerService} from '@ama-mfe/ng-utils';
|
|
97
|
-
import {ThemeConsumerService} from "./theme
|
|
97
|
+
import {ThemeConsumerService} from "./theme-consumer-service";
|
|
98
98
|
|
|
99
99
|
@Component({
|
|
100
|
-
selector: 'app-example-module',
|
|
101
|
-
template: './example-module.
|
|
102
|
-
styleUrl: './example-module.
|
|
100
|
+
selector: 'app-example-module-component',
|
|
101
|
+
template: './example-module-component.html',
|
|
102
|
+
styleUrl: './example-module-component.scss',
|
|
103
103
|
})
|
|
104
104
|
export class ExampleModuleComponent {
|
|
105
105
|
private readonly navigationConsumerService = inject(NavigationConsumerService);
|
|
@@ -123,7 +123,7 @@ import {inject, runInInjectionContext} from '@angular/core';
|
|
|
123
123
|
import {bootstrapApplication} from '@angular/platform-browser';
|
|
124
124
|
import {ConnectionService, ThemeConsumerService} from '@ama-mfe/ng-utils';
|
|
125
125
|
|
|
126
|
-
bootstrapApplication(
|
|
126
|
+
bootstrapApplication(App, appConfig)
|
|
127
127
|
.then((m) => {
|
|
128
128
|
runInInjectionContext(m.injector, () => {
|
|
129
129
|
if (window.top !== window.self) {
|
|
@@ -295,3 +295,157 @@ The host information is stored in session storage so it won't be lost when navig
|
|
|
295
295
|
When using iframes to embed applications, the browser history might be shared by the main page and the embedded iframe. For example `<iframe src="https://example.com" sandbox="allow-same-origin">` will share the same history as the main page. This can lead to unexpected behavior when using browser 'back' and 'forward' buttons.
|
|
296
296
|
|
|
297
297
|
To avoid this, the `@ama-mfe/ng-utils` will forbid the application running in the iframe to alter the browser history. It will happen when connection is configured using `provideConection()` function. This will prevent the iframe to be able to use the `history.pushState` and `history.replaceState` methods.
|
|
298
|
+
|
|
299
|
+
### User Activity Tracking
|
|
300
|
+
|
|
301
|
+
The User Activity Tracking feature allows a host application (shell) to monitor user interactions across embedded micro-frontends. This is useful for implementing session timeout functionality, analytics, or any feature that needs to know when users are actively interacting with the application.
|
|
302
|
+
|
|
303
|
+
#### How it works
|
|
304
|
+
|
|
305
|
+
- **Producer**: The `ActivityProducerService` listens for DOM events (click, keydown, scroll, touchstart, focus) and:
|
|
306
|
+
- Exposes a `localActivity` signal for consumers within the same application (not throttled for local detection).
|
|
307
|
+
- Sends throttled activity messages via the communication protocol to connected peers.
|
|
308
|
+
- **Consumer**: The `ActivityConsumerService` receives activity messages from connected peers via the communication protocol and exposes them via the `latestReceivedActivity` signal.
|
|
309
|
+
|
|
310
|
+
Both services can be used in either the shell (host) or embedded applications depending on your use case. For example:
|
|
311
|
+
- A shell can produce activity signals to notify embedded modules of user interactions in the host.
|
|
312
|
+
- An embedded module can consume activity signals from the shell.
|
|
313
|
+
- An application can use the producer's `localActivity` signal to detect activity locally.
|
|
314
|
+
|
|
315
|
+
The service automatically throttles messages sent to peers to prevent flooding the communication channel. High-frequency events like `scroll` have additional throttling.
|
|
316
|
+
|
|
317
|
+
#### Producer Configuration
|
|
318
|
+
|
|
319
|
+
Start the `ActivityProducerService` to send activity signals to connected peers:
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
import { inject, runInInjectionContext } from '@angular/core';
|
|
323
|
+
import { bootstrapApplication } from '@angular/platform-browser';
|
|
324
|
+
import { ConnectionService, ActivityProducerService } from '@ama-mfe/ng-utils';
|
|
325
|
+
|
|
326
|
+
bootstrapApplication(App, appConfig)
|
|
327
|
+
.then((m) => {
|
|
328
|
+
runInInjectionContext(m.injector, () => {
|
|
329
|
+
if (window.top !== window.self) {
|
|
330
|
+
inject(ConnectionService).connect('hostUniqueID');
|
|
331
|
+
// Start activity tracking with custom throttle
|
|
332
|
+
inject(ActivityProducerService).start({
|
|
333
|
+
throttleMs: 5000 // Send at most one message every 5 seconds
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
##### Configuration Options
|
|
341
|
+
|
|
342
|
+
| Option | Type | Default | Description |
|
|
343
|
+
|--------|------|---------|-------------|
|
|
344
|
+
| `throttleMs` | `number` | `1000` | Minimum interval between activity messages sent to the host |
|
|
345
|
+
| `trackNestedIframes` | `boolean` | `false` | Enable tracking of nested iframes within the application |
|
|
346
|
+
| `nestedIframePollIntervalMs` | `number` | `1000` | Polling interval for detecting iframe focus changes |
|
|
347
|
+
| `nestedIframeActivityEmitIntervalMs` | `number` | `30000` | Interval for sending activity signals while an iframe has focus |
|
|
348
|
+
| `highFrequencyThrottleMs` | `number` | `300` | Throttle time for high-frequency events (scroll) |
|
|
349
|
+
| `shouldBroadcast` | `(event: Event) => boolean` | - | Optional filter function to control which events are broadcast |
|
|
350
|
+
|
|
351
|
+
##### Filtering Events with shouldBroadcast
|
|
352
|
+
|
|
353
|
+
The `shouldBroadcast` option allows you to filter which events trigger activity messages. This is useful in the shell application to exclude events that occur on iframes, since user activity inside embedded modules is already tracked via the communication protocol.
|
|
354
|
+
|
|
355
|
+
```typescript
|
|
356
|
+
// In the shell application
|
|
357
|
+
inject(ActivityProducerService).start({
|
|
358
|
+
throttleMs: 1000,
|
|
359
|
+
shouldBroadcast: (event: Event) => {
|
|
360
|
+
// Exclude events on iframes - activity from embedded modules comes via the communication protocol
|
|
361
|
+
return !(event.target instanceof HTMLIFrameElement);
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
##### Tracking Nested Iframes
|
|
367
|
+
|
|
368
|
+
When a user interacts with content inside an iframe (e.g., a third-party widget, payment form, or embedded content), the parent application cannot detect those interactions directly. This is because:
|
|
369
|
+
|
|
370
|
+
1. **Cross-origin restrictions**: DOM events inside cross-origin iframes do not bubble up to the parent document.
|
|
371
|
+
2. **Focus isolation**: When an iframe has focus, the parent document stops receiving keyboard and mouse events.
|
|
372
|
+
|
|
373
|
+
This creates a problem for detection of user interactions: a user could be actively filling out a form inside an iframe, but the host application would see no activity and might incorrectly trigger a session timeout.
|
|
374
|
+
|
|
375
|
+
**Solution**: When `trackNestedIframes` is enabled, the `ActivityProducerService` polls `document.activeElement` to detect when an iframe gains focus. While an iframe has focus, the service simulates activity by periodically emitting `iframeinteraction` events. This ensures the host application knows the user is still active, even though it cannot see the actual interactions inside the iframe.
|
|
376
|
+
|
|
377
|
+
Enable nested iframe tracking in embedded applications that contain other iframes:
|
|
378
|
+
|
|
379
|
+
```typescript
|
|
380
|
+
inject(ActivityProducerService).start({
|
|
381
|
+
throttleMs: 5000,
|
|
382
|
+
trackNestedIframes: true,
|
|
383
|
+
nestedIframePollIntervalMs: 1000, // Check for iframe focus every second
|
|
384
|
+
nestedIframeActivityEmitIntervalMs: 30000 // Send activity every 30s while iframe has focus
|
|
385
|
+
});
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
**When to use**: Enable this option in embedded modules that contain iframes whose content you cannot modify to include activity tracking (e.g., third-party widgets, payment providers, or external content).
|
|
389
|
+
|
|
390
|
+
#### Consumer Configuration
|
|
391
|
+
|
|
392
|
+
Start the `ActivityConsumerService` to receive activity signals from connected peers:
|
|
393
|
+
|
|
394
|
+
```typescript
|
|
395
|
+
import { Component, inject, effect } from '@angular/core';
|
|
396
|
+
import { ActivityConsumerService } from '@ama-mfe/ng-utils';
|
|
397
|
+
|
|
398
|
+
@Component({
|
|
399
|
+
selector: 'app-shell',
|
|
400
|
+
template: '...'
|
|
401
|
+
})
|
|
402
|
+
export class ShellComponent {
|
|
403
|
+
private readonly activityConsumer = inject(ActivityConsumerService);
|
|
404
|
+
|
|
405
|
+
constructor() {
|
|
406
|
+
// Start listening for activity messages
|
|
407
|
+
this.activityConsumer.start();
|
|
408
|
+
|
|
409
|
+
// React to activity changes
|
|
410
|
+
effect(() => {
|
|
411
|
+
const activity = this.activityConsumer.latestReceivedActivity();
|
|
412
|
+
if (activity) {
|
|
413
|
+
console.log(`Activity from ${activity.channelId}: ${activity.eventType} at ${activity.timestamp}`);
|
|
414
|
+
// Reset session timeout, update analytics, etc.
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
ngOnDestroy() {
|
|
420
|
+
this.activityConsumer.stop();
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
#### Local Activity Signal
|
|
426
|
+
|
|
427
|
+
The `ActivityProducerService` also exposes a `localActivity` signal for detecting activity within the same application:
|
|
428
|
+
|
|
429
|
+
```typescript
|
|
430
|
+
import { Component, inject, effect } from '@angular/core';
|
|
431
|
+
import { ActivityProducerService } from '@ama-mfe/ng-utils';
|
|
432
|
+
|
|
433
|
+
@Component({
|
|
434
|
+
selector: 'app-root',
|
|
435
|
+
template: '...'
|
|
436
|
+
})
|
|
437
|
+
export class AppComponent {
|
|
438
|
+
private readonly activityProducer = inject(ActivityProducerService);
|
|
439
|
+
|
|
440
|
+
constructor() {
|
|
441
|
+
this.activityProducer.start({ throttleMs: 1000 });
|
|
442
|
+
|
|
443
|
+
effect(() => {
|
|
444
|
+
const activity = this.activityProducer.localActivity();
|
|
445
|
+
if (activity) {
|
|
446
|
+
// React to local activity (not throttled for local detection)
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
```
|