-
-
Notifications
You must be signed in to change notification settings - Fork 63
Description
In Util::pipe() the default behavior is to connect the sources end event with the destination's end event, but it does not forward any potential $data.
// forward end event from source as $dest->end()
$end = isset($options['end']) ? $options['end'] : true;
if ($end) {
$source->on('end', $ender = function () use ($dest) {
$dest->end();
});
$dest->on('close', function () use ($source, $ender) {
$source->removeListener('end', $ender);
});
} I've had to wrap Util::pipe() with my own function that basically mixes this behavior with the forwardEvents util call
public function pipe(WritableStreamInterface $dest, array $options = array())
{
$options['end'] = FALSE;
//default pipe behavior throws away $data for end events
$this->on('end', $ender = function () use($dest) {
call_user_func_array([$dest, 'end'], func_get_args());
});
$source = $this;
$dest->on('close', function () use ($source, $ender) {
$source->removeListener('end', $ender);
});
return Util::pipe($this, $dest, $options);
} Should end() not be used this way? I'm writing a pipe that will collect data from streams and concat into one file, when this "destination" pipe gets end() from its source it will concat all the info into one file and emit its own end with the name of the file as the only value of $data (wrapped in an array). I could rewrite it to emit 'data' one time, but it seems more natural for a pipe component that waits for upstream pipes to send end that it would also send end.
Editing the Util.php file also gives me the result I was expecting:
// forward end event from source as $dest->end()
$end = isset($options['end']) ? $options['end'] : true;
if ($end) {
$source->on('end', $ender = function ($data=null) use ($dest) { //change 1 of 2
$dest->end($data); //change 2 of 2
});
$dest->on('close', function () use ($source, $ender) {
$source->removeListener('end', $ender);
});
}