Skip to content

Commit 0bca6d5

Browse files
authored
Merge pull request #16 from clue-labs/close-on-data
Stop emitting events when closing stream during event handler
2 parents 251e42a + 3eb60bb commit 0bca6d5

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/ControlCodeParser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function close()
8484
}
8585

8686
$this->closed = true;
87+
$this->buffer = '';
8788

8889
$this->input->close();
8990

tests/ControlCodeParserTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ public function testEmitsDataInTwoChunkWithC0InBetweenWhileAddingDuringDataEvent
122122
$this->input->emit('data', array("hello\nworld"));
123123
}
124124

125+
public function testEmitsDataOnlyFirstChunkOfMultipleWhenClosingDuringFirstDataEvent()
126+
{
127+
// first data event is everything before control code
128+
$first = $this->expectCallableOnceWith('hello');
129+
$this->parser->on('data', $first);
130+
131+
// close the input stream on the first data event => no more data events
132+
$this->parser->once('data', array($this->input, 'close'));
133+
134+
$this->input->emit('data', array("hello\nworld"));
135+
}
136+
125137
public function testEmitsC0AndData()
126138
{
127139
$this->parser->on('data', $this->expectCallableOnceWith("hello world"));

0 commit comments

Comments
 (0)