Files
welshman/src/connect/target/Multi.ts
T
2024-02-26 15:04:31 -08:00

27 lines
585 B
TypeScript

import type {Target} from '../Executor'
import type {Message} from '../connect/Socket'
import {Emitter} from '../util/Emitter'
export class Multi extends Emitter {
constructor(readonly targets: Target[]) {
super()
targets.forEach(t => {
t.on('*', (verb, ...args) => this.emit(verb, ...args))
})
}
get connections() {
return this.targets.flatMap(t => t.connections)
}
send(...payload: Message) {
this.targets.forEach(t => t.send(...payload))
}
cleanup = () => {
this.removeAllListeners()
this.targets.forEach(t => t.cleanup())
}
}