-
-
Notifications
You must be signed in to change notification settings - Fork 1
Events
Greg Bowler edited this page May 4, 2026
·
1 revision
Alongside the loop and timers, this package includes a small in-memory event system.
There are only two classes involved:
-
Event, which carries an event name and optional detail data -
EventDispatcher, which stores subscribers by event name and publishes events to them
use GT\Async\Event\Event;
use GT\Async\Event\EventDispatcher;
$dispatcher = new EventDispatcher();
$dispatcher->subscribe("tick", function(Event $event) {
echo "Received: ", $event->getName(), PHP_EOL;
});$dispatcher->publish(new Event("tick"));That will invoke every callback subscribed to "tick", in the same order they were added.
The Event object can carry any detail value:
$dispatcher->subscribe("message", function(Event $event) {
echo $event->getDetail(), PHP_EOL;
});
$dispatcher->publish(new Event("message", "Hello"));This is a deliberately small utility. It does not include:
- wildcard subscriptions
- unsubscribe methods
- cross-process or network delivery
If you need those features, they would need to be built on top.
Finally, take a look at Examples for the bundled scripts in this repository.