Git Product home page Git Product logo

pinoven-event-dispatcher's Introduction

Pinoven: Event Dispatcher Pinoven

PSR-14: Event Dispatcher compatible. Implement event dispatcher to dispatch events. It provides a way to register listeners and subscribe/unsubscribe many listeners providers. Dispatcher can retrieve listeners through different providers by using an aggregator.

Features/Usage

Dispatcher

    use Pinoven\Dispatcher\Dispatch\EventDispatcher;
    use Pinoven\Dispatcher\Provider\AggregatorProvider;
    use Pinoven\Dispatcher\Samples\EventSampleA;
 
    $aggregator = new AggregatorProvider();
    $eventDispatcher =  new EventDispatcher($aggregator);
    $event = new EventSampleA();
    $eventDispatcher->dispatch($event);

Aggregator, Delegate, Mapper Setup

    use Fig\EventDispatcher\DelegatingProvider as FigDelegatingProvider;
    use Pinoven\Dispatcher\Listener\ProxyListeners;
    use Pinoven\Dispatcher\Provider\AggregatorProvider;
    use Pinoven\Dispatcher\Provider\DelegatingProvider;
    use Pinoven\Dispatcher\Samples\EventMapperProviderSample;
    use Pinoven\Dispatcher\Samples\EventMapperProviderSampleB;
    use Pinoven\Dispatcher\Samples\EventMapperProviderSampleC;
    use Pinoven\Dispatcher\Samples\EventSampleA;
 
    $aggregator = new AggregatorProvider();
    $proxy = new ProxyListeners();
    $defaultListenerProvider = new FigDelegatingProvider();
    $eventMapper1 = new  EventMapperProviderSample($proxy);
    $eventMapper2 = new  EventMapperProviderSampleB($proxy);
    $eventMapper3 = new  EventMapperProviderSampleC($proxy);
    $defaultListenerProvider->addProvider($eventMapper3, [EventSampleA::class]);
    $delegateListenerProvider1 = new DelegatingProvider();
    $delegateListenerProvider1->subscribe($eventMapper1);
    $delegateListenerProvider1->subscribe($eventMapper2);
    // \Pinoven\Dispatcher\Provider\DelegatingType() Can take a default listenerProvider.
    $delegateListenerProvider2 = new DelegatingProvider($defaultListenerProvider);
    $aggregator->addProvider($delegateListenerProvider1);
    $aggregator->addProvider($delegateListenerProvider2);

Mapper for Event/Listeners

use Pinoven\Dispatcher\Event\EventListenersMapper;
EventListenersMapper::class;

It helps to declare the event type and related listeners.

use Pinoven\Dispatcher\Event\EventListenersMapper;
EventListenersMapper::class;

Emit Event

use Pinoven\Dispatcher\Event\EventEmitter;
EventEmitter::class;

Emitter event permit to use the dispatcher and send a custom event too by using string and payload.

Order/Prioritize

Order items

To manage sorting on providers, listeners you have to implement:

use Pinoven\Dispatcher\Priority\PrioritizeInterface;
class Prioritize implements PrioritizeInterface 
{
    public function sortItems(iterable $items) : iterable
    {
        // Code ...
         return  $items;
    }
}

See the implementation

use Pinoven\Dispatcher\Priority\Prioritize;
$prioritize = new Prioritize();

Handle Priority

If the listener is a class then it should implement:

use Pinoven\Dispatcher\Priority\ItemPriorityInterface;
ItemPriorityInterface::class;

If your listener/callable cannot directly implement this interface you can wrap by using:

use Pinoven\Dispatcher\Priority\CallableInterface;
CallableInterface::class;

You may need to do some stuff on the wrapper, so you can use the factory to transformer your listener/callable to wrapper item.

use Pinoven\Dispatcher\Priority\CallableItemPriorityInterface;
CallableItemPriorityInterface::class;

Todo

  • merge payload if public $payload or add data to existing payload
  • Automatic event hierarchy. It means by dealing with BeforeEvent, AfterEvent. Perhaps BetweenEvent.??
  • Implement Logger
  • Implement Container
  • Implement CacheInterface
  • Attach/detach Listener ?
  • Implement own Collection/Generator
  • Clean Sample

Contribution

  • Create issue: improvement + the reason why it should be implemented or issue + how to reproduce.
  • Create pull request and explain the issue.

More information will come about how to contribute on all pinoven package.

pinoven-event-dispatcher's People

Contributors

rbergdrox avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.