Skip to content

Instantly share code, notes, and snippets.

@lachenmayer
Created April 5, 2017 12:02
Show Gist options
  • Save lachenmayer/89eff362b54586a84d2f0be2c4a01c11 to your computer and use it in GitHub Desktop.
Save lachenmayer/89eff362b54586a84d2f0be2c4a01c11 to your computer and use it in GitHub Desktop.
converting xstream to/from node streams
const xs = require('xstream').default
module.exports = function fromStream (stream, flush = stream => {}) {
return xs.create({
start: listener => {
stream.on('data', data => { listener.next(data) })
stream.on('error', error => { listener.error(error) })
stream.on('end', () => { listener.complete() })
},
stop: () => {
flush(stream)
}
})
}
const from = require('from2')
module.exports = function toStream (xstream) {
let next
let listening = false
return from.obj(function (_, n) {
next = n
if (!listening) {
xstream.addListener({
next: x => { next(null, x) },
error: e => { next(e, null) },
complete: () => { next(null, null) },
})
listening = true
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment