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 1016B

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