Skip to content

Commit

Permalink
Merge pull request #8 from clue-labs/close
Browse files Browse the repository at this point in the history
Remove event listeners once closed
  • Loading branch information
clue authored Jun 16, 2016
2 parents 813df56 + 942282e commit 53d01ea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Sequencer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
*/
class Sequencer extends EventEmitter implements ReadableStreamInterface
{
private $buffer = '';
private $expect = 0;
private $closed = false;

private $input;
private $invalid;

private $buffer = '';
private $closed = false;

public function __construct(ReadableStreamInterface $input, $replacementCharacter = '?')
{
$this->input = $input;
Expand Down Expand Up @@ -123,7 +122,10 @@ public function handleEnd()
$this->emit('data', array($data));
}

$this->emit('end', array());
if (!$this->closed) {
$this->emit('end');
$this->close();
}
}

/** @internal */
Expand All @@ -145,10 +147,12 @@ public function close()
}

$this->closed = true;
$this->buffer = '';

$this->input->close();

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

public function pause()
Expand Down
36 changes: 36 additions & 0 deletions tests/SequencerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ public function testClosingInputWillCloseSequencer()
$this->assertFalse($this->sequencer->isReadable());
}

public function testClosingInputWillRemoveAllDataListeners()
{
$this->input->close();

$this->assertEquals(array(), $this->input->listeners('data'));
$this->assertEquals(array(), $this->sequencer->listeners('data'));
}

public function testClosingSequencerWillCloseInput()
{
$this->input->on('close', $this->expectCallableOnce());
Expand All @@ -173,6 +181,25 @@ public function testClosingSequencerWillCloseInput()
$this->assertFalse($this->sequencer->isReadable());
}

public function testClosingSequencerWillRemoveAllDataListeners()
{
$this->sequencer->close();

$this->assertEquals(array(), $this->input->listeners('data'));
$this->assertEquals(array(), $this->sequencer->listeners('data'));
}

public function testClosingSequencerDuringFinalDataEventFromEndWillNotEmitEnd()
{
$this->sequencer->on('data', $this->expectCallableOnceWith('?'));
$this->sequencer->on('data', array($this->sequencer, 'close'));

$this->sequencer->on('end', $this->expectCallableNever());

$this->input->emit('data', array("\xc3"));
$this->input->emit('end');
}

public function testCustomReplacementEmitDataWithInvalidStartUtf8SequencesWillForwardOnceReplaced()
{
$this->sequencer = new Sequencer($this->input, 'X');
Expand Down Expand Up @@ -205,6 +232,15 @@ public function testUnreadableInputWillResultInUnreadableSequencer()
$this->assertFalse($this->sequencer->isReadable());
}

public function testUnreadableInputWillNotAddAnyEventListeners()
{
$this->input->close();
$this->sequencer = new Sequencer($this->input);

$this->assertEquals(array(), $this->input->listeners('data'));
$this->assertEquals(array(), $this->sequencer->listeners('data'));
}

public function testEmitErrorEventWillForwardAndClose()
{
$this->sequencer->on('error', $this->expectCallableOnce());
Expand Down

0 comments on commit 53d01ea

Please sign in to comment.