Skip to content

Commit 17c3f6e

Browse files
authored
Merge pull request #17 from clue-labs/cleanup
Remove all event listeners when either stream closes
2 parents 0bca6d5 + 026a2a1 commit 17c3f6e

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/ControlCodeParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(ReadableStreamInterface $input)
4646
$this->input = $input;
4747

4848
if (!$this->input->isReadable()) {
49-
$this->close();
49+
return $this->close();
5050
}
5151

5252
$this->input->on('data', array($this, 'handleData'));
@@ -89,6 +89,7 @@ public function close()
8989
$this->input->close();
9090

9191
$this->emit('close');
92+
$this->removeAllListeners();
9293
}
9394

9495
/** @internal */

tests/ControlCodeParserTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,16 @@ public function testClosingInputWillCloseParser()
238238
$this->assertFalse($this->parser->isReadable());
239239
}
240240

241+
public function testClosingInputWillRemoveAllDataListeners()
242+
{
243+
$this->parser->on('data', $this->expectCallableNever());
244+
245+
$this->input->close();
246+
247+
$this->assertEquals(array(), $this->input->listeners('data'));
248+
$this->assertEquals(array(), $this->parser->listeners('data'));
249+
}
250+
241251
public function testClosingParserWillCloseInput()
242252
{
243253
$this->input = $this->getMock('React\Stream\ReadableStreamInterface');
@@ -252,6 +262,19 @@ public function testClosingParserWillCloseInput()
252262
$this->assertFalse($this->parser->isReadable());
253263
}
254264

265+
public function testClosingParserWillRemoveAllDataListeners()
266+
{
267+
$this->input = new ReadableStream();
268+
$this->parser = new ControlCodeParser($this->input);
269+
270+
$this->parser->on('data', $this->expectCallableNever());
271+
272+
$this->parser->close();
273+
274+
$this->assertEquals(array(), $this->input->listeners('data'));
275+
$this->assertEquals(array(), $this->parser->listeners('data'));
276+
}
277+
255278
public function testClosingParserMultipleTimesWillOnlyCloseOnce()
256279
{
257280
$this->input = $this->getMock('React\Stream\ReadableStreamInterface');
@@ -275,6 +298,17 @@ public function testPassingClosedInputToParserWillCloseParser()
275298
$this->assertFalse($this->parser->isReadable());
276299
}
277300

301+
public function testPassingClosedInputToParserWillNotAddAnyDataListeners()
302+
{
303+
$this->input = new ReadableStream();
304+
$this->input->close();
305+
306+
$this->parser = new ControlCodeParser($this->input);
307+
308+
$this->assertEquals(array(), $this->input->listeners('data'));
309+
$this->assertEquals(array(), $this->parser->listeners('data'));
310+
}
311+
278312
public function testWillForwardPauseToInput()
279313
{
280314
$this->input = $this->getMock('React\Stream\ReadableStreamInterface');

0 commit comments

Comments
 (0)