This PHP library provide an immutable implementation of the PSR-14 for ReactiveX PHP.
composer require th3mouk/reactive-event-dispatcher
Psalm usage is recommended. See relative introduction and documentation.
use Psr\Container\ContainerInterface;
use Rx\Observable;
use Th3Mouk\ReactiveEventDispatcher\Dispatcher;
use Th3Mouk\ReactiveEventDispatcher\Event;
use Th3Mouk\ReactiveEventDispatcher\EventCorrelation;
use Th3Mouk\ReactiveEventDispatcher\Listener;
use Th3Mouk\ReactiveEventDispatcher\ListenerProvider;
use Th3Mouk\ReactiveEventDispatcher\Priority;
$event = new class implements Event {};
$listener = new class implements Listener {
public function process (Event $event) : Observable {
return Observable::of(1);
}
};
// Link between an event and a listener
// Higher is the priority, earlier is the call
$event_correlations = [
EventCorrelation::create(
get_class($event),
get_class($listener),
Priority::fromInt(0),
)
];
// Any object implementing ContainerInterface
// Listeners must be present into
$locator = new class implements ContainerInterface{
public function get($id){
}
public function has($id){
}
};
$listener_provider = new ListenerProvider($locator, $event_correlations);
$dispatcher = new Dispatcher($listener_provider);
$dispatcher->dispatch($event)->subscribe();
Feel free to improve this library.