Permalink
Browse files

simplify drain

  • Loading branch information...
1 parent b4a4450 commit 4eb87fd7142faa9cb92207fccf2ee0fa65bb6f61 @dominictarr dominictarr committed Jun 6, 2013
Showing with 20 additions and 20 deletions.
  1. +20 −20 sinks.js
View
@@ -1,28 +1,28 @@
var drain = exports.drain = function (read, op, done) {
+
;(function next() {
- var sync = true, returned = false, loop = true
- do {
- returned = false; sync = true
- read(null, function (err, data) {
- returned = true
-
- if(err) {
- done && done(err === true ? null : err)
- return loop = false
+ var loop = true, cbed = false
+ while(loop) {
+ cbed = false
+ read(null, function (end, data) {
+ cbed = true
+ if(end) {
+ loop = false
+ done && done(end === true ? null : end)
}
-
- if(op) {
- //return false to abort!
- if(false === op(data)) {
- loop = false
- return read(true, done || function () {})
- }
+ else if(op && false === op(data)) {
+ loop = false
+ read(true, done || function () {})
+ }
+ else if(!loop){
+ next()
}
- if(!sync) next()
})
- sync = false
- if(!returned) return
- } while (loop);
+ if(!cbed) {
+ loop = false
+ return
+ }
+ }
})()
}

0 comments on commit 4eb87fd

Please sign in to comment.