1 | 'use strict'
|
2 |
|
3 | module.exports = function drain (op, done) {
|
4 | var read, abort
|
5 |
|
6 | function sink (_read) {
|
7 | read = _read
|
8 | if(abort) return sink.abort()
|
9 |
|
10 |
|
11 |
|
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 | }
|