git ssb

2+

Dominic / pull-stream



Tree: 4030fd72088498cc22acca950c4a1bb121937cc0

Files: 4030fd72088498cc22acca950c4a1bb121937cc0 / sinks / drain.js

1232 bytesRaw
1'use strict'
2
3module.exports = function drain (op, done) {
4 var read, abort
5
6 function sink (_read) {
7 read = _read
8 if(abort) return sink.abort()
9 //this function is much simpler to write if you
10 //just use recursion, but by using a while loop
11 //we do not blow the stack if the stream happens to be sync.
12 ;(function next() {
13 var loop = true, cbed = false
14 while(loop) {
15 cbed = false
16 read(null, function (end, data) {
17 cbed = true
18 if(end = end || abort) {
19 loop = false
20 if(done) done(end === true ? null : end)
21 else if(end && end !== true)
22 throw end
23 }
24 else if(op && false === op(data) || abort) {
25 loop = false
26 read(abort || true, done || function () {})
27 }
28 else if(!loop){
29 next()
30 }
31 })
32 if(!cbed) {
33 loop = false
34 return
35 }
36 }
37 })()
38 }
39
40 sink.abort = function (err, cb) {
41 if('function' == typeof err)
42 cb = err, err = true
43 abort = err || true
44 if(read) return read(abort, cb || function () {})
45 }
46
47 return sink
48}
49

Built with git-ssb-web