Skip to content

Commit 51da8ec

Browse files
committedJul 3, 2023
Finished the command HMR system
1 parent edb203c commit 51da8ec

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed
 

‎Bootstrap/Commands.php

+1-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Core\Commands\QueuedCommand;
66
use Core\Disabled;
77
use Core\HMR\HotCommand;
8-
use React\EventLoop\Loop;
98

109
use function Core\discord;
1110
use function Core\doesClassHaveAttribute;
@@ -31,18 +30,10 @@
3130
foreach ($commands as $name => &$command) {
3231
$file = BOT_ROOT . '\\' . $command[0]->handler::class . '.php';
3332

34-
$command = new HotCommand(
33+
new HotCommand(
3534
$name,
3635
$command[1],
3736
$file
3837
);
3938
}
40-
41-
Loop::addTimer(1, static function () use ($commands) {
42-
foreach ($commands as $command) {
43-
$command->on(HotCommand::EVENT_HAS_CHANGED, static function (HotCommand $command) {
44-
$command->reload();
45-
});
46-
}
47-
});
4839
})->otherwise(static fn (Throwable $e) => $discord->getLogger()->error($e->getMessage()));

‎Core/HMR/HotCommand.php

+7-14
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,27 @@ class HotCommand extends EventEmitter
1111
{
1212
public const EVENT_RELOAD = 'reload';
1313

14-
public const EVENT_HAS_CHANGED = 'hasChanged';
15-
16-
protected int $lastModified;
17-
1814
protected ?HotCache $cachedScript = null;
1915

16+
protected HotFile $file;
17+
2018
public function __construct(
2119
public readonly string $name,
2220
protected readonly RegisteredCommand $command,
23-
public readonly string $file
21+
public readonly string $filePath
2422
) {
25-
$this->lastModified = filemtime($file);
23+
$this->file = new HotFile($filePath);
2624

27-
$this->on(self::EVENT_HAS_CHANGED, static function (self $command) {
28-
$command->reload();
25+
$this->file->on(HotFile::EVENT_HAS_CHANGED, function () {
26+
$this->reload();
2927
});
3028
}
3129

32-
public function loadContents(): string
33-
{
34-
return file_get_contents($this->file);
35-
}
36-
3730
protected function createCachedScript(): void
3831
{
3932
$className = GeneratorUtils::uuid(8, range('a', 'z'));
4033
$temp = BOT_ROOT . '/Core/HMR/' . $className . '.php';
41-
$contents = preg_replace('/class\s+([a-zA-Z0-9_]+)/', 'class ' . $className, $this->loadContents());
34+
$contents = preg_replace('/class\s+([a-zA-Z0-9_]+)/', 'class ' . $className, $this->file->getContents());
4235
$contents = preg_replace('/namespace\s+([a-zA-Z0-9_\\\\]+)/', 'namespace Core\\HMR\\Cached', $contents);
4336

4437
$this->cachedScript?->deleteCachedFile();

‎Core/HMR/HotFile.php

+23-6
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,43 @@
55
use Evenement\EventEmitter;
66
use React\EventLoop\Loop;
77

8+
use function Core\discord;
9+
810
class HotFile extends EventEmitter
911
{
1012
public const EVENT_HAS_CHANGED = 'hasChanged';
1113

12-
protected int $lastModified;
14+
protected string $contents = '';
1315

1416
public function __construct(
1517
public readonly string $file
1618
) {
17-
$this->lastModified = filemtime($file);
19+
if (!file_exists($file)) {
20+
discord()->getLogger()->error("File {$file} does not exist");
21+
22+
return;
23+
}
1824

19-
Loop::addTimer(1, function () {
20-
if ($this->hasChanged()) {
21-
$this->emit(self::EVENT_HAS_CHANGED, [$this]);
25+
$this->contents = $this->getContents();
26+
27+
Loop::addPeriodicTimer(1, function () {
28+
if (!$this->hasChanged()) {
29+
return;
2230
}
31+
32+
discord()->getLogger()->debug("File {$this->file} has changed");
33+
$this->contents = $this->getContents();
34+
$this->emit(self::EVENT_HAS_CHANGED, [$this]);
2335
});
2436
}
2537

38+
public function getContents(): string
39+
{
40+
return file_get_contents($this->file);
41+
}
42+
2643
public function hasChanged(): bool
2744
{
28-
return $this->lastModified !== filemtime($this->file);
45+
return $this->contents !== $this->getContents();
2946
}
3047
}

0 commit comments

Comments
 (0)
Please sign in to comment.