From 54153e723d88d743c70fbdb55a5aba9600374bbe Mon Sep 17 00:00:00 2001 From: elijahr2411 Date: Thu, 26 Jan 2023 16:27:19 -0500 Subject: [PATCH] switch to using nanoevents from the weird onmessage shit I had --- package.json | 1 + src/index.js | 70 +++++++++++++++++++++++++++------------------------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 26545db..d69059a 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "author": "Elijah R", "license": "GPL-3.0", "dependencies": { + "nanoevents": "^7.0.1", "webpack": "^5.75.0", "webpack-cli": "^5.0.1" } diff --git a/src/index.js b/src/index.js index 84a5fa9..a3c7190 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ import { guacutils } from "./protocol"; import { config } from "./common"; import { GetKeysym } from "./keyboard"; +import { createNanoEvents } from "nanoevents"; // None = -1 // Has turn = 0 // In queue = @@ -41,6 +42,7 @@ const votetime = document.getElementById("votetime"); const chatListDiv = document.querySelector(".chat-table"); class CollabVMClient { + eventemitter = createNanoEvents(); socket; #url; constructor(url) { @@ -67,21 +69,14 @@ class CollabVMClient { if (savedUsername === null) this.socket.send(guacutils.encode(["rename"])); else this.socket.send(guacutils.encode(["rename", savedUsername])); - var f = (e) => { - var msgArr = guacutils.decode(e.data); - if (msgArr[0] == "connect") { - switch (msgArr[1]) { - case "0": - rej("Failed to connect to the node"); - break; - case "1": - res(); - break; - } - this.socket.removeEventListener("message", f); - } - } - this.socket.addEventListener("message", f); + var unbind = this.eventemitter.on('connect', () => { + unbind(); + res(); + }); + var failunbind = this.eventemitter.on('connectfail', () => { + failunbind(); + rej(); + }); this.socket.send(guacutils.encode(["connect", node])); }); } @@ -91,6 +86,16 @@ class CollabVMClient { case "nop": this.socket.send("3.nop;"); break; + case "connect": + switch (msgArr[1]) { + case "0": + this.eventemitter.emit('connectfail'); + break; + case "1": + this.eventemitter.emit('connect'); + break; + } + break; case "chat": if (!connected) return; for (var i = 1; i < msgArr.length; i += 2) { @@ -99,6 +104,19 @@ class CollabVMClient { chatsound.play(); chatListDiv.scrollTop = chatListDiv.scrollHeight; break; + case "list": + var list = []; + for (var i = 1; i < msgArr.length; i+=3) { + list.push({ + url: this.#url, + id: msgArr[i], + name: msgArr[i+1], + thumb: msgArr[i+2], + + }); + } + this.eventemitter.emit('list', list); + break; case "size": if (!connected || msgArr[1] !== "0") return; display.width = msgArr[2]; @@ -252,24 +270,10 @@ class CollabVMClient { } async list() { return new Promise((res, rej) => { - var h = (e) => { - var msgArr = guacutils.decode(e.data); - if (msgArr[0] === "list") { - var list = []; - for (var i = 1; i < msgArr.length; i+=3) { - list.push({ - url: this.#url, - id: msgArr[i], - name: msgArr[i+1], - thumb: msgArr[i+2], - - }); - } - this.socket.removeEventListener("message", h); - res(list); - } - }; - this.socket.addEventListener("message", h); + var unbind = this.eventemitter.on('list', (e) => { + unbind(); + res(e); + }) this.socket.send("4.list;"); }); }