Skip to content

Commit af57fc2

Browse files
committed
refactor(material/table): remove use of zone onStable for style scheduling
1 parent dba9e24 commit af57fc2

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

src/cdk/table/coalesced-style-scheduler.ts

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

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';
1018
import {from, Subject} from 'rxjs';
1119
import {take, takeUntil} from 'rxjs/operators';
1220

@@ -35,6 +43,8 @@ export class _CoalescedStyleScheduler implements OnDestroy {
3543
private _currentSchedule: _Schedule | null = null;
3644
private readonly _destroyed = new Subject<void>();
3745

46+
private _injector = inject(Injector);
47+
3848
constructor(private readonly _ngZone: NgZone) {}
3949

4050
/**
@@ -69,33 +79,30 @@ export class _CoalescedStyleScheduler implements OnDestroy {
6979

7080
this._currentSchedule = new _Schedule();
7181

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+
);
100107
}
101108
}

0 commit comments

Comments
 (0)