Use SSE for get_guilds
This commit is contained in:
parent
6b6c047b68
commit
a8c0969a71
2 changed files with 80 additions and 46 deletions
|
@ -24,11 +24,43 @@
|
|||
const dialog = document.querySelector("dialog");
|
||||
// Fetch guilds
|
||||
let guilds = null;
|
||||
let is_in_participating_server = null;
|
||||
const getGuilds = async () => {
|
||||
if (guilds === null) {
|
||||
const response = await fetch("/get_guilds");
|
||||
const raw_json = await response.text()
|
||||
guilds = JSON.parse(raw_json);
|
||||
// Create the guilds dictionary object
|
||||
guilds = {};
|
||||
|
||||
// Create an EventSource object to listen to SSE events
|
||||
const eventSource = new EventSource('/get_guilds');
|
||||
|
||||
// Event listener for 'message' events
|
||||
eventSource.addEventListener('message', event => {
|
||||
// Parse the data received from the event
|
||||
const [id, is_member] = event.data.split(',');
|
||||
|
||||
// Add the key-value pair to the guilds dictionary
|
||||
guilds[id] = is_member === 'true';
|
||||
|
||||
if (guilds[id]) {
|
||||
setJoined(id);
|
||||
is_in_participating_server = true;
|
||||
}
|
||||
});
|
||||
|
||||
// Function to block execution until SSE events are done
|
||||
function waitForSSE() {
|
||||
return new Promise(resolve => {
|
||||
// Event listener for 'error' event
|
||||
// For some reason errors when completing
|
||||
eventSource.addEventListener('error', () => {
|
||||
eventSource.close();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Call the function to block execution until SSE events are done
|
||||
await waitForSSE()
|
||||
}
|
||||
return guilds;
|
||||
}
|
||||
|
@ -40,18 +72,29 @@ const showServers = async () => {
|
|||
const guilds = await getGuilds();
|
||||
Object.keys(guilds).forEach(id => {
|
||||
if (guilds[id]) {
|
||||
const element = document.getElementById(id);
|
||||
if (element !== null) {
|
||||
const button = document.createElement("button");
|
||||
button.tagName = "button";
|
||||
button.className = "joinButton";
|
||||
button.disabled = true;
|
||||
button.innerHTML = "Joined"
|
||||
element.querySelector("a").replaceWith(button);
|
||||
}
|
||||
setJoined(id);
|
||||
}
|
||||
})
|
||||
};
|
||||
// Switch Join button to Joined for server
|
||||
const setJoined = id => {
|
||||
element = document.getElementById(id);
|
||||
if (element === null) {
|
||||
return;
|
||||
}
|
||||
// Check if already set as joined
|
||||
a = element.querySelector("a");
|
||||
if (a === null) {
|
||||
return;
|
||||
}
|
||||
const button = document.createElement("button");
|
||||
button.tagName = "button";
|
||||
button.className = "joinButton";
|
||||
button.disabled = true;
|
||||
button.innerHTML = "Joined"
|
||||
element.querySelector("a").replaceWith(button);
|
||||
}
|
||||
|
||||
// Check for welcomed cookie
|
||||
const welcomed = () => document.cookie.split("; ").includes("welcomed=true");
|
||||
// Load welcome modal, check if it is necessary
|
||||
|
@ -61,15 +104,7 @@ const loadServers = async () => {
|
|||
// in case user pressed Join servers button
|
||||
document.querySelector("dialog .welcome").style.display = "none";
|
||||
} else {
|
||||
let is_in_participating_server = false;
|
||||
const guilds = await getGuilds();
|
||||
const ids = Object.keys(guilds);
|
||||
for (let i = 0; i < ids.length; i++) {
|
||||
if (guilds[ids[i]]) {
|
||||
is_in_participating_server = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!is_in_participating_server) {
|
||||
showServers();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue