From 4359a1b1b379710df3e5f29476b8c66146fa887a Mon Sep 17 00:00:00 2001 From: srgooglo Date: Fri, 3 Jun 2022 06:26:08 +0200 Subject: [PATCH] added `unlisten` method --- packages/app/src/cores/api/index.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/app/src/cores/api/index.js b/packages/app/src/cores/api/index.js index 126b0b83..3b10175c 100644 --- a/packages/app/src/cores/api/index.js +++ b/packages/app/src/cores/api/index.js @@ -12,7 +12,8 @@ export default class ApiCore extends Core { this.WSInterface = { ...this.apiBridge.wsInterface, request: this.WSRequest, - listen: this.handleWSListener, + listen: this.listenEvent, + unlisten: this.unlistenEvent, mainSocketConnected: false } @@ -23,7 +24,6 @@ export default class ApiCore extends Core { } async intialize() { - console.log(this.apiBridge) this.WSInterface.sockets.main.on("authenticated", () => { console.debug("[WS] Authenticated") }) @@ -127,7 +127,7 @@ export default class ApiCore extends Core { await this.apiBridge.initialize() } - handleWSListener = (to, fn) => { + listenEvent = (to, fn) => { if (typeof to === "undefined") { console.error("handleWSListener: to must be defined") return false @@ -152,6 +152,29 @@ export default class ApiCore extends Core { }) } + unlistenEvent = (to, fn) => { + if (typeof to === "undefined") { + console.error("handleWSListener: to must be defined") + return false + } + if (typeof fn !== "function") { + console.error("handleWSListener: fn must be function") + return false + } + + let ns = "main" + let event = null + + if (typeof to === "string") { + event = to + } else if (typeof to === "object") { + ns = to.ns + event = to.event + } + + return window.app.ws.sockets[ns].removeListener(event, fn) + } + WSRequest = (socket = "main", channel, ...args) => { return new Promise(async (resolve, reject) => { const request = await window.app.ws.sockets[socket].emit(channel, ...args)