From 0a3031822141e9946dc855b1aa69d690cb95d301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 10 Oct 2017 13:15:57 +0200 Subject: [PATCH 1/2] Remove undocumented left-over close event argument --- src/WritableResourceStream.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/WritableResourceStream.php b/src/WritableResourceStream.php index 8230ff9..f43f95c 100644 --- a/src/WritableResourceStream.php +++ b/src/WritableResourceStream.php @@ -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); } From 1d200c09c36391a4ca1a2fac09ceb08ad01aab9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 10 Oct 2017 15:54:04 +0200 Subject: [PATCH 2/2] Remove event listeners from CompositeStream once closed --- src/CompositeStream.php | 7 ++++--- tests/CompositeStreamTest.php | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/CompositeStream.php b/src/CompositeStream.php index d9253e4..153f2a3 100644 --- a/src/CompositeStream.php +++ b/src/CompositeStream.php @@ -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) { @@ -77,5 +77,6 @@ public function close() $this->writable->close(); $this->emit('close'); + $this->removeAllListeners(); } } diff --git a/tests/CompositeStreamTest.php b/tests/CompositeStreamTest.php index 4b4b2f4..df89c3e 100644 --- a/tests/CompositeStreamTest.php +++ b/tests/CompositeStreamTest.php @@ -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() {