Files
cairnquire/apps/server/internal/httpserver/static/realtime.js

36 lines
1.1 KiB
JavaScript

(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();
});
})();