Shopping List v2.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.js 928B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import App from './App.svelte';
  2. import WSockMan from './ws.js';
  3. let wsock = new WSockMan(
  4. `${location.protocol == "http:" ? "ws:" : "wss:"}//${location.host}/`);
  5. let app = new App({ target: document.body, props: { wsock }});
  6. let key = localStorage.getItem("key");
  7. if (!key)
  8. key = prompt("Key?");
  9. function auth(k) {
  10. wsock.send({ type: "init", key: k })
  11. .then(res => {
  12. localStorage.setItem("key", key);
  13. key = k;
  14. app.onInitialData(res.data);
  15. })
  16. .catch(err => {
  17. console.trace(err);
  18. let k = prompt(err);
  19. if (k != null)
  20. auth(k);
  21. });
  22. };
  23. wsock.onconnect = () => auth(key);
  24. wsock.ondisconnect = app.onDisconnect.bind(app);
  25. wsock.onmessage = msg => {
  26. switch (msg.type) {
  27. case "item-del":
  28. app.onRemoveFromServer(msg.index);
  29. break;
  30. case "item-add":
  31. app.onAddFromServer(msg.index, msg.content);
  32. break;
  33. default:
  34. console.warn("Unknown message type", msg.type);
  35. }
  36. };
  37. export default app;