-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
37 lines (31 loc) · 833 Bytes
/
app.js
File metadata and controls
37 lines (31 loc) · 833 Bytes
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
// Entry: auto-create or auto-join, SW registration
import { createRoom, joinRoom, leaveRoom } from './peer.js';
import { initUI } from './ui.js';
import state from './state.js';
// Register service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js').catch(() => {});
}
function getRoomFromHash() {
const match = location.hash.match(/^#room=([A-Z0-9]{4})$/i);
return match ? match[1].toUpperCase() : null;
}
function handleHashChange() {
const newRoom = getRoomFromHash();
if (newRoom && newRoom !== state.roomCode) {
leaveRoom();
joinRoom(newRoom);
}
}
// Boot
async function boot() {
await initUI();
const room = getRoomFromHash();
if (room) {
joinRoom(room);
} else {
createRoom();
}
}
window.addEventListener('hashchange', handleHashChange);
boot();