mirror of
https://github.com/Doodle3D/Doodle3D-API
synced 2024-12-22 22:03:48 +01:00
implemented queue
This commit is contained in:
parent
da7245218b
commit
7d9d3ad8ac
@ -6,16 +6,48 @@ const POST_TIMEOUT = 10000;
|
|||||||
// TODO
|
// TODO
|
||||||
// implement queue meganism
|
// implement queue meganism
|
||||||
const queue = [];
|
const queue = [];
|
||||||
|
let sending = false;
|
||||||
|
|
||||||
export function get (url) {
|
export function get (url) {
|
||||||
return await ajax(url, 'GET');
|
return new Promise((resolve, reject) => {
|
||||||
|
addQue({ url, type: 'GET', resolve, reject });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function post (url, data) {
|
export function post (url, data) {
|
||||||
return await ajax(url, 'POST', data);
|
return new Promise((resolve, reject) => {
|
||||||
|
addQue({ url, type: 'POST', data, resolve, reject });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function ajax(url, type, data) {
|
function addQue(ajaxData) {
|
||||||
|
if (sending) {
|
||||||
|
sendQueue(ajaxData);
|
||||||
|
} else {
|
||||||
|
queue.push(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