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

fix: assertInAngularZone should act as a noop #512

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}