rxjs-zone-less
Version:
A set of wrappers for RxJS to avoid unnecessary change detection and zone interference in Angular.
52 lines (51 loc) • 2.41 kB
JavaScript
import { __extends } from "tslib";
import { immediateProvider } from '../../internals/immediateProvider';
import { AsyncAction } from '../async/AsyncAction';
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var AsapAction = /** @class */ (function (_super) {
__extends(AsapAction, _super);
function AsapAction(scheduler, work) {
var _this = _super.call(this, scheduler, work) || this;
_this.scheduler = scheduler;
_this.work = work;
return _this;
}
AsapAction.prototype.requestAsyncId = function (scheduler, id, delay) {
if (delay === void 0) { delay = 0; }
// If delay is greater than 0, request as an async action.
if (delay !== null && delay > 0) {
return _super.prototype.requestAsyncId.call(this, scheduler, id, delay);
}
// Push the action to the end of the scheduler queue.
scheduler.actions.push(this);
// If a microtask has already been scheduled, don't schedule another
// one. If a microtask hasn't been scheduled yet, schedule one now. Return
// the current scheduled microtask id.
return (scheduler._scheduled ||
(scheduler._scheduled = immediateProvider.setImmediate(scheduler.flush.bind(scheduler, undefined))));
};
AsapAction.prototype.recycleAsyncId = function (scheduler, id, delay) {
if (delay === void 0) { delay = 0; }
// If delay exists and is greater than 0, or if the delay is null (the
// action wasn't rescheduled) but was originally scheduled as an async
// action, then recycle as an async action.
if ((delay !== null && delay > 0) || (delay === null && this.delay > 0)) {
return _super.prototype.recycleAsyncId.call(this, scheduler, id, delay);
}
// If the scheduler queue is empty, cancel the requested microtask and
// set the scheduled flag to undefined so the next AsapAction will schedule
// its own.
if (!scheduler.actions.some(function (action) { return action.id === id; })) {
immediateProvider.clearImmediate(id);
scheduler._scheduled = undefined;
}
// Return undefined so the action knows to request a new async id if it's rescheduled.
return undefined;
};
return AsapAction;
}(AsyncAction));
export { AsapAction };