Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions goldens/size-tracking/integration-payloads.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"cli-hello-world": {
"uncompressed": {
"main": 132425,
"polyfills": 33792
"main": 137461,
"polyfills": 34579
}
},
"cli-hello-world-ivy-i18n": {
Expand Down
33 changes: 24 additions & 9 deletions packages/core/src/testability/testability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {Inject, Injectable, InjectionToken} from '../di';
import {inject, Inject, Injectable, InjectionToken} from '../di';
import {isInInjectionContext} from '../di/contextual';
import {DestroyRef} from '../linker/destroy_ref';
import {NgZone} from '../zone/ng_zone';

/**
Expand Down Expand Up @@ -84,13 +86,21 @@ export class Testability implements PublicTestability {
private _isZoneStable: boolean = true;
private _callbacks: WaitCallback[] = [];

private taskTrackingZone: {macroTasks: Task[]} | null = null;
private _taskTrackingZone: {macroTasks: Task[]} | null = null;

private _destroyRef?: DestroyRef;

constructor(
private _ngZone: NgZone,
private registry: TestabilityRegistry,
@Inject(TESTABILITY_GETTER) testabilityGetter: GetTestability,
) {
// Attempt to retrieve a `DestroyRef` optionally.
// For backwards compatibility reasons, this cannot be required.
if (isInInjectionContext()) {
this._destroyRef = inject(DestroyRef, {optional: true}) ?? undefined;
}

// If there was no Testability logic registered in the global scope
// before, register the current testability getter as a global one.
if (!_testabilityGetter) {
Expand All @@ -99,19 +109,19 @@ export class Testability implements PublicTestability {
}
this._watchAngularEvents();
_ngZone.run(() => {
this.taskTrackingZone =
this._taskTrackingZone =
typeof Zone == 'undefined' ? null : Zone.current.get('TaskTrackingZone');
});
}

private _watchAngularEvents(): void {
this._ngZone.onUnstable.subscribe({
const onUnstableSubscription = this._ngZone.onUnstable.subscribe({
next: () => {
this._isZoneStable = false;
},
});

this._ngZone.runOutsideAngular(() => {
const onStableSubscription = this._ngZone.runOutsideAngular(() =>
this._ngZone.onStable.subscribe({
next: () => {
NgZone.assertNotInAngularZone();
Expand All @@ -120,7 +130,12 @@ export class Testability implements PublicTestability {
this._runCallbacksIfReady();
});
},
});
}),
);

this._destroyRef?.onDestroy(() => {
onUnstableSubscription.unsubscribe();
onStableSubscription.unsubscribe();
});
}

Expand Down Expand Up @@ -156,12 +171,12 @@ export class Testability implements PublicTestability {
}

private getPendingTasks(): PendingMacrotask[] {
if (!this.taskTrackingZone) {
if (!this._taskTrackingZone) {
return [];
}

// Copy the tasks data so that we don't leak tasks.
return this.taskTrackingZone.macroTasks.map((t: Task) => {
return this._taskTrackingZone.macroTasks.map((t: Task) => {
return {
source: t.source,
// From TaskTrackingZone:
Expand Down Expand Up @@ -196,7 +211,7 @@ export class Testability implements PublicTestability {
* and no further updates will be issued.
*/
whenStable(doneCb: Function, timeout?: number, updateCb?: Function): void {
if (updateCb && !this.taskTrackingZone) {
if (updateCb && !this._taskTrackingZone) {
throw new Error(
'Task tracking zone is required when passing an update callback to ' +
'whenStable(). Is "zone.js/plugins/task-tracking" loaded?',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@
"isFormControlState",
"isForwardRef",
"isFunction",
"isInInjectionContext",
"isInlineTemplate",
"isInputBinding",
"isInteropObservable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@
"isFormControlState",
"isForwardRef",
"isFunction",
"isInInjectionContext",
"isInlineTemplate",
"isInputBinding",
"isInteropObservable",
Expand Down