@progress/kendo-angular-common
Version:
Kendo UI for Angular - Utility Package
68 lines (67 loc) • 2.55 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
*/
var ResizeBatchService = /** @class */ (function () {
function ResizeBatchService(ngZone) {
this.ngZone = ngZone;
this.scheduled = [];
this.resolvedPromise = Promise.resolve(null);
this.flush = this.flush.bind(this);
}
ResizeBatchService.prototype.schedule = function (instance, method) {
var _this = this;
this.scheduled.push({ instance: instance, method: method });
if (!this.subscription) {
this.ngZone.runOutsideAngular(function () {
_this.subscription = fromPromise(_this.resolvedPromise)
.subscribe(_this.flush);
});
}
};
ResizeBatchService.prototype.isScheduled = function (instance) {
return Boolean(this.scheduled.find(function (item) { return item.instance === instance; }));
};
ResizeBatchService.prototype.cancel = function (instance) {
var scheduled = this.scheduled;
var count = scheduled.length;
for (var idx = 0; idx < count; idx++) {
if (scheduled[idx].instance === instance) {
scheduled.splice(idx, 1);
if (!scheduled.length) {
this.unsubscribe();
}
return;
}
}
};
ResizeBatchService.prototype.ngOnDestroy = function () {
this.unsubscribe();
};
ResizeBatchService.prototype.unsubscribe = function () {
if (this.subscription) {
this.subscription.unsubscribe();
this.subscription = null;
}
};
ResizeBatchService.prototype.flush = function () {
this.scheduled.forEach(function (item) {
item.method.call(item.instance);
});
this.scheduled = [];
this.unsubscribe();
};
ResizeBatchService = tslib_1.__decorate([
Injectable(),
tslib_1.__metadata("design:paramtypes", [NgZone])
], ResizeBatchService);
return ResizeBatchService;
}());
export { ResizeBatchService };