Browse Source

Initial commit

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

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

+ 32 - 0
index.js

@ -0,0 +1,32 @@
1
"use strict";
2
3
const Promise = require("bluebird");
4
const propagateAbort = require("@ppstreams/propagate-abort");
5
const { isEndOfStream } = require("@ppstreams/end-of-stream-marker");
6
7
module.exports = function greedySinkStream(description, callback) {
8
	return {
9
		description: `greedy sink stream (${description})`,
10
		abort: propagateAbort,
11
		read: function produceValue_greedySinkStream(source) {
12
			let lastResult;
13
14
			function attemptRead() {
15
				return Promise.try(() => {
16
					return source.read();
17
				}).then((value) => {
18
					return callback(value);
19
				}).then((result) => {
20
					lastResult = result;
21
22
					return attemptRead();
23
				}).catch(isEndOfStream, () => {
24
					/* Don't attempt to do another read, we're done. We return whatever value we got last from the specified callback. */
25
					return lastResult;
26
				});
27
			}
28
29
			return attemptRead();
30
		}
31
	};
32
};

+ 12 - 0
package.json

@ -0,0 +1,12 @@
1
{
2
  "name": "@ppstreams/greedy-sink",
3
  "version": "0.1.0",
4
  "main": "index.js",
5
  "repository": "http://git.cryto.net/ppstreams/greedy-sink.git",
6
  "author": "Sven Slootweg <admin@cryto.net>",
7
  "license": "WTFPL OR CC0-1.0",
8
  "dependencies": {
9
    "@ppstreams/propagate-abort": "^0.1.2",
10
    "bluebird": "^3.5.4"
11
  }
12
}