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

120 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-01-17 21:41:26 +01:00
"use strict"; /* global document, location, WebSocket */
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() {
2019-02-05 00:42:52 +01:00
if (!ws || ws.readyState === 3)
initializeWebSocketConnection();
2018-09-27 18:11:26 +02:00
}
function initializeWebSocketConnection() {
ws = new WebSocket(location.origin.replace(/^http/, "ws"));
2019-02-05 00:42:52 +01:00
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-28 21:09:45 +02:00
switch(true) {
case data.message === "show result":
2019-02-05 00:42:52 +01:00
if (!data.example)
return;
2018-09-28 21:09:45 +02:00
document.querySelector(data.selector).innerHTML = data.html;
if (!document.querySelector(`[data-example="${data.example}"`).classList.contains("completed"))
2018-10-03 22:27:13 +02:00
document.getElementById("playground-example-description").classList.remove("success");
2018-09-28 21:09:45 +02:00
document.querySelector(`[data-example="${data.example}"`).classList.add("completed");
2018-10-03 22:27:13 +02:00
document.getElementById("playground-example-description").classList.add("success");
2018-09-28 21:09:45 +02:00
2018-10-03 22:27:13 +02:00
document.getElementById("playground-example-description").innerHTML =
2018-09-28 21:09:45 +02:00
document.querySelector(`[data-example="${data.example}"`).dataset.success;
if (document.getElementById("temp-loader"))
document.getElementById("temp-loader").style.display = "none";
2018-10-03 22:27:13 +02:00
if (document.querySelector(".playground"))
document.querySelector(".playground").classList.remove("waiting");
2018-09-28 21:09:45 +02:00
break;
2018-09-27 18:11:26 +02:00
case data.message === "updated html":
document.querySelector(data.selector).innerHTML = data.html;
2018-10-10 21:04:16 +02:00
if (data.class)
document.querySelector(data.selector).classList.add(data.class);
if (data.selector !== "#emailMessage") {
document.getElementById("emailAddress").value = "";
document.getElementById("emailMessage").innerHTML = "";
}
2018-09-27 20:00:34 +02:00
2018-09-28 21:09:45 +02:00
if (data.example === 2) {
2019-02-05 00:42:52 +01:00
detectLanguageAndUpdate(); // eslint-disable-line no-undef
initCanvas(); // eslint-disable-line no-undef
2018-09-27 20:00:34 +02:00
2018-09-28 21:09:45 +02:00
setTimeout(() => {
2018-12-05 00:12:39 +01:00
document.querySelector(".playground-content__meme__canvas__thumbnail").click();
2018-10-03 21:14:38 +02:00
}, 500);
2018-09-27 20:00:34 +02:00
}
2018-10-03 22:27:13 +02:00
if (document.getElementById("playground-example-description")) {
document.getElementById("playground-example-description").classList.remove("success");
2018-09-27 20:00:34 +02:00
2018-10-03 22:27:13 +02:00
document.getElementById("playground-example-description").innerHTML =
2018-12-05 00:12:39 +01:00
document.querySelector(".playground-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-10-03 22:27:13 +02:00
if (document.querySelector(".playground"))
document.querySelector(".playground").classList.remove("waiting");
2018-09-28 21:09:45 +02:00
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:
2019-02-05 00:42:52 +01:00
console.log(data); // eslint-disable-line no-console
2018-09-27 18:11:26 +02:00
break;
}
};
2019-02-05 00:42:52 +01:00
ws.onclose = () => checkWebSocketConnection(); // reconnect now
2018-09-27 18:11:26 +02:00
}
2018-07-16 23:06:37 +02:00
2019-02-05 00:42:52 +01:00
function send(msg) { // eslint-disable-line no-unused-vars
socketReady(ws, () => ws.send(JSON.stringify(msg)));
2018-07-16 23:06:37 +02:00
}
function socketReady(socket, callback) {
setTimeout(() => {
2018-09-27 18:11:26 +02:00
if (socket && socket.readyState === 1) {
2019-02-05 00:42:52 +01:00
if (callback !== undefined)
callback();
2018-07-16 23:06:37 +02:00
return;
}
2018-09-27 18:11:26 +02:00
return socketReady(socket, callback);
2018-07-16 23:06:37 +02:00
}, 5);
}