mirror of
https://github.com/Doodle3D/Doodle3D-API
synced 2024-12-22 22:03:48 +01:00
remove queue from rest
This commit is contained in:
parent
e864d78c9c
commit
9f1f2bb5d8
@ -3,49 +3,15 @@ import $ from 'jquery';
|
|||||||
const GET_TIMEOUT = 5000;
|
const GET_TIMEOUT = 5000;
|
||||||
const POST_TIMEOUT = 10000;
|
const POST_TIMEOUT = 10000;
|
||||||
|
|
||||||
const queue = [];
|
|
||||||
let sending = false;
|
|
||||||
|
|
||||||
export function get (url) {
|
export function get (url) {
|
||||||
return new Promise((resolve, reject) => {
|
return send(url, 'GET');
|
||||||
addQueue({ url, type: 'GET', resolve, reject });
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function post (url, data) {
|
export function post (url, data) {
|
||||||
return new Promise((resolve, reject) => {
|
return send(url, 'POST', data);
|
||||||
addQueue({ url, type: 'POST', data, resolve, reject });
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function addQueue(ajaxData) {
|
function send(url, type, data) {
|
||||||
if (sending) {
|
|
||||||
queue.push(ajaxData);
|
|
||||||
} else {
|
|
||||||
sendQueue(ajaxData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function sendQueue() {
|
|
||||||
sending = true;
|
|
||||||
|
|
||||||
const ajaxData = queue.unshift();
|
|
||||||
try {
|
|
||||||
const response = await send(ajaxData);
|
|
||||||
} catch (e) {
|
|
||||||
throw ajaxData.reject(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
ajaxData.resolve(response);
|
|
||||||
|
|
||||||
if (queue.length > 0) {
|
|
||||||
sendQueue();
|
|
||||||
} else {
|
|
||||||
sending = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function send({ url, type, data }) {
|
|
||||||
const timeout = (type === 'GET') ? GET_TIMEOUT : POST_TIMEOUT;
|
const timeout = (type === 'GET') ? GET_TIMEOUT : POST_TIMEOUT;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user