|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | import {Injectable, NgZone, OnDestroy} from '@angular/core';
|
10 |
| -import {Subject} from 'rxjs'; |
| 10 | +import {from, Subject} from 'rxjs'; |
11 | 11 | import {take, takeUntil} from 'rxjs/operators';
|
12 | 12 |
|
13 | 13 | /**
|
@@ -62,19 +62,31 @@ export class _CoalescedStyleScheduler implements OnDestroy {
|
62 | 62 |
|
63 | 63 | this._currentSchedule = new _Schedule();
|
64 | 64 |
|
65 |
| - this._ngZone.onStable.pipe( |
66 |
| - take(1), |
| 65 | + this._getScheduleObservable().pipe( |
67 | 66 | takeUntil(this._destroyed),
|
68 | 67 | ).subscribe(() => {
|
69 |
| - const schedule = this._currentSchedule!; |
70 |
| - this._currentSchedule = null; |
| 68 | + while (this._currentSchedule!.tasks.length || this._currentSchedule!.endTasks.length) { |
| 69 | + const schedule = this._currentSchedule!; |
| 70 | + this._currentSchedule = new _Schedule(); |
71 | 71 |
|
72 |
| - for (const task of schedule.tasks) { |
73 |
| - task(); |
74 |
| - } |
75 |
| - for (const task of schedule.endTasks) { |
76 |
| - task(); |
| 72 | + for (const task of schedule.tasks) { |
| 73 | + task(); |
| 74 | + } |
| 75 | + |
| 76 | + for (const task of schedule.endTasks) { |
| 77 | + task(); |
| 78 | + } |
77 | 79 | }
|
| 80 | + |
| 81 | + this._currentSchedule = null; |
78 | 82 | });
|
79 | 83 | }
|
| 84 | + |
| 85 | + private _getScheduleObservable() { |
| 86 | + // Use onStable when in the context of an ongoing change detection cycle so that we |
| 87 | + // do not accidentally trigger additional cycles. |
| 88 | + return this._ngZone.isStable ? |
| 89 | + from(Promise.resolve(undefined)) : |
| 90 | + this._ngZone.onStable.pipe(take(1)); |
| 91 | + } |
80 | 92 | }
|
0 commit comments