more tests
Some checks failed
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Has started running
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Has started running
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Has started running
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Has been cancelled
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Has started running
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Has started running

This commit is contained in:
2025-11-14 20:41:35 -05:00
parent 474134c654
commit 20a4f51402
11 changed files with 709 additions and 28 deletions

View File

@@ -9,6 +9,9 @@ use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Siteworxpro\App\Attributes\Events\ListensFor;
use function React\Async\await;
use function React\Async\coroutine;
/**
* Class Dispatcher
*
@@ -29,6 +32,8 @@ class Dispatcher implements DispatcherContract, Arrayable
*/
private Collection $pushed;
private array $subscribers = [];
/**
* @var string LISTENERS_NAMESPACE The namespace where listeners are located
*/
@@ -99,7 +104,7 @@ class Dispatcher implements DispatcherContract, Arrayable
*/
public function subscribe($subscriber): void
{
$this->listeners = array_merge($this->listeners, (array) $subscriber);
$this->subscribers[] = $subscriber;
}
/**
@@ -108,6 +113,7 @@ class Dispatcher implements DispatcherContract, Arrayable
* @param $event
* @param array $payload
* @return array|null
* @throws \Throwable
*/
public function until($event, $payload = []): array|null
{
@@ -121,6 +127,7 @@ class Dispatcher implements DispatcherContract, Arrayable
* @param array $payload
* @param bool $halt
* @return array|null
* @throws \Throwable
*/
public function dispatch($event, $payload = [], $halt = false): array|null
{
@@ -130,23 +137,46 @@ class Dispatcher implements DispatcherContract, Arrayable
$eventClass = $event;
}
// Handle subscribers as a coroutine
$promise = coroutine(function () use ($event, $payload, $halt, $eventClass, &$responses) {
foreach ($this->subscribers as $subscriber) {
if (method_exists($subscriber, 'handle')) {
$response = $subscriber->handle($event, $payload);
$responses[$eventClass] = $response;
if ($halt && $response !== null) {
return $responses;
}
}
}
return null;
});
$listeners = $this->listeners[$eventClass] ?? null;
// If no listeners, just await the subscriber promise
if ($listeners === null) {
return null;
return await($promise);
}
$responses = [];
foreach ($listeners as $listener) {
$response = $listener($event, $payload);
$responses[] = $response;
$responses[$eventClass] = $response;
if ($halt && $response !== null) {
return $response;
}
}
// Await the subscriber promise and merge responses
$promiseResponses = await($promise);
if (is_array($promiseResponses)) {
$responses = array_merge($responses, $promiseResponses);
}
return $responses;
}
@@ -167,6 +197,7 @@ class Dispatcher implements DispatcherContract, Arrayable
*
* @param $event
* @return void
* @throws \Throwable
*/
public function flush($event): void
{

View File

@@ -18,12 +18,15 @@ use Siteworxpro\App\Services\Facades\Logger;
class Connected extends Listener
{
/**
* @param ConnectionEvent $event
* @param mixed $event
* @param array $payload
* @return null
*/
public function __invoke($event, array $payload = []): null
public function __invoke(mixed $event, array $payload = []): null
{
if (!($event instanceof ConnectionEvent)) {
throw new \TypeError("Invalid event type passed to listener " . static::class);
}
Logger::info("Database connection event", [get_class($event), $event->connectionName]);