Skip to content

Commit 977d94f

Browse files
committed
Simplify event forwarding semantics to not generate additional events
1 parent e203df1 commit 977d94f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/UnwrapReadableStream.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,23 @@ function (ReadableStreamInterface $stream) use ($out) {
4747
$out->emit('data', array($data, $out));
4848
});
4949

50+
// forward end events and close
51+
$stream->on('end', function () use ($out) {
52+
$out->emit('end', array($out));
53+
$out->close();
54+
});
55+
5056
// error events cancel output stream
5157
$stream->on('error', function ($error) use ($out) {
5258
$out->emit('error', array($error, $out));
5359
$out->close();
5460
});
5561

56-
// close output stream once body closes
62+
// close output stream once input closes
5763
$stream->on('close', function () use ($out) {
5864
$out->close();
5965
});
60-
$stream->on('end', function () use ($out) {
61-
$out->close();
62-
});
66+
6367
return $stream;
6468
},
6569
function ($e) use ($out) {
@@ -108,7 +112,6 @@ public function close()
108112
$this->promise->cancel();
109113
}
110114

111-
$this->emit('end', array($this));
112115
$this->emit('close', array($this));
113116
}
114117
}

tests/UnwrapReadableTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function testClosingStreamMakesItNotReadable()
3030
$promise = new \React\Promise\Promise(function () { });
3131
$stream = Stream\unwrapReadable($promise);
3232

33+
$stream->on('close', $this->expectCallableOnce());
34+
$stream->on('end', $this->expectCallableNever());
35+
3336
$stream->close();
3437

3538
$this->assertFalse($stream->isReadable());
@@ -54,6 +57,7 @@ public function testEmitsErrorWhenPromiseRejects()
5457
$this->assertTrue($stream->isReadable());
5558

5659
$stream->on('error', $this->expectCallableOnce());
60+
$stream->on('end', $this->expectCallableNever());
5761

5862
$this->loop->run();
5963

@@ -69,6 +73,7 @@ public function testEmitsErrorWhenPromiseResolvesWithWrongValue()
6973
$this->assertTrue($stream->isReadable());
7074

7175
$stream->on('error', $this->expectCallableOnce());
76+
$stream->on('end', $this->expectCallableNever());
7277

7378
$this->loop->run();
7479

@@ -160,6 +165,7 @@ public function testEmitsCloseOnlyOnceWhenClosingStreamMultipleTimes()
160165
$promise = new Promise\Promise(function () { });
161166
$stream = Stream\unwrapReadable($promise);
162167

168+
$stream->on('end', $this->expectCallableNever());
163169
$stream->on('close', $this->expectCallableOnce());
164170

165171
$stream->close();

0 commit comments

Comments
 (0)