Skip to content

Commit e5727a5

Browse files
authored
Merge pull request #38 from clue-labs/chunks
Improve performance by processing input stream in bigger chunks
2 parents 237219c + dad3f60 commit e5727a5

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"react/event-loop": "0.3.*|0.4.*",
1616
"react/stream": "^0.4.2",
1717
"clue/utf8-react": "^0.1",
18-
"clue/term-react": "^0.1"
18+
"clue/term-react": "^0.1.1"
1919
},
2020
"autoload": {
2121
"psr-4": { "Clue\\React\\Stdio\\": "src/" }

src/Stdin.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ class Stdin extends Stream
1414
public function __construct(LoopInterface $loop)
1515
{
1616
parent::__construct(STDIN, $loop);
17-
18-
$this->bufferSize = 1;
1917
}
2018

2119
public function resume()

tests/ReadlineTest.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -195,36 +195,47 @@ public function testDataEventWillBeEmittedForCompleteLine()
195195
{
196196
$this->readline->on('data', $this->expectCallableOnceWith('hello'));
197197

198-
$this->pushInputBytes($this->readline, "hello\n");
198+
$this->input->emit('data', array("hello\n"));
199199
}
200200

201-
public function testDataEventWillNotBeEmittedForIncompleteLine()
201+
public function testDataEventWillNotBeEmittedForIncompleteLineButWillStayInInputBuffer()
202202
{
203203
$this->readline->on('data', $this->expectCallableNever());
204204

205-
$this->pushInputBytes($this->readline, "hello");
205+
$this->input->emit('data', array("hello"));
206+
207+
$this->assertEquals('hello', $this->readline->getInput());
208+
}
209+
210+
public function testDataEventWillBeEmittedForCompleteLineAndRemainingWillStayInInputBuffer()
211+
{
212+
$this->readline->on('data', $this->expectCallableOnceWith('hello'));
213+
214+
$this->input->emit('data', array("hello\nworld"));
215+
216+
$this->assertEquals('world', $this->readline->getInput());
206217
}
207218

208219
public function testDataEventWillBeEmittedForEmptyLine()
209220
{
210221
$this->readline->on('data', $this->expectCallableOnceWith(''));
211222

212-
$this->pushInputBytes($this->readline, "\n");
223+
$this->input->emit('data', array("\n"));
213224
}
214225

215226
public function testWriteSimpleCharWritesOnce()
216227
{
217228
$this->output->expects($this->once())->method('write')->with($this->equalTo("\r\033[K" . "k"));
218229

219-
$this->pushInputBytes($this->readline, 'k');
230+
$this->input->emit('data', array('k'));
220231
}
221232

222233
public function testWriteMultiByteCharWritesOnce()
223234
{
224235
$this->output->expects($this->once())->method('write')->with($this->equalTo("\r\033[K" . "\xF0\x9D\x84\x9E"));
225236

226237
// "𝄞" – U+1D11E MUSICAL SYMBOL G CLEF
227-
$this->pushInputBytes($this->readline, "\xF0\x9D\x84\x9E");
238+
$this->input->emit('data', array("\xF0\x9D\x84\x9E"));
228239
}
229240

230241
public function testKeysHomeMovesToFront()
@@ -276,7 +287,7 @@ public function testKeysRightMovesToRight(Readline $readline)
276287

277288
public function testKeysSimpleChars()
278289
{
279-
$this->pushInputBytes($this->readline, 'hi!');
290+
$this->input->emit('data', array('hi!'));
280291

281292
$this->assertEquals('hi!', $this->readline->getInput());
282293
$this->assertEquals(3, $this->readline->getCursorPosition());
@@ -300,7 +311,7 @@ public function testKeysBackspaceDeletesLastCharacter(Readline $readline)
300311

301312
public function testKeysMultiByteInput()
302313
{
303-
$this->pushInputBytes($this->readline, '');
314+
$this->input->emit('data', array(''));
304315

305316
$this->assertEquals('', $this->readline->getInput());
306317
$this->assertEquals(2, $this->readline->getCursorPosition());
@@ -372,7 +383,7 @@ public function testKeysPrependCharacterInFrontOfMultiByte()
372383
$this->readline->setInput('ü');
373384
$this->readline->moveCursorTo(0);
374385

375-
$this->pushInputBytes($this->readline, 'h');
386+
$this->input->emit('data', array('h'));
376387

377388
$this->assertEquals('', $this->readline->getInput());
378389
$this->assertEquals(1, $this->readline->getCursorPosition());
@@ -383,7 +394,7 @@ public function testKeysWriteMultiByteAfterMultiByte()
383394
{
384395
$this->readline->setInput('ü');
385396

386-
$this->pushInputBytes($this->readline, 'ä');
397+
$this->input->emit('data', array('ä'));
387398

388399
$this->assertEquals('üä', $this->readline->getInput());
389400
$this->assertEquals(2, $this->readline->getCursorPosition());
@@ -395,7 +406,7 @@ public function testKeysPrependMultiByteInFrontOfMultiByte()
395406
$this->readline->setInput('ü');
396407
$this->readline->moveCursorTo(0);
397408

398-
$this->pushInputBytes($this->readline, 'ä');
409+
$this->input->emit('data', array('ä'));
399410

400411
$this->assertEquals('äü', $this->readline->getInput());
401412
$this->assertEquals(1, $this->readline->getCursorPosition());
@@ -618,11 +629,4 @@ public function testPipeWillReturnDest()
618629

619630
$this->assertEquals($dest, $ret);
620631
}
621-
622-
private function pushInputBytes(Readline $readline, $bytes)
623-
{
624-
foreach (str_split($bytes, 1) as $byte) {
625-
$this->input->emit('data', array($byte));
626-
}
627-
}
628632
}

0 commit comments

Comments
 (0)