Skip to content

Commit 253eb1e

Browse files
committed
Copy in style scheduler changes
1 parent 0544e43 commit 253eb1e

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

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

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Injectable, NgZone} from '@angular/core';
10-
import {from, Observable} from 'rxjs';
9+
import {Injectable, NgZone, OnDestroy} from '@angular/core';
10+
import {Subject} from 'rxjs';
11+
import {take, takeUntil} from 'rxjs/operators';
12+
13+
/**
14+
* @docs-private
15+
*/
16+
export class _Schedule {
17+
tasks: (() => unknown)[] = [];
18+
endTasks: (() => unknown)[] = [];
19+
}
1120

1221
/**
1322
* Allows grouping up CSSDom mutations after the current execution context.
@@ -17,40 +26,55 @@ import {from, Observable} from 'rxjs';
1726
* @docs-private
1827
*/
1928
@Injectable()
20-
export class _CoalescedStyleScheduler {
21-
private _currentSchedule: Observable<void>|null = null;
29+
export class _CoalescedStyleScheduler implements OnDestroy {
30+
private _currentSchedule: _Schedule|null = null;
31+
private readonly _destroyed = new Subject<void>();
2232

2333
constructor(private readonly _ngZone: NgZone) {}
2434

2535
/**
26-
* Schedules the specified task to run after the current microtask.
36+
* Schedules the specified task to run at the end of the current VM turn.
2737
*/
2838
schedule(task: () => unknown): void {
2939
this._createScheduleIfNeeded();
3040

31-
this._currentSchedule!.subscribe(task);
41+
this._currentSchedule!.tasks.push(task);
3242
}
3343

3444
/**
35-
* Schedules the specified task to run after all scheduled tasks.
36-
* This is useful when measuring after style updates is required.
45+
* Schedules the specified task to run after other scheduled tasks at the end of the current
46+
* VM turn.
3747
*/
3848
scheduleEnd(task: () => unknown): void {
3949
this._createScheduleIfNeeded();
4050

41-
this._currentSchedule!.subscribe(() => {
42-
this.schedule(task);
43-
});
51+
this._currentSchedule!.endTasks.push(task);
52+
}
53+
54+
/** Prevent any further tasks from running. */
55+
ngOnDestroy() {
56+
this._destroyed.next();
57+
this._destroyed.complete();
4458
}
4559

4660
private _createScheduleIfNeeded() {
4761
if (this._currentSchedule) { return; }
4862

49-
this._ngZone.runOutsideAngular(() => {
50-
this._currentSchedule = from(new Promise<void>((resolve) => {
51-
this._currentSchedule = null;
52-
resolve(undefined);
53-
}));
63+
this._currentSchedule = new _Schedule();
64+
65+
this._ngZone.onStable.pipe(
66+
take(1),
67+
takeUntil(this._destroyed),
68+
).subscribe(() => {
69+
const schedule = this._currentSchedule!;
70+
this._currentSchedule = null;
71+
72+
for (const task of schedule.tasks) {
73+
task();
74+
}
75+
for (const task of schedule.endTasks) {
76+
task();
77+
}
5478
});
5579
}
5680
}

0 commit comments

Comments
 (0)