Skip to content

Commit 32627b1

Browse files
author
Xavier Barbosa
committed
Replace while loop with sleep with react event loop.
1 parent 4fbd5a1 commit 32627b1

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/Console/Debug.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Madewithlove\LaravelDebugConsole\Renderers\Route;
1212
use Madewithlove\LaravelDebugConsole\Renderers\Timeline;
1313
use Madewithlove\LaravelDebugConsole\StorageRepository;
14+
use React\EventLoop\Factory;
1415

1516
class Debug extends Command
1617
{
@@ -38,6 +39,11 @@ class Debug extends Command
3839
*/
3940
private $currentRequest = null;
4041

42+
/**
43+
* @var \React\EventLoop\LoopInterface
44+
*/
45+
private $loop;
46+
4147
/**
4248
* @param \Madewithlove\LaravelDebugConsole\StorageRepository $repository
4349
*/
@@ -46,6 +52,7 @@ public function __construct(StorageRepository $repository)
4652
parent::__construct();
4753

4854
$this->repository = $repository;
55+
$this->loop = Factory::create();
4956
}
5057

5158
/**
@@ -54,20 +61,16 @@ public function __construct(StorageRepository $repository)
5461
public function handle()
5562
{
5663
$section = $this->argument('section');
57-
58-
// Watch files every second
59-
while (true) {
64+
$this->loop->addPeriodicTimer(1, function () use ($section) {
6065
$data = $this->repository->latest();
6166

6267
// Checks if its a new request
63-
if ($this->isNewRequest(array_get($data, '__meta.id'))) {
64-
$this->refresh();
65-
} else {
66-
$this->wait();
67-
68-
continue;
68+
if (!$this->isNewRequest($data)) {
69+
return;
6970
}
7071

72+
$this->refresh();
73+
7174
(new General($this->input, $this->output))->render($data);
7275

7376
switch ($section) {
@@ -90,28 +93,25 @@ public function handle()
9093
(new Request($this->input, $this->output))->render($data);
9194
break;
9295
}
93-
}
94-
}
96+
});
9597

96-
private function wait()
97-
{
98-
sleep(1);
98+
$this->loop->run();
9999
}
100100

101101
private function refresh()
102102
{
103-
$this->wait();
104103
system('clear');
105104
}
106105

107106
/**
108-
* @param $id
107+
* @param array $data
109108
*
110109
* @return bool
111110
*/
112-
private function isNewRequest($id)
111+
private function isNewRequest(array $data)
113112
{
114-
if (empty($this->currentRequest) || $this->currentRequest !== $id) {
113+
$id = array_get($data, '__meta.id');
114+
if ($id && empty($this->currentRequest) || $this->currentRequest !== $id) {
115115
$this->currentRequest = $id;
116116

117117
return true;

0 commit comments

Comments
 (0)