Files
welshman/lib/Relay.ts
T
2023-03-27 15:05:34 -05:00

22 lines
445 B
TypeScript

import {EventBus} from "./util/EventBus"
export class Relay {
constructor(socket) {
this.socket = socket
this.bus = new EventBus()
this.onMessage = this.onMessage.bind(this)
this.socket.bus.on('message', this.onMessage)
}
async send(...payload) {
await this.socket.connect()
this.socket.send(payload)
}
onMessage(message) {
const [verb, ...payload] = message
this.bus.handle(verb, ...payload)
}
}