Skip to content

Commit

Permalink
Add sleeps to optimise CPU
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed May 29, 2024
1 parent 3546e39 commit cf507d3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
20 changes: 13 additions & 7 deletions src/Components/InteractiveComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private function renderComponent(Console $console, InteractiveComponent $compone
$terminal = $this->createTerminal($console);

$fibers = [
new Fiber(fn () => $this->applyKey($component, $validation)),
new Fiber(fn () => $this->applyKey($component, $console, $validation)),
new Fiber(fn () => $this->renderFrames($component, $terminal)),
];

Expand All @@ -60,6 +60,12 @@ private function renderComponent(Console $console, InteractiveComponent $compone
}
}
}

// If we're running within a fiber, we'll suspend here as well so that the parent can continue
// This is needed for our testing helper
if (Fiber::getCurrent()) {
Fiber::suspend();
}
}
} catch (InterruptException $interruptException) {
$this->closeTerminal($terminal);
Expand All @@ -72,13 +78,13 @@ private function renderComponent(Console $console, InteractiveComponent $compone
return null;
}

private function applyKey(InteractiveComponent $component, array $validation): mixed
private function applyKey(InteractiveComponent $component, Console $console, array $validation): mixed
{
[$keyBindings, $inputHandlers] = $this->resolveHandlers($component);

while (true) {
usleep(5000);
$key = fread(STDIN, 16);
$key = $console->read(16);

// If there's no keypress, continue
if ($key === '') {
Expand All @@ -92,6 +98,8 @@ private function applyKey(InteractiveComponent $component, array $validation): m
throw new InterruptException();
}

$this->shouldRerender = true;

$return = null;

if ($handlersForKey = $keyBindings[$key] ?? null) {
Expand All @@ -108,7 +116,6 @@ private function applyKey(InteractiveComponent $component, array $validation): m

// If nothing's returned, we can continue waiting for the next key press
if ($return === null) {
$this->shouldRerender = true;
Fiber::suspend();

continue;
Expand All @@ -121,7 +128,6 @@ private function applyKey(InteractiveComponent $component, array $validation): m

// If invalid, we'll remember the validation message and continue
if ($failingRule) {
$this->shouldRerender = true;
$this->validationErrors[] = '<error>' . $failingRule->message() . '</error>';
Fiber::suspend();

Expand Down Expand Up @@ -150,10 +156,10 @@ private function renderFrames(InteractiveComponent $component, Terminal $termina
$frames = $terminal->render(
component: $component,
footerLines: $this->validationErrors,
renderFooter: $this->validationErrors !== [],
);

// Looping over the frames will display them (within Terminal, might need to refactor)
// Looping over the frames will display them
// (this happens within the Terminal class, might need to refactor)
// We suspend between each frame to allow key press interruptions
foreach ($frames as $frame) {
Fiber::suspend();
Expand Down
2 changes: 1 addition & 1 deletion src/ConsoleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function __construct(
/** @var array<array-key, class-string<\Tempest\Console\ConsoleMiddleware>> */
public array $middleware = [
OverviewMiddleware::class,
ResolveOrRescueMiddleware::class,
ConsoleExceptionMiddleware::class,
ResolveOrRescueMiddleware::class,
InvalidCommandMiddleware::class,
HelpMiddleware::class,
],
Expand Down
14 changes: 4 additions & 10 deletions src/Input/MemoryInputBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

final class MemoryInputBuffer implements InputBuffer
{
private ?string $buffer = null;
private array $buffer = [];
private ?Fiber $fiber = null;

public function __construct()
Expand All @@ -21,7 +21,7 @@ public function __construct()
public function add(int|string|Key ...$input): void
{
foreach ($input as $line) {
$this->buffer .= $line instanceof Key
$this->buffer[] = $line instanceof Key
? $line->value
: (string) $line;
}
Expand Down Expand Up @@ -49,14 +49,8 @@ private function consumeBuffer(): string

Fiber::suspend();

$buffer = $this->buffer;
$next = array_shift($this->buffer);

if ($buffer === null) {
throw new Exception('Empty buffer');
}

$this->buffer = null;

return $buffer;
return $next ?? '';
}
}
2 changes: 1 addition & 1 deletion src/Terminal/Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function render(
}

foreach ($rendered as $content) {
if ($renderFooter) {
if ($renderFooter || $footerLines !== []) {
if ($footer = $component->renderFooter()) {
$footerLines = [...$footerLines, $footer];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Components/ConfirmComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public function test_confirm_component(): void

$this->assertFalse($component->enter());

$this->assertTrue($component->getCursorPosition()->equals(new Point(20, 0)));
$this->assertTrue($component->getCursorPosition()->equals(new Point(17, 0)));
}
}

0 comments on commit cf507d3

Please sign in to comment.