X-Git-Url: https://git.auder.net/assets/discord.svg?a=blobdiff_plain;f=client%2Fsrc%2Fstore.js;h=12c756cf29ff828ea7f9540b0fb617a1123a558a;hb=cf742aaf8995ca8be8fc1f2751e4cf28de5d69b6;hp=9d1c6e6285b6d9e25f0f48f1fca1e2db5d4d5796;hpb=98f485791e915563996de4f37430b285ffc773ae;p=vchess.git diff --git a/client/src/store.js b/client/src/store.js index 9d1c6e62..12c756cf 100644 --- a/client/src/store.js +++ b/client/src/store.js @@ -2,6 +2,7 @@ import { ajax } from "./utils/ajax"; import { getRandString } from "./utils/alea"; import params from "./parameters"; //for socket connection +// Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87 export const store = { state: { @@ -12,6 +13,7 @@ export const store = settings: {}, lang: "", }, + socketCloseListener: null, initialize() { ajax("/variants", "GET", res => { this.state.variants = res.variantArray; }); let mysid = localStorage["mysid"]; @@ -29,15 +31,22 @@ export const store = }; if (this.state.user.id > 0) { - fetch(params.serverUrl + "/whoami", { - method: "GET", - credentials: params.cors ? "include" : "omit", - }).then((res) => { + ajax("/whoami", "GET", res => { this.state.user.email = res.email; this.state.user.notify = res.notify; }); + // TODO: fetch is simpler, but does not set req.xhr (for security check) +// fetch(params.serverUrl + "/whoami", { +// method: "GET", +// credentials: params.cors ? "include" : "omit", +// }).then((res) => { +// return res.json() +// }).then((user) => { +// this.state.user.email = user.email; +// this.state.user.notify = user.notify; +// }); } - this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + this.state.user.sid); + this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + mysid); // Settings initialized with values from localStorage this.state.settings = { bcolor: localStorage["bcolor"] || "lichess", @@ -47,10 +56,11 @@ export const store = highlight: !!eval(localStorage["highlight"]), sqSize: parseInt(localStorage["sqSize"]), }; - const socketCloseListener = () => { + this.socketCloseListener = () => { + // Next line may fail at first, but should retry and eventually success (TODO?) this.state.conn = new WebSocket(params.socketUrl + "/?sid=" + mysid); - } - this.state.conn.onclose = socketCloseListener; + }; + this.state.conn.onclose = this.socketCloseListener; const supportedLangs = ["en","es","fr"]; this.state.lang = localStorage["lang"] || supportedLangs.includes(navigator.language)