license import { Injectable , NgZone , OnDestroy , InjectionToken } from '@angular/core' ; import { from , Subject } from 'rxjs' ; import { take , takeUntil } from 'rxjs/operators' ; docs-private export class _Schedule { tasks : ( ( ) => unknown ) [ ] = [ ] ; endTasks : ( ( ) => unknown ) [ ] = [ ] ; } /** Injection token used to provide a coalesced style scheduler. */ export const _COALESCED_STYLE_SCHEDULER = new InjectionToken < _CoalescedStyleScheduler > ( '_COALESCED_STYLE_SCHEDULER' ) ; docs-private @ Injectable ( ) export class _CoalescedStyleScheduler implements OnDestroy { private _currentSchedule : _Schedule | null = null ; private readonly _destroyed = new Subject < void > ( ) ; constructor ( private readonly _ngZone ) { } /**
     * 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 ( ) ; } private _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 ! ; this . _currentSchedule = new _Schedule ( ) ; for ( const task of schedule . tasks ) { task ( ) ; } for ( const task of schedule . endTasks ) { task ( ) ; } } this . _currentSchedule = null ; } ) ; } private _getScheduleObservable ( ) { return this . _ngZone . isStable ? from ( Promise . resolve ( undefined ) ) : this . _ngZone . onStable . pipe ( take ( 1 ) ) ; } }