commit 858f393a7a65a9deeed41a6a09a64f5ceb625a64 Author: Sven Slootweg Date: Wed Apr 24 18:33:33 2019 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97008e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +yarn.lock \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..581a4fa --- /dev/null +++ b/index.js @@ -0,0 +1,32 @@ +"use strict"; + +const Promise = require("bluebird"); +const propagateAbort = require("@ppstreams/propagate-abort"); +const { isEndOfStream } = require("@ppstreams/end-of-stream-marker"); + +module.exports = function greedySinkStream(description, callback) { + return { + description: `greedy sink stream (${description})`, + abort: propagateAbort, + read: function produceValue_greedySinkStream(source) { + let lastResult; + + function attemptRead() { + return Promise.try(() => { + return source.read(); + }).then((value) => { + return callback(value); + }).then((result) => { + lastResult = result; + + return attemptRead(); + }).catch(isEndOfStream, () => { + /* Don't attempt to do another read, we're done. We return whatever value we got last from the specified callback. */ + return lastResult; + }); + } + + return attemptRead(); + } + }; +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..a480dcf --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "@ppstreams/greedy-sink", + "version": "0.1.0", + "main": "index.js", + "repository": "http://git.cryto.net/ppstreams/greedy-sink.git", + "author": "Sven Slootweg ", + "license": "WTFPL OR CC0-1.0", + "dependencies": { + "@ppstreams/propagate-abort": "^0.1.2", + "bluebird": "^3.5.4" + } +}