Skip to content

Commit 1da4a66

Browse files
authored
Merge pull request #116 from clue-labs/close
Remove event listeners from CompositeStream once closed and remove remove undocumented left-over close event argument
2 parents a1a9754 + 1d200c0 commit 1da4a66

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/CompositeStream.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
final class CompositeStream extends EventEmitter implements DuplexStreamInterface
88
{
9-
protected $readable;
10-
protected $writable;
11-
protected $closed = false;
9+
private $readable;
10+
private $writable;
11+
private $closed = false;
1212

1313
public function __construct(ReadableStreamInterface $readable, WritableStreamInterface $writable)
1414
{
@@ -77,5 +77,6 @@ public function close()
7777
$this->writable->close();
7878

7979
$this->emit('close');
80+
$this->removeAllListeners();
8081
}
8182
}

src/WritableResourceStream.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,9 @@ public function close()
9292
$this->writable = false;
9393
$this->data = '';
9494

95-
$this->emit('close', array($this));
95+
$this->emit('close');
9696
$this->removeAllListeners();
9797

98-
$this->handleClose();
99-
}
100-
101-
/** @internal */
102-
public function handleClose()
103-
{
10498
if (is_resource($this->stream)) {
10599
fclose($this->stream);
106100
}

tests/CompositeStreamTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ public function itShouldForwardCloseOnlyOnce()
188188
$writable->close();
189189
}
190190

191+
/** @test */
192+
public function itShouldForwardCloseAndRemoveAllListeners()
193+
{
194+
$in = new ThroughStream();
195+
196+
$composite = new CompositeStream($in, $in);
197+
$composite->on('close', $this->expectCallableOnce());
198+
199+
$this->assertTrue($composite->isReadable());
200+
$this->assertTrue($composite->isWritable());
201+
$this->assertCount(1, $composite->listeners('close'));
202+
203+
$composite->close();
204+
205+
$this->assertFalse($composite->isReadable());
206+
$this->assertFalse($composite->isWritable());
207+
$this->assertCount(0, $composite->listeners('close'));
208+
}
209+
191210
/** @test */
192211
public function itShouldReceiveForwardedEvents()
193212
{

0 commit comments

Comments
 (0)