Browse Source

fix(ws): put ListenerHandler class in its own file to fix eslint issue

Kristian Vos 3 years ago
parent
commit
4494376222
2 changed files with 52 additions and 52 deletions
  1. 51 0
      frontend/src/classes/ListenerHandler.class.js
  2. 1 52
      frontend/src/ws.js

+ 51 - 0
frontend/src/classes/ListenerHandler.class.js

@@ -0,0 +1,51 @@
+export default class ListenerHandler extends EventTarget {
+	constructor() {
+		super();
+		this.listeners = {};
+	}
+
+	addEventListener(type, cb, options) {
+		// add the listener type to listeners object
+		if (!(type in this.listeners)) this.listeners[type] = [];
+
+		const stack = this.listeners[type];
+
+		// push the callback
+		stack.push({ cb, options });
+
+		const replaceableIndexes = [];
+
+		// check for any replaceable callbacks
+		stack.forEach((element, index) => {
+			if (element.options && element.options.replaceable)
+				replaceableIndexes.push(index);
+		});
+
+		// should always be 1 replaceable callback remaining
+		replaceableIndexes.pop();
+
+		// delete the other replaceable callbacks
+		replaceableIndexes.forEach(index => stack.splice(index, 1));
+	}
+
+	// eslint-disable-next-line consistent-return
+	removeEventListener(type, cb) {
+		if (!(type in this.listeners)) return true; // event type doesn't exist
+
+		const stack = this.listeners[type];
+
+		stack.forEach((element, index) => {
+			if (element.cb === cb) stack.splice(index, 1);
+		});
+	}
+
+	dispatchEvent(event) {
+		if (!(event.type in this.listeners)) return true; // event type doesn't exist
+
+		const stack = this.listeners[event.type].slice();
+
+		stack.forEach(element => element.cb.call(this, event));
+
+		return !event.defaultPrevented;
+	}
+}

+ 1 - 52
frontend/src/ws.js

@@ -1,5 +1,6 @@
 // eslint-disable-next-line import/no-cycle
 import store from "./store";
+import ListenerHandler from "./classes/ListenerHandler.class";
 
 const onConnect = {
 	temp: [],
@@ -67,58 +68,6 @@ export default {
 		const waitForConnectionToDispatch = (...args) =>
 			this.socket.dispatch(...args);
 
-		class ListenerHandler extends EventTarget {
-			constructor() {
-				super();
-				this.listeners = {};
-			}
-
-			addEventListener(type, cb, options) {
-				// add the listener type to listeners object
-				if (!(type in this.listeners)) this.listeners[type] = [];
-
-				const stack = this.listeners[type];
-
-				// push the callback
-				stack.push({ cb, options });
-
-				const replaceableIndexes = [];
-
-				// check for any replaceable callbacks
-				stack.forEach((element, index) => {
-					if (element.options && element.options.replaceable)
-						replaceableIndexes.push(index);
-				});
-
-				// should always be 1 replaceable callback remaining
-				replaceableIndexes.pop();
-
-				// delete the other replaceable callbacks
-				replaceableIndexes.forEach(index => stack.splice(index, 1));
-			}
-
-			// eslint-disable-next-line consistent-return
-			removeEventListener(type, cb) {
-				if (!(type in this.listeners)) return true; // event type doesn't exist
-
-				const stack = this.listeners[type];
-
-				stack.forEach((element, index) => {
-					if (element.cb === cb) stack.splice(index, 1);
-				});
-			}
-
-			dispatchEvent(event) {
-				if (!(event.type in this.listeners)) return true; // event type doesn't exist
-
-				const stack = this.listeners[event.type].slice();
-
-				stack.forEach(element => element.cb.call(this, event));
-
-				return !event.defaultPrevented;
-			}
-		}
-
 		class CustomWebSocket extends WebSocket {
 			constructor() {
 				super(url);