podchat-browser
Version:
Javascript SDK to use POD's Chat Service - Browser Only
82 lines (69 loc) • 2.23 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var MessageExecutorQueue = /*#__PURE__*/function () {
function MessageExecutorQueue(_ref) {
var app = _ref.app,
_ref$executorTimeout = _ref.executorTimeout,
executorTimeout = _ref$executorTimeout === void 0 ? 10 : _ref$executorTimeout,
callback = _ref.callback;
(0, _classCallCheck2["default"])(this, MessageExecutorQueue);
this._app = app;
this._callMessageQueue = [];
this._executorTimeout = executorTimeout;
this._executionStarted = false;
this._callback = callback;
}
(0, _createClass2["default"])(MessageExecutorQueue, [{
key: "_executeCallMessageQueue",
value: function _executeCallMessageQueue() {
if (!this._executionStarted) {
this._executionStarted = true;
this._doExecution();
}
}
}, {
key: "_doExecution",
value: function _doExecution() {
var _this = this;
if (this._callMessageQueue.length) {
this._execute().then(function () {
_this._doExecution();
});
} else {
/**
* let other messages start the process
* @type {boolean}
* @private
*/
this._executionStarted = false;
}
}
}, {
key: "_execute",
value: function _execute() {
var _this2 = this;
return new Promise(function (resolve, reject) {
var message = _this2._callMessageQueue.shift();
_this2._callback(message);
setTimeout(function () {
resolve();
}, _this2._executorTimeout);
});
}
}, {
key: "pushCallMessageToQueue",
value: function pushCallMessageToQueue(message) {
this._callMessageQueue.push(message);
this._executeCallMessageQueue();
}
}]);
return MessageExecutorQueue;
}();
var _default = MessageExecutorQueue;
exports["default"] = _default;