Skip to content

Commit 8bd3b3f

Browse files
committed
Simplify closing stream
1 parent 437da5a commit 8bd3b3f

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/UnwrapReadableStream.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ public function __construct(PromiseInterface $promise)
3030

3131
// TODO: support backpressure
3232

33-
// try to cancel promise once the stream closes
34-
if ($promise instanceof CancellablePromiseInterface) {
35-
$out->on('close', function() use ($promise) {
36-
$promise->cancel();
37-
});
38-
}
39-
4033
$this->promise = $promise->then(
4134
function ($stream) {
4235
if (!($stream instanceof ReadableStreamInterface)) {
@@ -106,6 +99,11 @@ public function close()
10699

107100
$this->closed = true;
108101

102+
// try to cancel promise once the stream closes
103+
if ($this->promise instanceof CancellablePromiseInterface) {
104+
$this->promise->cancel();
105+
}
106+
109107
$this->emit('end', array($this));
110108
$this->emit('close', array($this));
111109
}

tests/UnwrapReadableTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,15 @@ public function testEmitsEndAndClosesWhenInputEmitsEnd()
153153

154154
$this->assertFalse($stream->isReadable());
155155
}
156+
157+
public function testEmitsCloseOnlyOnceWhenClosingStreamMultipleTimes()
158+
{
159+
$promise = new Promise\Promise(function () { });
160+
$stream = Stream\unwrapReadable($promise);
161+
162+
$stream->on('close', $this->expectCallableOnce());
163+
164+
$stream->close();
165+
$stream->close();
166+
}
156167
}

0 commit comments

Comments
 (0)