choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
114 lines (92 loc) • 2.76 kB
JavaScript
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import { __decorate } from "tslib";
import { action, observable, runInAction } from 'mobx';
var PromiseQueue =
/*#__PURE__*/
function () {
function PromiseQueue() {
var _this = this;
_classCallCheck(this, PromiseQueue);
this.queueing = false;
runInAction(function () {
_this.queue = [];
});
}
_createClass(PromiseQueue, [{
key: "clear",
value: function clear(promise) {
if (!this.queueing) {
var queue = this.queue;
var index = queue.indexOf(promise);
if (index !== -1) {
queue.splice(index, 1);
}
}
}
}, {
key: "add",
value: function add(promise) {
var _this2 = this;
var queue = this.queue;
queue.push(promise);
return promise.then(function (value) {
_this2.clear(promise);
return value;
})["catch"](function (error) {
_this2.clear(promise);
throw error;
});
}
}, {
key: "ready",
value: function () {
var _ready = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee() {
var queue;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
this.queueing = true;
queue = this.queue;
if (!queue.length) {
_context.next = 6;
break;
}
_context.next = 5;
return queue.pop();
case 5:
return _context.abrupt("return", this.ready());
case 6:
this.queueing = false;
return _context.abrupt("return", Promise.resolve());
case 8:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function ready() {
return _ready.apply(this, arguments);
}
return ready;
}()
}, {
key: "length",
get: function get() {
return this.queue.length;
}
}]);
return PromiseQueue;
}();
export { PromiseQueue as default };
__decorate([observable], PromiseQueue.prototype, "queue", void 0);
__decorate([action], PromiseQueue.prototype, "clear", null);
__decorate([action], PromiseQueue.prototype, "add", null);
__decorate([action], PromiseQueue.prototype, "ready", null);
//# sourceMappingURL=PromiseQueue.js.map