/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { Injectable, NgZone, InjectionToken } from '@angular/core'; import { from, Subject } from 'rxjs'; import { take, takeUntil } from 'rxjs/operators'; /** * @docs-private */ export class _Schedule { constructor() { this.tasks = []; this.endTasks = []; } } /** Injection token used to provide a coalesced style scheduler. */ export const _COALESCED_STYLE_SCHEDULER = new InjectionToken('_COALESCED_STYLE_SCHEDULER'); /** * Allows grouping up CSSDom mutations after the current execution context. * This can significantly improve performance when separate consecutive functions are * reading from the CSSDom and then mutating it. * * @docs-private */ export class _CoalescedStyleScheduler { constructor(_ngZone) { this._ngZone = _ngZone; this._currentSchedule = null; this._destroyed = new Subject(); } /** * Schedules the specified task to run at the end of the current VM turn. */ schedule(task) { this._createScheduleIfNeeded(); this._currentSchedule.tasks.push(task); } /** * Schedules the specified task to run after other scheduled tasks at the end of the current * VM turn. */ scheduleEnd(task) { this._createScheduleIfNeeded(); this._currentSchedule.endTasks.push(task); } /** Prevent any further tasks from running. */ ngOnDestroy() { this._destroyed.next(); this._destroyed.complete(); } _createScheduleIfNeeded() { if (this._currentSchedule) { return; } this._currentSchedule = new _Schedule(); this._getScheduleObservable().pipe(takeUntil(this._destroyed)).subscribe(() => { while (this._currentSchedule.tasks.length || this._currentSchedule.endTasks.length) { const schedule = this._currentSchedule; // Capture new tasks scheduled by the current set of tasks. this._currentSchedule = new _Schedule(); for (const task of schedule.tasks) { task(); } for (const task of schedule.endTasks) { task(); } } this._currentSchedule = null; }); } _getScheduleObservable() { // Use onStable when in the context of an ongoing change detection cycle so that we // do not accidentally trigger additional cycles. return this._ngZone.isStable ? from(Promise.resolve(undefined)) : this._ngZone.onStable.pipe(take(1)); } } _CoalescedStyleScheduler.decorators = [ { type: Injectable } ]; _CoalescedStyleScheduler.ctorParameters = () => [ { type: NgZone } ]; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29h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