Skip to content

Commit 8e63546

Browse files
atscottwildcardalice
authored andcommitted
refactor(upgrade): downgradeModule uses zone-based change detection by default (angular#63814)
To avoid the need for specifying `provideZoneChangeDetection` in any/all modules used with `downgradedModule`, this change adds the zone-based change detection providers by default. PR Close angular#63814
1 parent 339a1ea commit 8e63546

3 files changed

Lines changed: 14 additions & 21 deletions

File tree

‎packages/core/src/platform/platform_ref.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from '../change_detection/scheduling/ng_zone_scheduling';
1515
import {ChangeDetectionScheduler} from '../change_detection/scheduling/zoneless_scheduling';
1616
import {ChangeDetectionSchedulerImpl} from '../change_detection/scheduling/zoneless_scheduling_impl';
17-
import {Injectable, Injector} from '../di';
17+
import {Injectable, Injector, StaticProvider} from '../di';
1818
import {errorHandlerEnvironmentInitializer} from '../error_handler';
1919
import {RuntimeError, RuntimeErrorCode} from '../errors';
2020
import {Type} from '../interface/type';
@@ -39,6 +39,7 @@ export class PlatformRef {
3939
private _modules: NgModuleRef<any>[] = [];
4040
private _destroyListeners: Array<() => void> = [];
4141
private _destroyed: boolean = false;
42+
private _additionalApplicationProviders?: StaticProvider[];
4243

4344
/** @internal */
4445
constructor(private _injector: Injector) {}
@@ -67,6 +68,7 @@ export class PlatformRef {
6768
ngZoneFactory,
6869
}),
6970
{provide: ChangeDetectionScheduler, useExisting: ChangeDetectionSchedulerImpl},
71+
...(this._additionalApplicationProviders ?? []),
7072
errorHandlerEnvironmentInitializer,
7173
];
7274
const moduleRef = createNgModuleRefWithProviders(

‎packages/upgrade/static/src/downgrade_module.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
PlatformRef,
1414
StaticProvider,
1515
Type,
16+
ɵinternalProvideZoneChangeDetection as internalProvideZoneChangeDetection,
1617
} from '@angular/core';
1718
import {platformBrowser} from '@angular/platform-browser';
1819

