function Generator() {} Generator.prototype.rand = Math.floor(Math.random() * 26) + Date.now(); Generator.prototype.getId = function () { return this.rand++; }; function Queue() { this.elements = []; this.forceStop = false; this.state = "INITIALIZED"; this.idGen = new Generator(); } // add a request to the queue Queue.prototype.enqueue = function (request, data) { var id = this.idGen.getId(); var queue_data = { id: id, data: data, state: "INQUEUE", // PROCESSING, SUCCEED, FAILED request: request, response: "", }; this.elements.push(queue_data); return id; }; // Queue.prototype.dequeue = function () { // return this.elements.shift(); // }; //clear the queue Queue.prototype.clear = function () { this.elements = []; this.state = "INITIALIZED"; }; // check if queue items is emtry Queue.prototype.isEmpty = function () { return this.elements.length == 0; }; // get the queue item that failed Queue.prototype.getById = function (id) { if (!this.isEmpty()) { var res = $.grep(this.elements, function (data) { return data.id == id; }); if (res.length > 0) return res[0]; } return undefined; }; // get the current queue item that is processing Queue.prototype.getCurrentProcessing = function (id) { if (!this.isEmpty()) { var res = $.grep(this.elements, function (data) { return data.state == "PROCESSING"; }); if (res.length > 0) return res[res.length - 1]; } return undefined; }; // get the queue item that failed Queue.prototype.getFail = function (id) { if (!this.isEmpty()) { var res = $.grep(this.elements, function (data) { return data.state == "FAILED"; }); if (res.length > 0) return res[0]; } return undefined; }; // get the total number of queue items Queue.prototype.length = function () { return this.elements.length; }; //terminate queue Queue.prototype.stop = function () { this.state = "STOPPED"; this.forceStop = true; }; //start the queue Queue.prototype.start = async function (callback, fromFail = false) { let $this = this; $this.forceStop = false; $this.state = "PROCESSING"; $this.elements = $.map($this.elements, function (v, i) { if (fromFail) { if (v.state != "SUCCEED") { v.state = "INQUEUE"; } } else { v.state = "INQUEUE"; } return v; }); var terminateQueue = false; var success = false; debugger; for (let i = 0; i < $this.length(); i++) { if (terminateQueue) break; if ($this.forceStop) { $this.elements[i].state = "TERMINATED"; return; } if ($this.elements[i].state == "INQUEUE") { $this.elements[i].state = "PROCESSING"; let promise = new Promise(function (onSuccess, onFailed) { getFromAPI( $this.elements[i].data, function (data) { // set queue element response $this.elements[i].response = data; if (data.status == false) { success = false; $this.elements[i].state = "FAILED"; terminateQueue = true; } else if (JSON.stringify(data).indexOf("FAIL") !== -1) { success = false; $this.elements[i].state = "FAILED"; terminateQueue = true; } else { success = true; $this.elements[i].state = "SUCCEED"; } onSuccess(); }, function () { success = false; $this.elements[i].response = "HTTP Request failed"; $this.elements[i].state = "FAILED"; terminateQueue = true; onFailed(); } ); }); await promise; } } $this.state = success ? "COMPLETED" : "FAILED"; if (success) callback(true); else callback(false); };