Files
welshman/packages/net/target/Multi.ts
T
2024-04-22 13:22:53 -07:00

27 lines
580 B
TypeScript

import {Emitter} from '@welshman/lib'
import type {Message} from '@welshman/util'
import type {Target} from '../Executor'
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())
}
}