Skip to content

Commit 9b19a3a

Browse files
committed
Merge pull request #1 from clue-labs/close
Closing stream should also close input stream
2 parents 41c40ac + 66b0ca1 commit 9b19a3a

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/UnwrapReadableStream.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ function (ReadableStreamInterface $stream) use ($out, &$closed) {
5656
});
5757

5858
// forward end events and close
59-
$stream->on('end', function () use ($out) {
60-
$out->emit('end', array($out));
61-
$out->close();
59+
$stream->on('end', function () use ($out, &$closed) {
60+
if (!$closed) {
61+
$out->emit('end', array($out));
62+
$out->close();
63+
}
6264
});
6365

6466
// error events cancel output stream
@@ -67,10 +69,9 @@ function (ReadableStreamInterface $stream) use ($out, &$closed) {
6769
$out->close();
6870
});
6971

70-
// close output stream once input closes
71-
$stream->on('close', function () use ($out) {
72-
$out->close();
73-
});
72+
// close both streams once either side closes
73+
$stream->on('close', array($out, 'close'));
74+
$out->on('close', array($stream, 'close'));
7475

7576
return $stream;
7677
},

tests/UnwrapReadableTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ public function testPipingStreamWillForwardDataEvents()
211211
$output->promise()->then($this->expectCallableOnceWith('helloworld'));
212212
}
213213

214+
public function testClosingStreamWillCloseInputStream()
215+
{
216+
$input = $this->getMock('React\Stream\ReadableStreamInterface');
217+
$input->expects($this->once())->method('isReadable')->willReturn(true);
218+
$input->expects($this->once())->method('close');
219+
220+
$promise = Promise\resolve($input);
221+
$stream = Stream\unwrapReadable($promise);
222+
223+
$stream->close();
224+
}
225+
214226
public function testClosingStreamWillCloseStreamIfItIgnoredCancellationAndResolvesLater()
215227
{
216228
$input = new ReadableStream();

0 commit comments

Comments
 (0)