Created
April 5, 2017 12:02
-
-
Save lachenmayer/89eff362b54586a84d2f0be2c4a01c11 to your computer and use it in GitHub Desktop.
converting xstream to/from node streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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