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

Add zoneless scheduler to Angular by default #55102

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/core/src/change_detection/scheduling/flags.ts
@@ -0,0 +1,10 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

/** Flag to enable/disable the zoneless scheduler as default provider with zone scheduling. */
export const alwaysProvideZonelessScheduler = true;
Expand Up @@ -17,6 +17,7 @@ import {performanceMarkFeature} from '../../util/performance';
import {NgZone} from '../../zone';
import {InternalNgZoneOptions} from '../../zone/ng_zone';

import {alwaysProvideZonelessScheduler} from './flags';
import {ChangeDetectionScheduler, ZONELESS_SCHEDULER_DISABLED} from './zoneless_scheduling';
import {ChangeDetectionSchedulerImpl} from './zoneless_scheduling_impl';

Expand Down Expand Up @@ -93,10 +94,12 @@ export function internalProvideZoneChangeDetection(
}
},
{provide: INTERNAL_APPLICATION_ERROR_HANDLER, useFactory: ngZoneApplicationErrorHandlerFactory},
// Always disable scheduler whenever explicitly disabled, even if Hybrid was specified elsewhere
// Always disable scheduler whenever explicitly disabled, even if another place called
// `provideZoneChangeDetection` without the 'ignore' option.
ignoreChangesOutsideZone === true ? {provide: ZONELESS_SCHEDULER_DISABLED, useValue: true} : [],
// Only provide scheduler when explicitly not disabled
ignoreChangesOutsideZone === false ?
// TODO(atscott): This should move to the same places that zone change detection is provided by
// default instead of being in the zone scheduling providers.
alwaysProvideZonelessScheduler || ignoreChangesOutsideZone === false ?
{provide: ChangeDetectionScheduler, useExisting: ChangeDetectionSchedulerImpl} :
[],
];
Expand Down
3 changes: 2 additions & 1 deletion packages/core/test/application_ref_spec.ts
Expand Up @@ -8,7 +8,7 @@

import {DOCUMENT, ɵgetDOM as getDOM} from '@angular/common';
import {ResourceLoader} from '@angular/compiler';
import {APP_BOOTSTRAP_LISTENER, APP_INITIALIZER, ChangeDetectionStrategy, Compiler, CompilerFactory, Component, EnvironmentInjector, InjectionToken, LOCALE_ID, NgModule, NgZone, PlatformRef, RendererFactory2, TemplateRef, Type, ViewChild, ViewContainerRef} from '@angular/core';
import {APP_BOOTSTRAP_LISTENER, APP_INITIALIZER, ChangeDetectionStrategy, Compiler, CompilerFactory, Component, EnvironmentInjector, InjectionToken, LOCALE_ID, NgModule, NgZone, PlatformRef, provideZoneChangeDetection, RendererFactory2, TemplateRef, Type, ViewChild, ViewContainerRef} from '@angular/core';
import {ErrorHandler} from '@angular/core/src/error_handler';
import {ComponentRef} from '@angular/core/src/linker/component_factory';
import {createEnvironmentInjector, getLocaleId} from '@angular/core/src/render3';
Expand Down Expand Up @@ -771,6 +771,7 @@ describe('AppRef', () => {
beforeEach(() => {
stableCalled = false;
TestBed.configureTestingModule({
providers: [provideZoneChangeDetection({ignoreChangesOutsideZone: true})],
declarations: [
SyncComp, MicroTaskComp, MacroTaskComp, MicroMacroTaskComp, MacroMicroTaskComp, ClickComp
],
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/bundling/defer/bundle.golden_symbols.json
Expand Up @@ -1370,6 +1370,9 @@
{
"name": "init_fields"
},
{
"name": "init_flags"
},
{
"name": "init_forward_ref"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/change_detection_scheduler_spec.ts
Expand Up @@ -574,7 +574,9 @@ describe('Angular with scheduler and ZoneJS', () => {
{providers: [{provide: ComponentFixtureAutoDetect, useValue: true}]});
});

it('current default behavior requires updates inside Angular zone', async () => {
it('requires updates inside Angular zone when using ngZoneOnly', async () => {
TestBed.configureTestingModule(
{providers: [provideZoneChangeDetection({ignoreChangesOutsideZone: true})]});
@Component({template: '{{thing()}}', standalone: true})
class App {
thing = signal('initial');
Expand All @@ -593,8 +595,6 @@ describe('Angular with scheduler and ZoneJS', () => {
});

it('updating signal outside of zone still schedules update when in hybrid mode', async () => {
TestBed.configureTestingModule(
{providers: [provideZoneChangeDetection({ignoreChangesOutsideZone: false})]});
@Component({template: '{{thing()}}', standalone: true})
class App {
thing = signal('initial');
Expand Down
12 changes: 0 additions & 12 deletions packages/router/src/router.ts
Expand Up @@ -10,7 +10,6 @@ import {Location} from '@angular/common';
import {
inject,
Injectable,
NgZone,
Type,
ɵConsole as Console,
ɵPendingTasks as PendingTasks,
Expand Down Expand Up @@ -106,7 +105,6 @@ export class Router {
}
private disposed = false;
private nonRouterCurrentEntryChangeSubscription?: SubscriptionLike;
private isNgZoneEnabled = false;

private readonly console = inject(Console);
private readonly stateManager = inject(StateManager);
Expand Down Expand Up @@ -186,8 +184,6 @@ export class Router {
readonly componentInputBindingEnabled: boolean = !!inject(INPUT_BINDER, {optional: true});

constructor() {
this.isNgZoneEnabled = inject(NgZone) instanceof NgZone && NgZone.isInAngularZone();

this.resetConfig(this.config);

this.navigationTransitions
Expand Down Expand Up @@ -522,14 +518,6 @@ export class Router {
skipLocationChange: false,
},
): Promise<boolean> {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
if (this.isNgZoneEnabled && !NgZone.isInAngularZone()) {
this.console.warn(
`Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?`,
);
}
}

const urlTree = isUrlTree(url) ? url : this.parseUrl(url);
const mergedTree = this.urlHandlingStrategy.merge(urlTree, this.rawUrlTree);

Expand Down
10 changes: 8 additions & 2 deletions packages/service-worker/test/provider_spec.ts
Expand Up @@ -166,7 +166,10 @@ const serviceWorkerModuleApi = 'ServiceWorkerModule';
TestBed.configureTestingModule({
providers: [
provideServiceWorker('sw.js'),
{provide: ApplicationRef, useValue: {isStable: isStableSub.asObservable()}},
{
provide: ApplicationRef,
useValue: {isStable: isStableSub.asObservable(), afterTick: new Subject()},
},
{provide: PLATFORM_ID, useValue: 'browser'},
{
provide: SwRegistrationOptions,
Expand All @@ -178,7 +181,10 @@ const serviceWorkerModuleApi = 'ServiceWorkerModule';
TestBed.configureTestingModule({
imports: [ServiceWorkerModule.register('sw.js')],
providers: [
{provide: ApplicationRef, useValue: {isStable: isStableSub.asObservable()}},
{
provide: ApplicationRef,
useValue: {isStable: isStableSub.asObservable(), afterTick: new Subject()},
},
{provide: PLATFORM_ID, useValue: 'browser'},
{
provide: SwRegistrationOptions,
Expand Down