Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Created August 16, 2013 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdickinson/6ba25896cf7b8463065b to your computer and use it in GitHub Desktop.
Save chrisdickinson/6ba25896cf7b8463065b to your computer and use it in GitHub Desktop.
var oneToOne = function(innerWrite, innerEnd) {
var stream = through(write, end)
, pending = 0
, queue
, ended
, abort
queue = stream.queue
stream.queue = _queue
return stream
function _queue(what) {
--pending
if(what === null) {
abort = true
}
queue.call(stream, what)
return check()
}
function write(datum) {
++pending
innerWrite.call(stream, datum)
}
function end() {
ended = true
check()
}
function check() {
if(pending || abort) {
return
}
innerEnd.call(stream)
if(abort) {
return
}
queue.call(stream, null)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment