lbry.tech/app/dist/scripts/sockets.js

101 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-09-27 18:11:26 +02:00
"use strict";
2018-07-16 23:06:37 +02:00
2018-09-27 18:11:26 +02:00
document.addEventListener("DOMContentLoaded", () => {
initializeWebSocketConnection();
setInterval(checkWebSocketConnection, 5000);
});
2018-07-12 23:07:16 +02:00
2018-09-27 18:11:26 +02:00
let ws = null;
2018-07-12 23:07:16 +02:00
2018-09-27 18:11:26 +02:00
function checkWebSocketConnection() {
if (!ws || ws.readyState === 3) initializeWebSocketConnection();
}
function initializeWebSocketConnection() {
ws = new WebSocket(location.origin.replace(/^http/, "ws"));
ws.onopen = () => {
console.log("WebSocket connection established"); // eslint-disable-line
};
2018-07-12 23:07:16 +02:00
2018-09-27 18:11:26 +02:00
ws.onmessage = socket => {
const data = JSON.parse(socket.data);
2018-07-17 21:40:25 +02:00
2018-09-27 18:11:26 +02:00
switch (true) {
case data.message === "updated html":
document.querySelector(data.selector).innerHTML = data.html;
document.getElementById("emailAddress").value = "";
document.getElementById("emailMessage").innerHTML = "";
2018-09-27 20:00:34 +02:00
// `data.example` is added when updating HTML.
// This is when the results of an example are sent to the client.
if (data.example) {
if (!document.querySelector(`[data-example="${data.example}"`).classList.contains("completed")) {
document.getElementById("tour-example-description").classList.remove("success");
}
document.querySelector(`[data-example="${data.example}"`).classList.add("completed");
document.getElementById("tour-example-description").classList.add("success");
document.getElementById("tour-example-description").innerHTML =
document.querySelector(`[data-example="${data.example}"`).dataset.success;
}
// If `data.example` isn't found, reset the description area.
else {
2018-09-27 22:55:51 +02:00
if (document.getElementById("tour-example-description")) {
document.getElementById("tour-example-description").classList.remove("success");
2018-09-27 20:00:34 +02:00
2018-09-27 22:55:51 +02:00
document.getElementById("tour-example-description").innerHTML =
document.querySelector(".tour__navigation__example.active").dataset.description;
}
2018-09-27 20:00:34 +02:00
}
if (document.getElementById("temp-loader"))
document.getElementById("temp-loader").style.display = "none";
2018-09-27 22:55:51 +02:00
if (document.querySelector(".tour"))
document.querySelector(".tour").classList.remove("waiting");
2018-09-27 18:11:26 +02:00
break;
2018-07-17 21:40:25 +02:00
2018-09-27 18:11:26 +02:00
case data.message === "notification": // TODO: Make work with appending so multiple notifications can be sent
document.getElementById("flash-container").innerHTML =
`<div class="flash active${data.type ? " " + data.type : ""}">${data.details}</div>`;
2018-07-17 21:40:25 +02:00
2018-09-27 18:11:26 +02:00
setTimeout(() => {
document.getElementById("flash-container").innerHTML = "";
}, 2100);
break;
default:
console.log(data); // eslint-disable-line
break;
}
};
ws.onclose = () => {
console.log("WebSocket connection lost"); // eslint-disable-line
checkWebSocketConnection(); // reconnect now
};
}
2018-07-16 23:06:37 +02:00
function send(msg) { // eslint-disable-line
socketReady(ws, () => ws.send(msg));
}
function socketReady(socket, callback) {
setTimeout(() => {
2018-09-27 18:11:26 +02:00
if (socket && socket.readyState === 1) {
2018-07-16 23:06:37 +02:00
if (callback !== undefined) callback();
return;
}
2018-09-27 18:11:26 +02:00
return socketReady(socket, callback);
2018-07-16 23:06:37 +02:00
}, 5);
}