|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {Injectable, NgZone, OnDestroy, InjectionToken} from '@angular/core'; |
| 9 | +import { |
| 10 | + Injectable, |
| 11 | + NgZone, |
| 12 | + OnDestroy, |
| 13 | + InjectionToken, |
| 14 | + afterNextRender, |
| 15 | + inject, |
| 16 | + Injector, |
| 17 | +} from '@angular/core'; |
10 | 18 | import {from, Subject} from 'rxjs'; |
11 | 19 | import {take, takeUntil} from 'rxjs/operators'; |
12 | 20 |
|
@@ -35,6 +43,8 @@ export class _CoalescedStyleScheduler implements OnDestroy { |
35 | 43 | private _currentSchedule: _Schedule | null = null; |
36 | 44 | private readonly _destroyed = new Subject<void>(); |
37 | 45 |
|
| 46 | + private _injector = inject(Injector); |
| 47 | + |
38 | 48 | constructor(private readonly _ngZone: NgZone) {} |
39 | 49 |
|
40 | 50 | /** |
@@ -69,33 +79,30 @@ export class _CoalescedStyleScheduler implements OnDestroy { |
69 | 79 |
|
70 | 80 | this._currentSchedule = new _Schedule(); |
71 | 81 |
|
72 | | - this._getScheduleObservable() |
73 | | - .pipe(takeUntil(this._destroyed)) |
74 | | - .subscribe(() => { |
75 | | - while (this._currentSchedule!.tasks.length || this._currentSchedule!.endTasks.length) { |
76 | | - const schedule = this._currentSchedule!; |
77 | | - |
78 | | - // Capture new tasks scheduled by the current set of tasks. |
79 | | - this._currentSchedule = new _Schedule(); |
80 | | - |
81 | | - for (const task of schedule.tasks) { |
82 | | - task(); |
83 | | - } |
84 | | - |
85 | | - for (const task of schedule.endTasks) { |
86 | | - task(); |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - this._currentSchedule = null; |
91 | | - }); |
92 | | - } |
93 | | - |
94 | | - private _getScheduleObservable() { |
95 | | - // Use onStable when in the context of an ongoing change detection cycle so that we |
96 | | - // do not accidentally trigger additional cycles. |
97 | | - return this._ngZone.isStable |
98 | | - ? from(Promise.resolve(undefined)) |
99 | | - : this._ngZone.onStable.pipe(take(1)); |
| 82 | + this._ngZone.run(() => |
| 83 | + queueMicrotask(() => |
| 84 | + afterNextRender( |
| 85 | + () => { |
| 86 | + while (this._currentSchedule!.tasks.length || this._currentSchedule!.endTasks.length) { |
| 87 | + const schedule = this._currentSchedule!; |
| 88 | + |
| 89 | + // Capture new tasks scheduled by the current set of tasks. |
| 90 | + this._currentSchedule = new _Schedule(); |
| 91 | + |
| 92 | + for (const task of schedule.tasks) { |
| 93 | + task(); |
| 94 | + } |
| 95 | + |
| 96 | + for (const task of schedule.endTasks) { |
| 97 | + task(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + this._currentSchedule = null; |
| 102 | + }, |
| 103 | + {injector: this._injector}, |
| 104 | + ), |
| 105 | + ), |
| 106 | + ); |
100 | 107 | } |
101 | 108 | } |
0 commit comments