You've already forked Php-Template
more tests (#19)
All checks were successful
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 2m32s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 2m48s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 2m33s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 2m44s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 2m53s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 3m5s
All checks were successful
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 2m32s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 2m48s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 2m33s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 2m44s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 2m53s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 3m5s
Reviewed-on: #19 Co-authored-by: Ron Rise <ron@siteworxpro.com> Co-committed-by: Ron Rise <ron@siteworxpro.com>
This commit was merged in pull request #19.
This commit is contained in:
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user