Refactor everything to use EventEmitter

This commit is contained in:
Jonathan Staab
2023-07-07 17:37:35 -07:00
parent 46470aabe4
commit 4d6ea603ff
11 changed files with 157 additions and 189 deletions
+11 -14
View File
@@ -1,25 +1,22 @@
import {EventBus} from "./util/EventBus"
import {EventEmitter} from 'events'
export class Relay {
export class Relay extends EventEmitter {
constructor(socket) {
super()
this.socket = socket
this.bus = new EventBus()
this.listeners = [
socket.bus.addListener('message', (url, [verb, ...payload]) => {
this.bus.emit(verb, url, ...payload)
})
]
this.socket.on('message', this.onMessage)
}
get sockets() {
return [this.socket]
}
async send(...payload) {
await this.socket.connect()
send(...payload) {
this.socket.send(payload)
}
cleanup() {
this.bus.clear()
this.listeners.map(unsubscribe => unsubscribe())
onMessage = (url, [verb, ...payload]) => {
this.emit(verb, url, ...payload)
}
cleanup = () => {
this.socket.off('message', this.onMessage)
}
}