import {makeBroadcastStream} from 'jdefer'
// or
const {makeBroadcastStream} = require('jdefer')
const stream = makeBroadcastStream()
// to listen
const removeListener = stream.listen(
(value) => {
console.log(value)
}, {
onDone() {
console.log('done')
},
onError(error) {
console.error(error)
},
})
// to stop listening
removeListener()
// to broadcast
stream.next('foo')
// to broadcast error
stream.throw(new Error('bar'))
// to broadcast done
stream.done()
// iterate
for await (const value of stream) {
console.log(value)
}