summaryrefslogtreecommitdiff
path: root/packages/web-util/src/index.browser.ts
blob: 514a2ec4219dcfa2ba22e78961317bd169cb64a3 (plain)
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

//`ws://localhost:8003/socket`
export function setupLiveReload(wsURL: string | undefined) {
  if (!wsURL) return;
  const ws = new WebSocket(wsURL);
  ws.addEventListener('message', (message) => {
    const event = JSON.parse(message.data);
    if (event.type === "LOG") {
      console.log(event.message);
    }
    if (event.type === "RELOAD") {
      window.location.reload();
    }
    if (event.type === "UPDATE") {
      const c = document.getElementById("container")
      if (c) {
        document.body.removeChild(c);
      }
      const d = document.createElement("div");
      d.setAttribute("id", "container");
      d.setAttribute("class", "app-container");
      document.body.appendChild(d);
      const s = document.createElement("script");
      s.setAttribute("id", "code");
      s.setAttribute("type", "application/javascript");
      s.textContent = atob(event.content);
      document.body.appendChild(s);
    }
  });
  ws.onerror = (error) => {
    console.error(error);
  };
  ws.onclose = (e) => {
    setTimeout(setupLiveReload, 500);
  };
}

export { renderStories, parseGroupImport } from "./stories.js"