podchat-browser
Version:
Javascript SDK to use POD's Chat Service - Browser Only
48 lines (43 loc) • 1.31 kB
JavaScript
class MessageExecutorQueue {
constructor({app, executorTimeout = 10, callback}) {
this._app = app;
this._callMessageQueue = []
this._executorTimeout = executorTimeout;
this._executionStarted = false;
this._callback = callback;
}
_executeCallMessageQueue() {
if(!this._executionStarted) {
this._executionStarted = true;
this._doExecution();
}
}
_doExecution() {
if(this._callMessageQueue.length) {
this._execute().then(()=> {
this._doExecution();
});
} else {
/**
* let other messages start the process
* @type {boolean}
* @private
*/
this._executionStarted = false;
}
}
_execute() {
return new Promise((resolve, reject) => {
let message = this._callMessageQueue.shift();
this._callback(message);
setTimeout(() => {
resolve();
},this._executorTimeout)
})
}
pushCallMessageToQueue(message) {
this._callMessageQueue.push(message);
this._executeCallMessageQueue();
}
}
export default MessageExecutorQueue;