Skip to content

Commit

Permalink
refactor(core): Remove hybrid mode flag and move scheduler provider l…
Browse files Browse the repository at this point in the history
…ocation

The flag is not used anymore and, as a result, is easier to move the
scheduler provider.
  • Loading branch information
atscott committed May 21, 2024
1 parent 048847f commit 1df5e1c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 29 deletions.
12 changes: 10 additions & 2 deletions packages/core/src/application/create_application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import {NgZone} from '../zone/ng_zone';

import {ApplicationInitStatus} from './application_init';
import {_callAndReportToErrorHandler, ApplicationRef} from './application_ref';
import {PROVIDED_ZONELESS} from '../change_detection/scheduling/zoneless_scheduling';
import {
PROVIDED_ZONELESS,
ChangeDetectionScheduler,
} from '../change_detection/scheduling/zoneless_scheduling';
import {ChangeDetectionSchedulerImpl} from '../change_detection/scheduling/zoneless_scheduling_impl';

/**
* Internal create application API that implements the core application creation logic and optional
Expand Down Expand Up @@ -59,7 +63,11 @@ export function internalCreateApplication(config: {

// Create root application injector based on a set of providers configured at the platform
// bootstrap level as well as providers passed to the bootstrap call by a user.
const allAppProviders = [internalProvideZoneChangeDetection({}), ...(appProviders || [])];
const allAppProviders = [
internalProvideZoneChangeDetection({}),
{provide: ChangeDetectionScheduler, useExisting: ChangeDetectionSchedulerImpl},
...(appProviders || []),
];
const adapter = new EnvironmentNgModuleRefAdapter({
providers: allAppProviders,
parent: platformInjector as EnvironmentInjector,
Expand Down
10 changes: 0 additions & 10 deletions packages/core/src/change_detection/scheduling/flags.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@ import {performanceMarkFeature} from '../../util/performance';
import {NgZone} from '../../zone';
import {InternalNgZoneOptions} from '../../zone/ng_zone';

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

@Injectable({providedIn: 'root'})
export class NgZoneChangeDetectionScheduler {
private readonly zone = inject(NgZone);
private readonly changeDetectionScheduler = inject(ChangeDetectionScheduler, {optional: true});
private readonly changeDetectionScheduler = inject(ChangeDetectionScheduler);
private readonly applicationRef = inject(ApplicationRef);

private _onMicrotaskEmptySubscription?: Subscription;
Expand All @@ -51,7 +49,7 @@ export class NgZoneChangeDetectionScheduler {
// `onMicroTaskEmpty` can happen _during_ the zoneless scheduler change detection because
// zone.run(() => {}) will result in `checkStable` at the end of the `zone.run` closure
// and emit `onMicrotaskEmpty` synchronously if run coalsecing is false.
if (this.changeDetectionScheduler?.runningTick) {
if (this.changeDetectionScheduler.runningTick) {
return;
}
this.zone.run(() => {
Expand Down Expand Up @@ -119,11 +117,6 @@ export function internalProvideZoneChangeDetection({
// Always disable scheduler whenever explicitly disabled, even if another place called
// `provideZoneChangeDetection` without the 'ignore' option.
ignoreChangesOutsideZone === true ? {provide: ZONELESS_SCHEDULER_DISABLED, useValue: true} : [],
// 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
1 change: 1 addition & 0 deletions packages/core/src/core_private_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {
defaultKeyValueDiffers as ɵdefaultKeyValueDiffers,
} from './change_detection/change_detection';
export {internalProvideZoneChangeDetection as ɵinternalProvideZoneChangeDetection} from './change_detection/scheduling/ng_zone_scheduling';
export {ChangeDetectionSchedulerImpl as ɵChangeDetectionSchedulerImpl} from './change_detection/scheduling/zoneless_scheduling_impl';
export {
ChangeDetectionScheduler as ɵChangeDetectionScheduler,
NotificationSource as ɵNotificationSource,
Expand Down
18 changes: 12 additions & 6 deletions packages/core/src/platform/platform_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {
internalProvideZoneChangeDetection,
PROVIDED_NG_ZONE,
} from '../change_detection/scheduling/ng_zone_scheduling';
import {ZONELESS_ENABLED} from '../change_detection/scheduling/zoneless_scheduling';
import {
ChangeDetectionScheduler,
ZONELESS_ENABLED,
} from '../change_detection/scheduling/zoneless_scheduling';
import {ChangeDetectionSchedulerImpl} from '../change_detection/scheduling/zoneless_scheduling_impl';
import {Injectable, InjectionToken, Injector} from '../di';
import {ErrorHandler} from '../error_handler';
import {RuntimeError, RuntimeErrorCode} from '../errors';
Expand Down Expand Up @@ -89,11 +93,13 @@ export class PlatformRef {
// created outside of the Angular zone.
return ngZone.run(() => {
const ignoreChangesOutsideZone = options?.ignoreChangesOutsideZone;
const moduleRef = createNgModuleRefWithProviders(
moduleFactory.moduleType,
this.injector,
internalProvideZoneChangeDetection({ngZoneFactory: () => ngZone, ignoreChangesOutsideZone}),
);
const moduleRef = createNgModuleRefWithProviders(moduleFactory.moduleType, this.injector, [
...internalProvideZoneChangeDetection({
ngZoneFactory: () => ngZone,
ignoreChangesOutsideZone,
}),
{provide: ChangeDetectionScheduler, useExisting: ChangeDetectionSchedulerImpl},
]);

if (typeof ngDevMode === 'undefined' || ngDevMode) {
if (moduleRef.injector.get(PROVIDED_NG_ZONE)) {
Expand Down
8 changes: 7 additions & 1 deletion packages/core/testing/src/test_bed_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import {ResourceLoader} from '@angular/compiler';
import {
ApplicationInitStatus,
ɵChangeDetectionScheduler as ChangeDetectionScheduler,
ɵChangeDetectionSchedulerImpl as ChangeDetectionSchedulerImpl,
Compiler,
COMPILER_OPTIONS,
Component,
Expand Down Expand Up @@ -926,7 +928,11 @@ export class TestBedCompiler {
private compileTestModule(): void {
class RootScopeModule {}
compileNgModuleDefs(RootScopeModule as NgModuleType<any>, {
providers: [...this.rootProviderOverrides, internalProvideZoneChangeDetection({})],
providers: [
...this.rootProviderOverrides,
internalProvideZoneChangeDetection({}),
{provide: ChangeDetectionScheduler, useExisting: ChangeDetectionSchedulerImpl},
],
});

const providers = [
Expand Down
3 changes: 3 additions & 0 deletions packages/platform-browser/testing/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
platformCore,
StaticProvider,
ɵinternalProvideZoneChangeDetection as internalProvideZoneChangeDetection,
ɵChangeDetectionScheduler as ChangeDetectionScheduler,
ɵChangeDetectionSchedulerImpl as ChangeDetectionSchedulerImpl,
} from '@angular/core';
import {BrowserModule, ɵBrowserDomAdapter as BrowserDomAdapter} from '@angular/platform-browser';

Expand Down Expand Up @@ -47,6 +49,7 @@ export const platformBrowserTesting = createPlatformFactory(
providers: [
{provide: APP_ID, useValue: 'a'},
internalProvideZoneChangeDetection({}),
{provide: ChangeDetectionScheduler, useExisting: ChangeDetectionSchedulerImpl},
{provide: PlatformLocation, useClass: MockPlatformLocation},
],
})
Expand Down

0 comments on commit 1df5e1c

Please sign in to comment.