Skip to content

Commit 9a4a33f

Browse files
committed
Move type getter into callback classes
1 parent 988b79b commit 9a4a33f

8 files changed

Lines changed: 47 additions & 10 deletions

src/EventLoop/Internal/AbstractDriver.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public function __debugInfo(): array
350350
{
351351
// @codeCoverageIgnoreStart
352352
return \array_map(fn (DriverCallback $callback) => [
353-
'type' => $this->getType($callback->id),
353+
'type' => $callback->getType(),
354354
'enabled' => $callback->enabled,
355355
'referenced' => $callback->referenced,
356356
], $this->callbacks);
@@ -368,13 +368,7 @@ public function getType(string $callbackId): CallbackType
368368
{
369369
$callback = $this->callbacks[$callbackId] ?? throw InvalidCallbackError::invalidIdentifier($callbackId);
370370

371-
return match ($callback::class) {
372-
DeferCallback::class => CallbackType::Defer,
373-
TimerCallback::class => $callback->repeat ? CallbackType::Repeat : CallbackType::Delay,
374-
StreamReadableCallback::class => CallbackType::Readable,
375-
StreamWritableCallback::class => CallbackType::Writable,
376-
SignalCallback::class => CallbackType::Signal,
377-
};
371+
return $callback->getType();
378372
}
379373

380374
#[\Override]
@@ -395,6 +389,8 @@ public function isReferenced(string $callbackId): bool
395389

396390
/**
397391
* Activates (enables) all the given callbacks.
392+
*
393+
* @param array<string, DriverCallback> $callbacks
398394
*/
399395
abstract protected function activate(array $callbacks): void;
400396

src/EventLoop/Internal/DeferCallback.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
namespace Revolt\EventLoop\Internal;
66

7+
use Revolt\EventLoop\CallbackType;
8+
79
/** @internal */
810
final class DeferCallback extends DriverCallback
911
{
12+
#[\Override]
13+
public function getType(): CallbackType
14+
{
15+
return CallbackType::Defer;
16+
}
1017
}

src/EventLoop/Internal/DriverCallback.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Revolt\EventLoop\Internal;
66

7+
use Revolt\EventLoop\CallbackType;
8+
79
/**
810
* @internal
911
*/
@@ -21,6 +23,8 @@ public function __construct(
2123
) {
2224
}
2325

26+
abstract public function getType(): CallbackType;
27+
2428
/**
2529
* @param string $property
2630
*/

src/EventLoop/Internal/SignalCallback.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Revolt\EventLoop\Internal;
66

7+
use Revolt\EventLoop\CallbackType;
8+
79
/** @internal */
810
final class SignalCallback extends DriverCallback
911
{
@@ -14,4 +16,10 @@ public function __construct(
1416
) {
1517
parent::__construct($id, $closure);
1618
}
19+
20+
#[\Override]
21+
public function getType(): CallbackType
22+
{
23+
return CallbackType::Signal;
24+
}
1725
}

src/EventLoop/Internal/StreamCallback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class StreamCallback extends DriverCallback
1313
public function __construct(
1414
string $id,
1515
\Closure $closure,
16-
public readonly mixed $stream
16+
public readonly mixed $stream,
1717
) {
1818
parent::__construct($id, $closure);
1919
}

src/EventLoop/Internal/StreamReadableCallback.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
namespace Revolt\EventLoop\Internal;
66

7+
use Revolt\EventLoop\CallbackType;
8+
79
/** @internal */
810
final class StreamReadableCallback extends StreamCallback
911
{
12+
#[\Override]
13+
public function getType(): CallbackType
14+
{
15+
return CallbackType::Readable;
16+
}
1017
}

src/EventLoop/Internal/StreamWritableCallback.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
namespace Revolt\EventLoop\Internal;
66

7+
use Revolt\EventLoop\CallbackType;
8+
79
/** @internal */
810
final class StreamWritableCallback extends StreamCallback
911
{
12+
#[\Override]
13+
public function getType(): CallbackType
14+
{
15+
return CallbackType::Writable;
16+
}
1017
}

src/EventLoop/Internal/TimerCallback.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Revolt\EventLoop\Internal;
66

7+
use Revolt\EventLoop\CallbackType;
8+
79
/** @internal */
810
final class TimerCallback extends DriverCallback
911
{
@@ -12,8 +14,14 @@ public function __construct(
1214
public readonly float $interval,
1315
\Closure $callback,
1416
public float $expiration,
15-
public readonly bool $repeat = false
17+
public readonly bool $repeat = false,
1618
) {
1719
parent::__construct($id, $callback);
1820
}
21+
22+
#[\Override]
23+
public function getType(): CallbackType
24+
{
25+
return $this->repeat ? CallbackType::Repeat : CallbackType::Delay;
26+
}
1927
}

0 commit comments

Comments
 (0)