Skip to content

Commit

Permalink
Merge pull request #116 from clue-labs/close
Browse files Browse the repository at this point in the history
 Remove event listeners from CompositeStream once closed and remove remove undocumented left-over close event argument
  • Loading branch information
WyriHaximus authored Oct 11, 2017
2 parents a1a9754 + 1d200c0 commit 1da4a66
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/CompositeStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

final class CompositeStream extends EventEmitter implements DuplexStreamInterface
{
protected $readable;
protected $writable;
protected $closed = false;
private $readable;
private $writable;
private $closed = false;

public function __construct(ReadableStreamInterface $readable, WritableStreamInterface $writable)
{
Expand Down Expand Up @@ -77,5 +77,6 @@ public function close()
$this->writable->close();

$this->emit('close');
$this->removeAllListeners();
}
}
8 changes: 1 addition & 7 deletions src/WritableResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,9 @@ public function close()
$this->writable = false;
$this->data = '';

$this->emit('close', array($this));
$this->emit('close');
$this->removeAllListeners();

$this->handleClose();
}

/** @internal */
public function handleClose()
{
if (is_resource($this->stream)) {
fclose($this->stream);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/CompositeStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ public function itShouldForwardCloseOnlyOnce()
$writable->close();
}

/** @test */
public function itShouldForwardCloseAndRemoveAllListeners()
{
$in = new ThroughStream();

$composite = new CompositeStream($in, $in);
$composite->on('close', $this->expectCallableOnce());

$this->assertTrue($composite->isReadable());
$this->assertTrue($composite->isWritable());
$this->assertCount(1, $composite->listeners('close'));

$composite->close();

$this->assertFalse($composite->isReadable());
$this->assertFalse($composite->isWritable());
$this->assertCount(0, $composite->listeners('close'));
}

/** @test */
public function itShouldReceiveForwardedEvents()
{
Expand Down

0 comments on commit 1da4a66

Please sign in to comment.