-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (46 loc) · 1.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { LenraApp, Listener } from '@lenra/client';
const app = new LenraApp({
appName: "XXX-XXX-XXX",
clientId: "XXX-XXX-XXX",
});
let authenticated = false;
console.log("connecting...");
app.openSocket().then(appConnected);
const authenticationButton = document.querySelector("body > button");
authenticationButton.onclick = () => {
app.disconnect();
authenticated = true;
app.connect().then(appConnected);
};
const disconnectButton = document.createElement("button");
disconnectButton.textContent = "Disconnect";
disconnectButton.onclick = () => {
app.disconnect();
disconnectButton.style.display = "none";
};
document.body.appendChild(disconnectButton);
function appConnected() {
console.log("Connected !");
disconnectButton.style.display = undefined;
const counters = document.querySelectorAll(".counter");
counters.forEach((counter) => {
// Filter counters that need authentication
if (!authenticated && counter.classList.contains("authenticated")) return;
const button = counter.querySelector("button");
const output = counter.querySelector("output");
console.log("Open route for counter", counter.id);
app.route(`/counter/${counter.id}`,
/**
* @param {{value: number, onIncrement: Listener<{value: string}>}} data
*/
(data) => {
output.textContent = data.value;
button.onclick = () => {
output.classList.add("loading");
data.onIncrement({ value: "custom value" }).then(() => {
output.classList.remove("loading");
});
};
});
});
}