Skip to content

Commit

Permalink
Fix linting warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Nov 6, 2024
1 parent a13ea4a commit bd96777
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
.vscode/
*.swp
*.swo
.DS_Store
.DS_Store
.php-cs-fixer.cache
11 changes: 6 additions & 5 deletions examples/basic-usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require __DIR__ . '/../vendor/autoload.php';

use ArchiPro\EventDispatcher\AsyncEventDispatcher;
use ArchiPro\EventDispatcher\ListenerProvider;
use ArchiPro\EventDispatcher\Event\AbstractEvent;
use ArchiPro\EventDispatcher\ListenerProvider;
use Revolt\EventLoop;

// Create a custom event
Expand All @@ -13,21 +13,22 @@ class UserCreatedEvent extends AbstractEvent
public function __construct(
public readonly string $userId,
public readonly string $email
) {}
) {
}
}

// Create the listener provider and register listeners
$listenerProvider = new ListenerProvider();

$listenerProvider->addListener(UserCreatedEvent::class, function (UserCreatedEvent $event) {
// Simulate async operation
EventLoop::delay(1, fn() => null);
EventLoop::delay(1, fn () => null);
echo "Sending welcome email to {$event->email}\n";
});

$listenerProvider->addListener(UserCreatedEvent::class, function (UserCreatedEvent $event) {
// Simulate async operation
EventLoop::delay(0.5, fn() => null);
EventLoop::delay(0.5, fn () => null);
echo "Logging user creation: {$event->userId}\n";
});

Expand All @@ -39,4 +40,4 @@ public function __construct(
$dispatcher->dispatch($event);

// Run the event loop
EventLoop::run();
EventLoop::run();
19 changes: 9 additions & 10 deletions src/AsyncEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace ArchiPro\EventDispatcher;

use function Amp\async;

use Amp\Pipeline\Queue;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
use Psr\EventDispatcher\StoppableEventInterface;
use Revolt\EventLoop;
use Amp\Pipeline\Queue;

use function Amp\async;
use Revolt\EventLoop;

/**
* Asynchronous implementation of PSR-14 EventDispatcherInterface using Revolt and AMPHP.
Expand Down Expand Up @@ -49,7 +50,7 @@ public function dispatch(object $event): object
if ($event instanceof StoppableEventInterface) {
return $this->dispatchStoppableEvent($event, $listeners);
}

return $this->dispatchNonStoppableEvent($event, $listeners);
}

Expand All @@ -63,7 +64,7 @@ public function dispatch(object $event): object
*/
private function dispatchStoppableEvent(StoppableEventInterface $event, iterable $listeners): StoppableEventInterface
{
async(function() use ($event, $listeners): void {
async(function () use ($event, $listeners): void {
foreach ($listeners as $listener) {
$listener($event);
if ($event->isPropagationStopped()) {
Expand All @@ -78,7 +79,7 @@ private function dispatchStoppableEvent(StoppableEventInterface $event, iterable
/**
* Dispatches a non-stoppable event to listeners asynchronously.
* Simply queues all listeners in the event loop.
*
*
* Because we don't need to worry about stopping propagation, we can simply
* queue all listeners in the event loop and let them run whenever in any order.
*
Expand All @@ -89,14 +90,12 @@ private function dispatchStoppableEvent(StoppableEventInterface $event, iterable
private function dispatchNonStoppableEvent(object $event, iterable $listeners): object
{
foreach ($listeners as $listener) {
EventLoop::queue(function() use ($event, $listener) {
EventLoop::queue(function () use ($event, $listener) {
$listener($event);
});
}

return $event;
}



}
}
2 changes: 1 addition & 1 deletion src/Event/AbstractEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ public function stopPropagation(): void
{
$this->propagationStopped = true;
}
}
}
2 changes: 1 addition & 1 deletion src/ListenerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ public function getListenersForEvent(object $event): iterable
{
return $this->listeners[$event::class] ?? [];
}
}
}
19 changes: 9 additions & 10 deletions tests/AsyncEventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace ArchiPro\EventDispatcher\Tests;

use PHPUnit\Framework\TestCase;
use ArchiPro\EventDispatcher\AsyncEventDispatcher;
use ArchiPro\EventDispatcher\ListenerProvider;
use ArchiPro\EventDispatcher\Tests\Fixture\TestEvent;
use PHPUnit\Framework\TestCase;
use Revolt\EventLoop;

/**
Expand All @@ -17,7 +17,7 @@
* - Multiple listener execution
* - Event propagation stopping
* - Handling events with no listeners
*
*
* @covers \ArchiPro\EventDispatcher\AsyncEventDispatcher
*/
class AsyncEventDispatcherTest extends TestCase
Expand Down Expand Up @@ -46,21 +46,21 @@ public function testDispatchEventToMultipleListeners(): void
{
$results = [];
$completed = false;

$this->listenerProvider->addListener(TestEvent::class, function (TestEvent $event) use (&$results) {
EventLoop::delay(0.1, fn() => null);
EventLoop::delay(0.1, fn () => null);
$results[] = 'listener1: ' . $event->data;
});

$this->listenerProvider->addListener(TestEvent::class, function (TestEvent $event) use (&$results, &$completed) {
EventLoop::delay(0.05, fn() => null);
EventLoop::delay(0.05, fn () => null);
$results[] = 'listener2: ' . $event->data;
$completed = true;
});

$event = new TestEvent('test data');
$this->dispatcher->dispatch($event);

// Verify immediate return
$this->assertEmpty($results);

Expand All @@ -79,7 +79,7 @@ public function testDispatchEventToMultipleListeners(): void
public function testSynchronousStoppableEvent(): void
{
$results = [];

$this->listenerProvider->addListener(TestEvent::class, function (TestEvent $event) use (&$results) {
$results[] = 'listener1';
$event->stopPropagation();
Expand All @@ -97,7 +97,6 @@ public function testSynchronousStoppableEvent(): void
$this->assertEquals(['listener1'], $results);
}


/**
* Tests handling of events with no registered listeners.
*/
Expand All @@ -114,7 +113,7 @@ public function testNoListenersForEvent(): void
*/
public function testDispatchesNonStoppableEvents(): void
{
$event = new class() {
$event = new class () {
public bool $called = false;
};

Expand All @@ -132,4 +131,4 @@ public function testDispatchesNonStoppableEvents(): void
$this->assertTrue($event->called, 'Listener should have been called for non-stoppable event');
}

}
}
5 changes: 3 additions & 2 deletions tests/Fixture/TestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ class TestEvent extends AbstractEvent
*/
public function __construct(
public readonly string $data
) {}
}
) {
}
}

0 comments on commit bd96777

Please sign in to comment.