UNPKG

@codianz/rx

Version:
112 lines 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InflowRestriction = void 0; var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var rx_1 = require("./rx"); var InflowRestriction = /** @class */ (function () { function InflowRestriction(limit, log) { this.m_caller = new rxjs_1.Subject(); this.m_queue = []; this.m_running = []; this.m_runnerId = 0; this.m_status = new rxjs_1.BehaviorSubject({ waitings: 0, runnings: 0, total: 0 }); this.m_limit = limit; this.m_log = log; } InflowRestriction.prototype.emitStatus = function (memo) { var status = { waitings: this.m_queue.length, runnings: this.m_running.length, total: this.m_runnerId }; this.m_log.debug("InflowRestriction.emitStatus " + memo + ": ", status); this.m_status.next(status); }; Object.defineProperty(InflowRestriction.prototype, "Status", { get: function () { return this.m_status.asObservable(); }, enumerable: false, configurable: true }); InflowRestriction.prototype.reset = function () { this.m_log.debug("InflowRestriction.reset"); this.m_caller.complete(); this.m_caller = new rxjs_1.Subject(); this.m_queue = []; this.m_running = []; this.m_runnerId = 0; this.emitStatus("reset()"); }; InflowRestriction.prototype.checkIn = function (memo, id) { this.m_log.debug("InflowRestriction.checkIn " + memo); if (this.m_running.length >= this.m_limit) { this.m_log.debug("InflowRestriction.checkIn " + memo + " -> queue"); this.m_queue.push(id); } else { this.m_log.debug("InflowRestriction.checkIn " + memo + " -> start"); this.m_running.push(id); this.m_caller.next(id); } this.emitStatus(memo); }; InflowRestriction.prototype.checkOut = function (memo, id) { this.m_log.debug("InflowRestriction.checkOut " + memo); this.m_running = this.m_running.filter(function (x) { return x !== id; }); var next = this.m_queue.shift(); if (next !== undefined) { this.m_log.debug("InflowRestriction.checkOut " + memo + " -> start next"); this.m_running.push(next); this.m_caller.next(next); } this.emitStatus(memo); }; // prettier-ignore InflowRestriction.prototype.enter = function (memo, func) { var _this = this; var id = ++this.m_runnerId; var _memo = "(" + id + ") " + memo; this.m_log.info("InflowRestriction start " + _memo); return rx_1.readySetGo(function () { _this.checkIn(_memo, id); }, this.m_caller) .pipe(operators_1.observeOn(rxjs_1.asyncScheduler)) .pipe(operators_1.mergeMap(function (x) { if (x !== id) return rxjs_1.NEVER; return func(); })) .pipe(operators_1.take(1)) .pipe(operators_1.catchError(function (err) { _this.m_log.error("InflowRestriction error " + _memo + ": " + err); _this.checkOut(_memo, id); return rxjs_1.throwError(function () { return err; }); })) .pipe(operators_1.map(function (x) { _this.checkOut(_memo, id); _this.m_log.info("InflowRestriction finish " + _memo); return x; })); }; InflowRestriction.prototype.enterWithArray = function (memo, items, fn) { var _this = this; var numItems = items.length; // prettier-ignore return rxjs_1.from(items) .pipe(operators_1.mergeMap(function (item, n) { return _this.enter(memo + " [" + n + "/" + numItems + "]", function () { return fn(item); }); })) .pipe(operators_1.take(numItems)); }; return InflowRestriction; }()); exports.InflowRestriction = InflowRestriction; //# sourceMappingURL=InflowRestriction.js.map