@@ -382,12 +383,18 @@ export function downgradeModule<T>(
382383
let bootstrapFn: (extraProviders: StaticProvider[]) => Promise<NgModuleRef<T>>;
383384
if (ɵutil.isNgModuleType(moduleOrBootstrapFn)) {
384385
// NgModule class
385-
bootstrapFn = (extraProviders: StaticProvider[]) =>
386-
platformBrowser(extraProviders).bootstrapModule(moduleOrBootstrapFn);
386+
bootstrapFn = (extraProviders: StaticProvider[]) => {
387+
const platform = platformBrowser(extraProviders);
388+
(platform as any)._additionalApplicationProviders = internalProvideZoneChangeDetection({});
389+
return platform.bootstrapModule(moduleOrBootstrapFn);
390+
};
387391
} else if (!ɵutil.isFunction(moduleOrBootstrapFn)) {
388392
// NgModule factory
389-
bootstrapFn = (extraProviders: StaticProvider[]) =>
390-
platformBrowser(extraProviders).bootstrapModuleFactory(moduleOrBootstrapFn);
393+
bootstrapFn = (extraProviders: StaticProvider[]) => {
394+
const platform = platformBrowser(extraProviders);
395+
(platform as any)._additionalApplicationProviders = internalProvideZoneChangeDetection({});
396+
return platform.bootstrapModuleFactory(moduleOrBootstrapFn);
397+
};
391398
} else {
392399
// bootstrap function
393400
bootstrapFn = moduleOrBootstrapFn;

‎packages/upgrade/static/test/integration/downgrade_module_spec.ts‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
OnChanges,
2929
OnDestroy,
3030
OnInit,
31-
provideZoneChangeDetection,
3231
StaticProvider,
3332
Type,
3433
ViewRef,
@@ -488,7 +487,6 @@ withEachNg1Version(() => {
488487
{provide: 'FOO', useValue: 'Mod-foo'},
489488
{provide: 'BAR', useValue: 'Mod-bar'},
490489
{provide: 'BAZ', useValue: 'Mod-baz'},
491-
provideZoneChangeDetection(),
492490
],
493491
})
494492
class Ng2Module {
@@ -686,7 +684,6 @@ withEachNg1Version(() => {
686684
@NgModule({
687685
declarations: [Ng2AComponent, Ng2BComponent],
688686
imports: [BrowserModule],
689-
providers: [provideZoneChangeDetection()],
690687
})
691688
class Ng2Module {
692689
ngDoBootstrap() {}
@@ -756,7 +753,6 @@ withEachNg1Version(() => {
756753
useFactory: (i: angular.IInjectorService) => i.get('ng1Value'),
757754
deps: ['$injector'],
758755
},
759-
provideZoneChangeDetection(),
760756
],
761757
})
762758
class Ng2Module {
@@ -807,7 +803,6 @@ withEachNg1Version(() => {
807803
@NgModule({
808804
declarations: [Ng2Component],
809805
imports: [BrowserModule],
810-
providers: [provideZoneChangeDetection()],
811806
})
812807
class Ng2Module {
813808
ngDoBootstrap() {}
@@ -847,7 +842,6 @@ withEachNg1Version(() => {
847842
@NgModule({
848843
declarations: [Ng2Component],
849844
imports: [BrowserModule],
850-
providers: [provideZoneChangeDetection()],
851845
})
852846
class Ng2Module {
853847
ngDoBootstrap() {}
@@ -892,7 +886,6 @@ withEachNg1Version(() => {
892886
@NgModule({
893887
declarations: [Ng2Component],
894888
imports: [BrowserModule],
895-
providers: [provideZoneChangeDetection()],
896889
})
897890
class Ng2Module {
898891
ngDoBootstrap() {}
@@ -967,7 +960,6 @@ withEachNg1Version(() => {
967960
@NgModule({
968961
declarations: [TestComponent, WrapperComponent],
969962
imports: [BrowserModule],
970-
providers: [provideZoneChangeDetection()],
971963
})
972964
class Ng2Module {
973965
ngDoBootstrap() {}
@@ -1018,7 +1010,6 @@ withEachNg1Version(() => {
10181010
@NgModule({
10191011
declarations: [Ng2Component],
10201012
imports: [BrowserModule],
1021-
providers: [provideZoneChangeDetection()],
10221013
})
10231014
class Ng2Module {
10241015
ngDoBootstrap() {}
@@ -1073,7 +1064,6 @@ withEachNg1Version(() => {
10731064
@NgModule({
10741065
declarations: [TestComponent, WrapperComponent],
10751066
imports: [BrowserModule],
1076-
providers: [provideZoneChangeDetection()],
10771067
})
10781068
class Ng2Module {
10791069
ngDoBootstrap() {}
@@ -1167,7 +1157,6 @@ withEachNg1Version(() => {
11671157
@NgModule({
11681158
declarations: [Ng2Component],
11691159
imports: [BrowserModule],
1170-
providers: [provideZoneChangeDetection()],
11711160
})
11721161
class Ng2Module {
11731162
ngDoBootstrap() {}
@@ -1310,7 +1299,6 @@ withEachNg1Version(() => {
13101299
@NgModule({
13111300
declarations: [Ng2Component],
13121301
imports: [BrowserModule],
1313-
providers: [provideZoneChangeDetection()],
13141302
})
13151303
class Ng2Module {
13161304
ngDoBootstrap() {}
@@ -1362,7 +1350,6 @@ withEachNg1Version(() => {
13621350
@NgModule({
13631351
declarations: [Ng2Component],
13641352
imports: [BrowserModule],
1365-
providers: [provideZoneChangeDetection()],
13661353
})
13671354
class Ng2Module {
13681355
ngDoBootstrap() {}
@@ -1432,7 +1419,6 @@ withEachNg1Version(() => {
14321419

14331420
@NgModule({
14341421
declarations: [Ng2Component],
1435-
providers: [provideZoneChangeDetection()],
14361422
imports: [BrowserModule],
14371423
})
14381424
class Ng2Module {
@@ -1493,7 +1479,6 @@ withEachNg1Version(() => {
14931479
@NgModule({
14941480
declarations: [Ng2Component],
14951481
imports: [BrowserModule],
1496-
providers: [provideZoneChangeDetection()],
14971482
})
14981483
class Ng2Module {
14991484
constructor(injector: Injector) {
@@ -1528,7 +1513,6 @@ withEachNg1Version(() => {
15281513
@NgModule({
15291514
declarations: [Ng2Component],
15301515
imports: [BrowserModule],
1531-
providers: [provideZoneChangeDetection()],
15321516
})
15331517
class Ng2Module {
15341518
ngDoBootstrap() {}

0 commit comments

Comments
 (0)