th3mouk/reactive-event-dispatcher

An immutable implementation of the PSR-14 (event-dispatcher) for ReactiveX PHP.

0.1.0 2021-10-28 20:39 UTC

This package is auto-updated.

Last update: 2024-10-29 03:09:59 UTC


README

This PHP library provide an immutable implementation of the PSR-14 for ReactiveX PHP.

Latest Stable Version Latest Unstable Version Total Downloads License

Coverage Status Scrutinizer Code Quality

Installation

composer require th3mouk/reactive-event-dispatcher

Usage

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();

Please

Feel free to improve this library.