Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/172 #199

Merged
merged 4 commits into from
Nov 23, 2022
Merged

Issue/172 #199

merged 4 commits into from
Nov 23, 2022

Conversation

danielwiehl
Copy link
Collaborator

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our guidelines
  • Tests for the changes have been added
  • Docs have been added or updated

PR Type

What kind of change does this PR introduce?

  • Fix
  • Feature
  • Documentation
  • Refactoring (changes that neither fixes a bug nor adds a feature)
  • Performance (changes that improve performance)
  • Test (adding missing tests, refactoring tests; no production code change)
  • Chore (other changes like formatting, updating the license, removal of deprecations, etc)
  • Deps (changes related to updating dependencies)
  • CI (changes to our CI configuration files and scripts)
  • Revert (revert of a previous commit)
  • Release (publish a new release)
  • Other... Please describe:

Issue

Issue Number: #172

Does this PR introduce a breaking change?

  • Yes
  • No

For Angular applications, we strongly recommend replacing zone-specific decorators for MessageClient and IntentClient with an ObservableDecorator. See https://scion-microfrontend-platform-developer-guide.vercel.app/#chapter:angular-integration-guide:synchronizing-rxjs-observables-with-angular-zone for an example.

It turned out that Angular zone synchronization with decorators for MessageClient and IntentClient is not sufficient and that observables should emit in the same Angular zone in which the subscription was performed. Using the new ObservableDecorator API, Angular zone synchronization can now be performed in a single place for all observables, as follows:

  • Remove decorators for MessageClient and IntentClient including their registration in the bean manager.
  • Provide a NgZoneObservableDecorator and register it in the bean manager before starting the platform. Note to register it as a bean, not as a decorator.

For a complete example and detailed instructions, see https://scion-microfrontend-platform-developer-guide.vercel.app/#chapter:angular-integration-guide:synchronizing-rxjs-observables-with-angular-zone.

Example of a decorator for synchronizing the Angular zone

/**
 * Mirrors the source, but ensures subscription and emission {@link NgZone} to be identical.
 */
export class NgZoneObservableDecorator implements ObservableDecorator {

  constructor(private zone: NgZone) {
  }

  public decorate$<T>(source$: Observable<T>): Observable<T> {
    return new Observable<T>(observer => {
      const insideAngular = NgZone.isInAngularZone();
      const subscription = source$
        .pipe(
          subscribeInside(fn => this.zone.runOutsideAngular(fn)),
          observeInside(fn => insideAngular ? this.zone.run(fn) : this.zone.runOutsideAngular(fn)),
        )
        .subscribe(observer);
      return () => subscription.unsubscribe();
    });
  }
}

Registration of the decorator in the bean manager

const zone: NgZone = ...;
Beans.register(ObservableDecorator, {useValue: new NgZoneObservableDecorator(zone)});

@danielwiehl danielwiehl added BREAKING CHANGE performance Related to the performance of the platform. labels Nov 18, 2022
@danielwiehl danielwiehl added this to the v1.0.0 milestone Nov 18, 2022
…gateway

With the introduction of intent subscriptions, the observable is no longer used outside of the gateway to observe messages received by the broker and has therefore been removed from its API.
The emission context of an Observables may be different than the subscription context, which can lead to unexpected behavior on the subscriber side. For example, Angular uses zones (Zone.js) to trigger change detection. Angular applications expect an RxJS Observable to emit in the same Angular zone in which subscribed to the Observable. That is, if subscribing inside the Angular zone, emissions are expected to be received inside the Angular zone. Otherwise, the UI may not be updated as expected but delayed until the next change detection cycle. Similarly, if subscribing outside the Angular zone, emissions are expected to be received outside the Angular zone. Otherwise, this would cause unnecessary change detection cycles resulting in potential performance degradation.

BREAKING CHANGE: For Angular applications, we strongly recommend replacing zone-specific decorators for `MessageClient` and `IntentClient` with an `ObservableDecorator`. See https://scion-microfrontend-platform-developer-guide.vercel.app/#chapter:angular-integration-guide:synchronizing-rxjs-observables-with-angular-zone for an example.

It turned out that Angular zone synchronization with decorators for `MessageClient` and `IntentClient` is not sufficient and that observables should emit in the same Angular zone in which the subscription was performed. Using the new `ObservableDecorator` API, Angular zone synchronization can now be performed in a single place for all observables, as follows:

- Remove decorators for `MessageClient` and `IntentClient` including their registration in the bean manager.
- Provide a `NgZoneObservableDecorator` and register it in the bean manager before starting the platform. Note to register it as a bean, not as a decorator.

For a complete example and detailed instructions, see https://scion-microfrontend-platform-developer-guide.vercel.app/#chapter:angular-integration-guide:synchronizing-rxjs-observables-with-angular-zone.

### Example of a decorator for synchronizing the Angular zone
```ts
/**
 * Mirrors the source, but ensures subscription and emission {@link NgZone} to be identical.
 */
export class NgZoneObservableDecorator implements ObservableDecorator {

  constructor(private zone: NgZone) {
  }

  public decorate$<T>(source$: Observable<T>): Observable<T> {
    return new Observable<T>(observer => {
      const insideAngular = NgZone.isInAngularZone();
      const subscription = source$
        .pipe(
          subscribeInside(fn => this.zone.runOutsideAngular(fn)),
          observeInside(fn => insideAngular ? this.zone.run(fn) : this.zone.runOutsideAngular(fn)),
        )
        .subscribe(observer);
      return () => subscription.unsubscribe();
    });
  }
}
```

### Registration of the decorator in the bean manager
```ts
const zone: NgZone = ...;
Beans.register(ObservableDecorator, {useValue: new NgZoneObservableDecorator(zone)});
```
Optimizations include:
- Reducing the number of events for the platform to track focus across microfrontends.
- Dispatching of synthetic mouse events only from inactive microfrontends.
- Delivery of synthetic mouse events to the active microfrontend only.
- Disabling event listeners that are not needed in activators.
- Listening to keyboard events only in contexts where keystrokes are registered.

closes #172
@Marcarrian Marcarrian merged commit daff4f0 into master Nov 23, 2022
@Marcarrian Marcarrian deleted the issue/172 branch November 23, 2022 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE performance Related to the performance of the platform.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants