Browse Source

Initial commit

master
Sven Slootweg 1 year ago
commit
858f393a7a
3 changed files with 46 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +32
    -0
      index.js
  3. +12
    -0
      package.json

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
node_modules
yarn.lock

+ 32
- 0
index.js View File

@@ -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();
}
};
};

+ 12
- 0
package.json View File

@@ -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 <admin@cryto.net>",
"license": "WTFPL OR CC0-1.0",
"dependencies": {
"@ppstreams/propagate-abort": "^0.1.2",
"bluebird": "^3.5.4"
}
}

Loading…
Cancel
Save