Events
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 dataEventDispatcher, which stores subscribers by event name and publishes events to them
Subscribing to an event
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;
});
Publishing an event
$dispatcher->publish(new Event("tick"));
That will invoke every callback subscribed to "tick", in the same order they were added.
Passing detail data
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"));
What this dispatcher does not do
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.