浏览代码

fix(WS): when backend connection loss + regain, callbacks weren't being fired

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 3 年之前
父节点
当前提交
4dd174ecbd

+ 2 - 2
frontend/src/main.js

@@ -239,8 +239,8 @@ lofig.folder = "../config/default.json";
 		}
 
 		if (ws.socket) {
-			ws.clear();
-			ws.removeAllListeners();
+			ws.clearCallbacks();
+			ws.destroyListeners();
 		}
 
 		if (

+ 1 - 1
frontend/src/store/modules/modalVisibility.js

@@ -51,7 +51,7 @@ const mutations = {
 	},
 	closeCurrentModal(state) {
 		// remove any websocket listeners for the modal
-		ws.removeModalListeners(state.currentlyActive[0]);
+		ws.destroyModalListeners(state.currentlyActive[0]);
 
 		state.modals[state.currentlyActive[0]] = false;
 		state.currentlyActive.shift();

+ 2 - 2
frontend/src/store/modules/user.js

@@ -122,9 +122,9 @@ const modules = {
 								"users.getUsernameFromId",
 								userId,
 								res => {
-									const { username } = res.data;
-
 									if (res.status === "success") {
+										const { username } = res.data;
+
 										commit("mapUserId", {
 											userId,
 											username

+ 6 - 3
frontend/src/store/modules/websockets.js

@@ -24,10 +24,13 @@ const mutations = {
 			// for each listener type
 			Object.keys(listeners).forEach(listenerType =>
 				// for each callback previously present for the listener type
-				listeners[listenerType].forEach(cb =>
+				listeners[listenerType].forEach(element => {
 					// add the listener back after the websocket object is reset
-					state.socket.dispatcher.addEventListener(listenerType, cb)
-				)
+					state.socket.dispatcher.addEventListener(
+						listenerType,
+						element.cb
+					);
+				})
 			);
 		}
 	}

+ 7 - 9
frontend/src/ws.js

@@ -31,12 +31,12 @@ export default {
 		else onDisconnect.temp.push(args[0]);
 	},
 
-	clear: () => {
+	clearCallbacks: () => {
 		onConnect.temp = [];
 		onDisconnect.temp = [];
 	},
 
-	removeAllListeners: () =>
+	destroyListeners: () =>
 		Object.keys(CB_REFS).forEach(id => {
 			if (
 				id.indexOf("$event:") !== -1 &&
@@ -45,14 +45,12 @@ export default {
 				delete CB_REFS[id];
 		}),
 
-	removeModalListeners(modal) {
+	destroyModalListeners(modal) {
 		Object.keys(this.socket.dispatcher.listeners).forEach(type =>
-			this.socket.dispatcher.listeners[type].forEach(
-				(listener, index) => {
-					if (listener.options && listener.options.modal === modal)
-						this.socket.dispatcher.listeners[type].splice(index, 1);
-				}
-			)
+			this.socket.dispatcher.listeners[type].forEach((element, index) => {
+				if (element.options && element.options.modal === modal)
+					this.socket.dispatcher.listeners[type].splice(index, 1);
+			})
 		);
 	},