feat: notify document updates over websocket
This commit is contained in:
35
apps/server/internal/httpserver/static/realtime.js
Normal file
35
apps/server/internal/httpserver/static/realtime.js
Normal file
@@ -0,0 +1,35 @@
|
||||
(function () {
|
||||
const notice = document.querySelector("[data-version-notice]");
|
||||
const reload = document.querySelector("[data-version-reload]");
|
||||
const documentShell = document.querySelector("[data-document-path][data-document-hash]");
|
||||
|
||||
if (!notice || !reload || !documentShell || !window.WebSocket) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentPath = documentShell.getAttribute("data-document-path");
|
||||
const currentHash = documentShell.getAttribute("data-document-hash");
|
||||
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
|
||||
const socket = new WebSocket(protocol + "//" + window.location.host + "/ws");
|
||||
|
||||
socket.addEventListener("message", function (event) {
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.parse(event.data);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.type !== "document_version" || !payload.data) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.data.path === currentPath && payload.data.hash !== currentHash) {
|
||||
notice.hidden = false;
|
||||
}
|
||||
});
|
||||
|
||||
reload.addEventListener("click", function () {
|
||||
window.location.reload();
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user