drop the SSE, use client side fetch every 5s for thread updates
This commit is contained in:
44
js/thread.js
44
js/thread.js
@ -37,8 +37,8 @@
|
||||
})
|
||||
}
|
||||
|
||||
let newPostSubscription = null;
|
||||
|
||||
const threadEndpoint = document.getElementById("thread-subscribe-endpoint").value;
|
||||
let now = Math.floor(new Date() / 1000);
|
||||
function hideNotification() {
|
||||
const notification = document.getElementById('new-post-notification');
|
||||
notification.classList.add('hidden');
|
||||
@ -50,35 +50,31 @@
|
||||
notification.classList.remove("hidden");
|
||||
|
||||
document.getElementById("dismiss-new-post-button").onclick = () => {
|
||||
now = Math.floor(new Date() / 1000);
|
||||
hideNotification();
|
||||
reconnectSSE();
|
||||
tryFetchUpdate();
|
||||
}
|
||||
|
||||
document.getElementById("go-to-new-post-button").href = url;
|
||||
|
||||
document.getElementById("unsub-new-post-button").onclick = () => {
|
||||
hideNotification()
|
||||
hideNotification();
|
||||
}
|
||||
}
|
||||
|
||||
function reconnectSSE() {
|
||||
if (newPostSubscription) newPostSubscription.close();
|
||||
|
||||
const threadEndpoint = document.getElementById("thread-subscribe-endpoint").value;
|
||||
newPostSubscription = new EventSource(threadEndpoint);
|
||||
newPostSubscription.onerror = (e) => {
|
||||
console.error(e);
|
||||
};
|
||||
newPostSubscription.addEventListener("new_post_url", (e) => {
|
||||
showNewPostNotification(e.data);
|
||||
newPostSubscription.close();
|
||||
})
|
||||
|
||||
function tryFetchUpdate() {
|
||||
if (!threadEndpoint) return;
|
||||
const body = JSON.stringify({since: now});
|
||||
fetch(threadEndpoint, {method: "POST", headers: {"Content-Type": "application/json"}, body: body})
|
||||
.then(res => res.json())
|
||||
.then(json => {
|
||||
if (json.status === "none") {
|
||||
setTimeout(tryFetchUpdate, 5000);
|
||||
} else if (json.status === "new_post") {
|
||||
showNewPostNotification(json.url);
|
||||
}
|
||||
})
|
||||
.catch(error => console.log(error))
|
||||
}
|
||||
window.addEventListener('beforeunload', () => {
|
||||
if(newPostSubscription)
|
||||
{
|
||||
newPostSubscription.close();
|
||||
}
|
||||
});
|
||||
reconnectSSE();
|
||||
tryFetchUpdate();
|
||||
}
|
||||
|
Reference in New Issue
Block a user