Re-organize connection management

This commit is contained in:
Jonathan Staab
2023-08-14 16:31:17 -07:00
parent 3caa4c31d1
commit ee1ac84ab7
18 changed files with 2470 additions and 2050 deletions
+28
View File
@@ -0,0 +1,28 @@
import {EventEmitter} from 'events'
import {Connection} from '../Connection'
import type {PlexMessage, Message} from '../util/Socket'
export class Plex extends EventEmitter {
constructor(readonly urls: string[], readonly connection: Connection) {
super()
this.connection.on('receive', this.onMessage)
}
get connections() {
return [this.connection]
}
send = (...payload: Message) => {
this.connection.send([{relays: this.urls}, payload])
}
onMessage = (connection: Connection, [{relays}, [verb, ...payload]]: PlexMessage) => {
this.emit(verb, relays[0], ...payload)
}
cleanup = () => {
this.removeAllListeners()
this.connection.off('receive', this.onMessage)
}
}