Update http.js.

This commit is contained in:
voussoir 2022-10-12 18:40:39 -07:00
parent 9ce9417ce8
commit dcaff7fb11
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
2 changed files with 32 additions and 14 deletions

View file

@ -8,27 +8,30 @@ http.request_queue = {};
http.request_queue.array = [];
http.request_queue.push =
function request_queue_push(func)
function request_queue_push(func, kwargs)
{
const delay = ((! kwargs) ? 0 : kwargs["delay"]) || 0;
http.request_queue.array.push(func)
if (http.requests_in_flight == 0)
{
http.request_queue_next();
}
setTimeout(http.request_queue.next, 0);
}
http.request_queue.unshift =
function request_queue_unshift(func)
http.request_queue.pushleft =
function request_queue_pushleft(func, kwargs)
{
http.request_queue.array.unshift(func)
if (http.requests_in_flight == 0)
{
http.request_queue_next();
setTimeout(http.request_queue.next, 0);
}
}
http.request_queue.pushleft = http.request_queue.unshift;
http.request_queue_next =
http.request_queue.clear =
function request_queue_clear()
{
while (http.request_queue.array.length > 0)
{
http.request_queue.array.shift();
}
}
http.request_queue.next =
function request_queue_next()
{
if (http.requests_in_flight > 0)
@ -68,9 +71,23 @@ function _request(kwargs)
/*
Perform an HTTP request and call the `callback` with the response.
Required kwargs:
url
Optional kwargs:
with_credentials: goes to xhr.withCredentials
callback
asynchronous: goes to the async parameter of xhr.open
headers: an object fully of {key: value} that will get added as headers in
addition to those in the global http.HEADERS.
data: the body of your post request. Can be a FormData object, a string,
or an object of {key: value} that will get automatically turned into
a FormData.
The response will have the following structure:
{
"meta": {
"id": a large random number to uniquely identify this request.
"request": the XMLHttpRequest object,
"completed": true / false,
"status": If the connection failed or request otherwise could not
@ -95,6 +112,7 @@ function _request(kwargs)
const request = new XMLHttpRequest();
const response = {
"meta": {
"id": Math.random() * Number.MAX_SAFE_INTEGER,
"request": request,
"completed": false,
"status": 0,
@ -122,7 +140,7 @@ function _request(kwargs)
http.requests_in_flight -= 1;
setTimeout(http.request_queue_next, 0);
if (kwargs["callback"] == null)
if (! (kwargs["callback"]))
{
return;
}

View file

@ -400,11 +400,11 @@ function add_remove_photo_tag_callback(response)
{
const tagname = response.data.tagname;
message_positivity = "message_positive";
if (response.meta.request_url.includes("add_tag"))
if (response.meta.kwargs.url.includes("add_tag"))
{
message_text = "Added tag " + tagname;
}
else if (response.meta.request_url.includes("remove_tag"))
else if (response.meta.kwargs.url.includes("remove_tag"))
{
message_text = "Removed tag " + tagname;
}