From 77b5f5ca623519b250192eb30b3aff9d011fe2c1 Mon Sep 17 00:00:00 2001 From: Artur Androsovych Date: Fri, 19 Apr 2024 14:12:24 +0100 Subject: [PATCH] fix: `assertInAngularZone` should act as a noop (#512) --- .../src/single-spa-angular.ts | 22 ++++++------------- libs/single-spa-angular/src/types.ts | 1 - 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/libs/single-spa-angular/src/single-spa-angular.ts b/libs/single-spa-angular/src/single-spa-angular.ts index fa1ac01..18ab73d 100644 --- a/libs/single-spa-angular/src/single-spa-angular.ts +++ b/libs/single-spa-angular/src/single-spa-angular.ts @@ -60,7 +60,7 @@ export function singleSpaAngular(userOptions: SingleSpaAngularOptions): Li }; } -async function bootstrap(options: BootstrappedSingleSpaAngularOptions, props: any): Promise { +async function bootstrap(options: BootstrappedSingleSpaAngularOptions): Promise { // Angular provides an opportunity to develop `zone-less` application, where developers // have to trigger change detection manually. // See https://angular.io/guide/zone#noopzone @@ -68,18 +68,12 @@ async function bootstrap(options: BootstrappedSingleSpaAngularOptions, props: an return; } - // In order for multiple Angular apps to work concurrently on a page, they each need a unique identifier. - options.zoneIdentifier = `single-spa-angular:${props.name || props.appName}`; - - // This is a hack, since NgZone doesn't allow you to configure the property that identifies your zone. - // See https://github.com/PlaceMe-SAS/single-spa-angular-cli/issues/33, - // https://github.com/single-spa/single-spa-angular/issues/47, - // https://github.com/angular/angular/blob/a14dc2d7a4821a19f20a9547053a5734798f541e/packages/core/src/zone/ng_zone.ts#L144, - // and https://github.com/angular/angular/blob/a14dc2d7a4821a19f20a9547053a5734798f541e/packages/core/src/zone/ng_zone.ts#L257 - options.NgZone.isInAngularZone = () => { - // @ts-ignore - return window.Zone.current._properties[options.zoneIdentifier] === true; - }; + // Note that we have to make it a noop function because it's a static property and not + // an instance property. We're unable to configure it for multiple apps when dependencies + // are shared and reference the same `NgZone` class. We can't determine where this function + // is being executed or under which application, making it difficult to assert whether this + // app is running under its zone. + options.NgZone.assertInAngularZone = () => {}; options.routingEventListener = () => { options.bootstrappedNgZone!.run(() => { @@ -133,7 +127,6 @@ async function mount( if (ngZoneEnabled) { const ngZone: NgZone = ngModuleRefOrAppRef.injector.get(options.NgZone); - const zoneIdentifier: string = bootstrappedOptions.zoneIdentifier!; // `NgZone` can be enabled but routing may not be used thus `getSingleSpaExtraProviders()` // function was not called. @@ -142,7 +135,6 @@ async function mount( } bootstrappedOptions.bootstrappedNgZone = ngZone; - (bootstrappedOptions.bootstrappedNgZone as any)._inner._properties[zoneIdentifier] = true; window.addEventListener('single-spa:routing-event', bootstrappedOptions.routingEventListener!); } diff --git a/libs/single-spa-angular/src/types.ts b/libs/single-spa-angular/src/types.ts index 6e06dd5..7b85bf8 100644 --- a/libs/single-spa-angular/src/types.ts +++ b/libs/single-spa-angular/src/types.ts @@ -20,5 +20,4 @@ export interface BootstrappedSingleSpaAngularOptions extends SingleSpaAngularOpt // `SingleSpaAngularOpts.NgZone` is a `noop` string and not an `NgZone` class. bootstrappedNgZone?: NgZone; routingEventListener?: () => void; - zoneIdentifier?: string; }