Gathering detailed insights and metrics for @promistream/simple-sink
Gathering detailed insights and metrics for @promistream/simple-sink
Gathering detailed insights and metrics for @promistream/simple-sink
Gathering detailed insights and metrics for @promistream/simple-sink
npm install @promistream/simple-sink
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
12
2
A generic sink stream for Promistreams that can cover most usecases. It automatically drives the pipeline above it once it is read from once, in its default configuration.
Note that if you simply need to collect all of the values that came from a pipeline into an array, you are probably looking for @promistream/collect
instead, which is built on top of this stream. This library (@promistream/simple-sink
) is only useful if you need lower-level control over reads and cumulative results than that.
Stream characteristics:
Both of these examples are included in the package in runnable form.
A simple stream (example-simple.js
):
1"use strict"; 2 3const pipe = require("@promistream/pipe"); 4const fromIterable = require("@promistream/from-iterable"); 5const simpleSink = require("@promistream/simple-sink"); 6 7(async () => { 8 let result = await pipe([ 9 fromIterable([ 1, 2, 3, 4, 5 ]), 10 simpleSink(async (value, abort) => { 11 console.log("value seen:", value); 12 }) 13 ]).read(); 14 15 console.log("result:", result); 16})(); 17 18/* Output: 19value seen: 1 20value seen: 2 21value seen: 3 22value seen: 4 23value seen: 5 24result: undefined 25*/
A stream that uses more of the features (example.js
):
1"use strict"; 2 3const pipe = require("@promistream/pipe"); 4const fromIterable = require("@promistream/from-iterable"); 5const simpleSink = require("@promistream/simple-sink"); 6 7(async () => { 8 try { 9 let result = await pipe([ 10 fromIterable([ 1, 2, 3, 4, 5 ]), 11 simpleSink({ 12 onValue: async (value, abort) => { 13 console.log("value seen:", value); 14 // if (value === 3) { abort(new Error("uncomment this to trigger an abort")); } 15 }, 16 onEnd: async () => { 17 console.log("stream finished"); 18 return "arbitrary value"; 19 }, 20 onAbort: async (error) => { 21 console.log("stream aborted due to reason:", error); 22 } 23 }) 24 ]).read(); 25 26 console.log("result:", result); 27 } catch (error) { 28 console.log("caught error:", error); 29 } 30})(); 31 32/* Output: 33value seen: 1 34value seen: 2 35value seen: 3 36value seen: 4 37value seen: 5 38stream finished 39result: arbitrary value 40*/
In the typical, simple usecase, all you need to do is to specify a single async callback that does something with each value, and returns (resolves) once it is done. You may also need to specify an onEnd
handler to produce a cumulative result once the pipeline ends. The other options are mainly for more complex cases.
onValue
option), or an object of options:
(value, abort)
, where the value
is the value that has been read, and abort
is a callable function. There are a few things that this callback can do:
undefined
: This pauses the reading by the sink until the Promise has resolved, and so can be used to delay reads (by having the Promise resolve at a later time).undefined
value: This causes the pipeline's read
operation to return before the source stream has ended; this can be used to do some internal reading of a pipeline before handing it over to user code, for example, like in a protocol handler. Note that in this case, the stream-end handlers will not be called! The sink simply detaches and leaves the rest of the pipeline untouched.abort
function with true
as the argument: This asks the source stream to terminate early. It is equivalent to the abort
method in the Promistream spec, and should only be used under success conditions (ie. the stream should terminate early for an expected and normal reason). Note that source streams are not required to honour this request.abort
function with an Error object as the argument: This terminates the stream early, under error conditions. It is again equivalent to abort
in the spec, and should be used in case of unexpected errors. Note that an Error that's thrown/rejected from the callback is handled the same way, so you should rarely need this.read
that started the sink processing.No vulnerabilities found.
No security vulnerabilities found.