Skip to content

Commit

Permalink
fix: assertInAngularZone should act as a noop (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturovt authored Apr 19, 2024
1 parent afae0ed commit 77b5f5c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
22 changes: 7 additions & 15 deletions libs/single-spa-angular/src/single-spa-angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,20 @@ export function singleSpaAngular<T>(userOptions: SingleSpaAngularOptions<T>): Li
};
}

async function bootstrap(options: BootstrappedSingleSpaAngularOptions, props: any): Promise<void> {
async function bootstrap(options: BootstrappedSingleSpaAngularOptions): Promise<void> {
// Angular provides an opportunity to develop `zone-less` application, where developers
// have to trigger change detection manually.
// See https://angular.io/guide/zone#noopzone
if (options.NgZone === 'noop') {
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(() => {
Expand Down Expand Up @@ -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.
Expand All @@ -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!);
}

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 @@ -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;
}

0 comments on commit 77b5f5c

Please sign in to comment.