X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Fviews%2FMyGames.vue;h=1e1c9e7cb420ea5d5c6fc4ada911b77e5e3f74b1;hb=7ebc0408a76b4a966273190a2ade49e0f97099be;hp=60783941ff005005a2ec1dff3decb7cee385a173;hpb=934f7f70431e9892b3ea48ba199356b4f24eaf1b;p=vchess.git diff --git a/client/src/views/MyGames.vue b/client/src/views/MyGames.vue index 60783941..1e1c9e7c 100644 --- a/client/src/views/MyGames.vue +++ b/client/src/views/MyGames.vue @@ -53,12 +53,18 @@ export default { corr: Number.MAX_SAFE_INTEGER }, // hasMore == TRUE: a priori there could be more games to load - hasMore: { live: true, corr: true }, + hasMore: { live: true, corr: store.state.user.id > 0 }, conn: null, connexionString: "" }; }, + watch: { + $route: function(to, from) { + if (to.path != "/mygames") this.cleanBeforeDestroy(); + } + }, created: function() { + window.addEventListener("beforeunload", this.cleanBeforeDestroy); // Initialize connection this.connexionString = params.socketUrl + @@ -123,18 +129,20 @@ export default { } } ); - } else { - this.loadMore( - "live", - () => this.loadMore("corr", adjustAndSetDisplay) - ); - } + } else this.loadMore("live", adjustAndSetDisplay); }); }, beforeDestroy: function() { - this.conn.send(JSON.stringify({code: "disconnect"})); + this.cleanBeforeDestroy(); }, methods: { + cleanBeforeDestroy: function() { + window.removeEventListener("beforeunload", this.cleanBeforeDestroy); + this.conn.removeEventListener("message", this.socketMessageListener); + this.conn.removeEventListener("close", this.socketCloseListener); + this.conn.send(JSON.stringify({code: "disconnect"})); + this.conn = null; + }, setDisplay: function(type, e) { this.display = type; localStorage.setItem("type-myGames", type); @@ -172,6 +180,7 @@ export default { }); }, socketMessageListener: function(msg) { + if (!this.conn) return; const data = JSON.parse(msg.data); let gamesArrays = { "corr": this.corrGames, @@ -252,16 +261,18 @@ export default { game.players[0].sid == this.st.user.sid ? game.players[1].sid : game.players[0].sid; - this.conn.send( - JSON.stringify( - { - code: "mabort", - gid: game.id, - // NOTE: target might not be online - target: oppsid - } - ) - ); + if (!!this.conn) { + this.conn.send( + JSON.stringify( + { + code: "mabort", + gid: game.id, + // NOTE: target might not be online + target: oppsid + } + ) + ); + } } else if (!game.deletedByWhite || !game.deletedByBlack) { // Set score if game isn't deleted on server: @@ -281,7 +292,7 @@ export default { } }, loadMore: function(type, cb) { - if (type == "corr") { + if (type == "corr" && this.st.user.id > 0) { ajax( "/completedgames", "GET",