added unlisten method

This commit is contained in:
srgooglo 2022-06-03 06:26:08 +02:00
parent d47f07fe8b
commit 4359a1b1b3

View File

@ -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)