Skip to content
This repository

stream: notify user of to pipe state via 'unpipe' event. #3785

Closed
wants to merge 1 commit into from

4 participants

Dominic Tarr Isaac Z. Schlueter Nodejs Jenkins Timothy J Fontaine
Dominic Tarr

this does not change the semantics of any of the current Stream methods or events,
but makes only a tiny two line change, which gives the user a lot more power.

firstly, by emitting pipe's internal cleanup function to the dest it gives the option of disconnecting the stream.

secondly, by emitting source.emit('unpipe', dest) it allows the source to know when the pipe is disconnected.

this would allow users to do things like

requestThatMayFail
  .on('pipe', function (source, unpipe) {
    requestThatMayFail.unpipe = unpipe
  })
  .on('error', function (){
    requestThatMayFail.unpipe()
  })

stream
  .pipe(requestThatMayFail)
stream
  .pipe(aVeryReliableStream)

Also, it would become possible to notify the upstream pipeline that the pipe has ended,
which could be necessary for long pipelines.
(such was the intention of: #3759 ) but with out changing any semantics.

Isaac Z. Schlueter
Collaborator

Why is the cleanup function passed to the dest's pipe event?

so that the dest can trigger the unpipe if it so wishes.
it's probably gonna be the dest that needs to do so, because say, the dest has errored.
although, with a tee pipe, in the new api, you could maybe pass the particular dest to source.unpipe(dest)

the most useful case for this would be piping some thing to multiple places, for redundancy, expecting some to possibly
fail. hmm, I think this is a sufficiently rare use case to justify a userland stream.

Isaac Z. Schlueter
Collaborator
isaacs commented

I'm not opposed to this in principle. However:

  1. It's new API, so can't be in 0.8.
  2. It's going to be deprecated immediately in 0.9, since pipe() is a whole different animal.
Nodejs Jenkins
Collaborator

Can one of the admins verify this patch?

Timothy J Fontaine
Owner

If you'd like, we can revisit some of these concepts, but closing this as it is stale and would like to explore it with a new pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jul 27, 2012
Dominic Tarr dominictarr stream: notify user of to pipe state via 'unpipe' event. bc1e02c
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 3 additions and 1 deletion. Show diff stats Hide diff stats

  1. +3 1 lib/stream.js
4 lib/stream.js
@@ -101,6 +101,8 @@ Stream.prototype.pipe = function(dest, options) {
101 101
102 102 dest.removeListener('end', cleanup);
103 103 dest.removeListener('close', cleanup);
  104 +
  105 + source.emit('unpipe', dest);
104 106 }
105 107
106 108 source.on('end', cleanup);
@@ -109,7 +111,7 @@ Stream.prototype.pipe = function(dest, options) {
109 111 dest.on('end', cleanup);
110 112 dest.on('close', cleanup);
111 113
112   - dest.emit('pipe', source);
  114 + dest.emit('pipe', source, cleanup);
113 115
114 116 // Allow for unix-like usage: A.pipe(B).pipe(C)
115 117 return dest;

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.