Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Mar 26, 2024
2 parents 35da59e + 2a2b371 commit 2f1f670
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 32 deletions.
16 changes: 10 additions & 6 deletions web/api/limits.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ Limits.fetch = function (subj, headers) {
})
.then(resp => handleCookieExpiration(resp, headers, (h) => Limits.fetch(subj, h)))
.then(resp => {
if (!resp.ok) {
if (subj === "1" || subj === "2") {
handleResponseStatus(resp.status);
} else {
alert(`Invalid subject: ${subj}`);
if (resp) {
if (!resp.ok) {
if (subj === "1" || subj === "2") {
handleResponseStatus(resp.status);
} else {
alert(`Invalid subject: ${subj}`);
}
return null;
}
return resp.json();
} else {
return null;
}
return resp.json();
})

}
10 changes: 7 additions & 3 deletions web/api/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ Subscriptions.fetchListPage = function (cursor, order, limit, filter, headers) {
})
.then(resp => handleCookieExpiration(resp, headers, (h) => Subscriptions.fetchListPage(cursor, order, limit, filter, h)))
.then(resp => {
if (!resp.ok) {
handleResponseStatus(resp.code);
if (resp) {
if (!resp.ok) {
handleResponseStatus(resp.code);
return null;
}
return resp.json();
} else {
return null;
}
return resp.json();
})
}

Expand Down
16 changes: 10 additions & 6 deletions web/api/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ Usage.fetch = function (subj, headers) {
})
.then(resp => handleCookieExpiration(resp, headers, (h) => Usage.fetch(subj, h)))
.then(resp => {
if (!resp.ok) {
if (subj === "1" || subj === "2") {
handleResponseStatus(resp.status);
} else {
alert(`Invalid usage subject: ${subj}`);
if (resp) {
if (!resp.ok) {
if (subj === "1" || subj === "2") {
handleResponseStatus(resp.status);
} else {
alert(`Invalid usage subject: ${subj}`);
}
return null;
}
return resp.json();
} else {
return null;
}
return resp.json();
});
}
2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
<div class="flex">
<div class="grow w-full"></div>
<svg xmlns="http://www.w3.org/2000/svg"
onclick="confirm('Stop search and results streaming?') ? queryStop() : console.log('query stop cancelled by user')"
onclick="confirm('Stop search and results streaming?') ? queryStop(true) : console.log('query stop cancelled by user')"
class="h-6 w-6 place-self-end sm:place-self-center text-gray-800 dark:text-gray-200"
fill="none"
viewBox="0 0 24 24"
Expand Down
37 changes: 21 additions & 16 deletions web/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ document
if (q != null && q !== "") {
const expires = Date.now() + resultsStreamingTimeout;
createQuerySubscription(q, expires, false)
.then(subId => {
if (subId && subId !== "") {
startEventsLoading(subId, expires);
.then(resp => {
if (!resp.ok) {
alert(`Subscription create request failed, response status: ${resp.status}`);
return null;
}
return resp.json();
})
.then(data => {
if (data && data.id !== "") {
startEventsLoading(data.id, expires);
}
});
}
Expand All @@ -33,18 +40,13 @@ function createQuerySubscription(q, expires, retried) {
};
return Subscriptions
.createResponse(searchSubNamePrefix + q, true, new Date(expires), cond, headers)
.then(resp => handleLimitReached(resp, q, expires, headers))
.then(resp => {
if (!resp.ok) {
alert(`Subscription create request failed, response status: ${resp.status}`);
return null;
}
return resp.json();
})
.then(data => data ? data.id : null);
.then(resp => handleLimitReached(resp, q, expires, headers, retried));
}

function handleLimitReached(resp, q, expires, headers) {
function handleLimitReached(resp, q, expires, headers, retried) {
if (retried) {
return resp;
}
if (!resp.ok && resp.status === 429) {
console.log("Subscriptions limit reached");
return Subscriptions
Expand Down Expand Up @@ -75,7 +77,7 @@ function handleLimitReached(resp, q, expires, headers) {

let activeSubId = null;

async function queryStop() {
async function queryStop(askClear) {
const headers = getAuthHeaders();
const data = await Subscriptions
.fetch(activeSubId, headers)
Expand All @@ -94,7 +96,7 @@ async function queryStop() {
let elemEventsMenu = document.getElementById("events-menu");
if (elemEvents.innerHTML === "") {
elemEventsMenu.style.display = "none";
} else if (elemEventsMenu.style.display !== "none" && confirm("Results streaming ended. Clear the results?")) {
} else if (elemEventsMenu.style.display !== "none" && (!askClear || confirm("Results streaming ended. Clear the results?"))) {
document.getElementById("events-menu").style.display = "none";
elemEvents.innerHTML = "";
}
Expand All @@ -103,6 +105,9 @@ async function queryStop() {
let audio = {};

async function startEventsLoading(subId, deadline) {
if (activeSubId != null) {
await queryStop(false);
}
document.getElementById("events-menu").style.display = "flex";
activeSubId = subId;
audio.ctx = new (window.AudioContext || window.webkitAudioContext)();
Expand Down Expand Up @@ -130,7 +135,7 @@ async function startEventsLoading(subId, deadline) {
alert(`Unexpected events loading error ${subId}: ${e}`);
} finally {
document.getElementById("streaming-results").innerText = "Results streaming ended.";
await queryStop();
await queryStop(true);
}
}

Expand Down

0 comments on commit 2f1f670

Please sign in to comment.