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

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 = null;
  7. function auth(key) {
  8. if (!key)
  9. key = prompt("Key?");
  10. wsock.send({ type: "init", key })
  11. .then(res => {
  12. localStorage.setItem("key", key);
  13. app.onInitialData(res.data);
  14. })
  15. .catch(err => {
  16. console.trace(err);
  17. let k = prompt(err);
  18. if (k != null)
  19. auth(k);
  20. });
  21. };
  22. wsock.onconnect = () => auth(localStorage.getItem("key"));
  23. wsock.ondisconnect = app.onDisconnect.bind(app);
  24. wsock.onmessage = msg => {
  25. switch (msg.type) {
  26. case "item-del":
  27. app.onRemoveFromServer(msg.index);
  28. break;
  29. case "item-add":
  30. app.onAddFromServer(msg.index, msg.content);
  31. break;
  32. default:
  33. console.warn("Unknown message type", msg.type);
  34. }
  35. };
  36. export default app;