Skip to content

Commit

Permalink
fix: assertInAngularZone should act as a noop (single-spa#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturovt authored and enlob committed Jun 5, 2024
1 parent f5da8e1 commit efd4016
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
21 changes: 6 additions & 15 deletions libs/single-spa-angular/src/single-spa-angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,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.
instance.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
// Check zone for any instance.
return Object.values(options.instances).some(instance => window.Zone.current._properties[instance.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 = () => {};

instance.routingEventListener = () => {
instance.bootstrappedNgZone!.run(() => {
Expand Down Expand Up @@ -141,7 +134,6 @@ async function mount(

if (ngZoneEnabled) {
const ngZone: NgZone = ngModuleRefOrAppRef.injector.get(options.NgZone);
const zoneIdentifier: string = instance.zoneIdentifier!;

// `NgZone` can be enabled but routing may not be used thus `getSingleSpaExtraProviders()`
// function was not called.
Expand All @@ -150,7 +142,6 @@ async function mount(
}

instance.bootstrappedNgZone = ngZone;
(instance.bootstrappedNgZone as any)._inner._properties[zoneIdentifier] = true;
window.addEventListener('single-spa:routing-event', instance.routingEventListener!);
}

Expand Down
1 change: 0 additions & 1 deletion libs/single-spa-angular/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ export interface Instance {
// `SingleSpaAngularOpts.NgZone` is a `noop` string and not an `NgZone` class.
bootstrappedNgZone?: NgZone;
routingEventListener?: () => void;
zoneIdentifier?: string;
}

0 comments on commit efd4016

Please sign in to comment.