@progress/kendo-angular-common
Version:
Kendo UI for Angular - Utility Package
66 lines (65 loc) • 2.16 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2020 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import * as tslib_1 from "tslib";
import { Injectable, NgZone } from '@angular/core';
import { from as fromPromise } from 'rxjs';
/* tslint:disable:align */
/**
* @hidden
*/
let ResizeBatchService = class ResizeBatchService {
constructor(ngZone) {
this.ngZone = ngZone;
this.scheduled = [];
this.resolvedPromise = Promise.resolve(null);
this.flush = this.flush.bind(this);
}
schedule(instance, method) {
this.scheduled.push({ instance, method });
if (!this.subscription) {
this.ngZone.runOutsideAngular(() => {
this.subscription = fromPromise(this.resolvedPromise)
.subscribe(this.flush);
});
}
}
isScheduled(instance) {
return Boolean(this.scheduled.find(item => item.instance === instance));
}
cancel(instance) {
const scheduled = this.scheduled;
const count = scheduled.length;
for (let idx = 0; idx < count; idx++) {
if (scheduled[idx].instance === instance) {
scheduled.splice(idx, 1);
if (!scheduled.length) {
this.unsubscribe();
}
return;
}
}
}
ngOnDestroy() {
this.unsubscribe();
}
unsubscribe() {
if (this.subscription) {
this.subscription.unsubscribe();
this.subscription = null;
}
}
flush() {
this.scheduled.forEach(item => {
item.method.call(item.instance);
});
this.scheduled = [];
this.unsubscribe();
}
};
ResizeBatchService = tslib_1.__decorate([
Injectable(),
tslib_1.__metadata("design:paramtypes", [NgZone])
], ResizeBatchService);
export { ResizeBatchService };