Last active
January 6, 2024 04:19
-
-
Save raimohanska/6202053 to your computer and use it in GitHub Desktop.
Bacon.fromNodeStream
This file contains hidden or 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
fs = require("fs") | |
Bacon = require("baconjs") | |
# Bacon.fromNodeStream converts Node.js ReadableStream into an EventStream | |
Bacon.fromNodeStream = (stream) -> | |
Bacon.fromBinder (sink) -> | |
listeners = {} | |
addListener = (event, listener) -> | |
listeners[event] = listener | |
stream.on event, listener | |
addListener "data", (chunk) -> sink(chunk) | |
addListener "end", () -> sink(new Bacon.End()) | |
addListener "error", (error) -> sink(new Bacon.Error(error)) | |
-> for event,listener of listeners | |
stream.removeListener event,listener | |
# Example code | |
toCharStream = (str) -> Bacon.fromArray(str.split("")) | |
Bacon.fromNodeStream(fs.createReadStream "bacon.md", { encoding: "utf8" }) | |
.flatMap(toCharStream) | |
.bufferWithCount(80) | |
.map(".join", ""). | |
.log() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment