Skip to content

Commit

Permalink
[EventDispatcher] initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed May 9, 2020
1 parent 1fee965 commit 79855d1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Nuxed/Contract/EventDispatcher/Event/IEvent.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Nuxed\EventDispatcher\Event;

/**
* Marker interface indicating an event instance.
*
* Event instances may contain zero methods, or as many methods as they
* want. The interface MUST be implemented, however, to provide type-safety
* to both listeners as well as the dispatcher.
*/
interface IEvent {}
22 changes: 22 additions & 0 deletions src/Nuxed/Contract/EventDispatcher/Event/IStoppableEvent.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Nuxed\EventDispatcher\Event;

/**
* An Event whose processing may be interrupted when the event has been handled.
*
* A Dispatcher implementation MUST check to determine an Event
* is marked as stopped after each listener is called. If it is then it should
* return immediately without calling any further Listeners.
*/
interface IStoppableEvent extends IEvent {
/**
* Is propagation stopped?
*
* This will typically only be used by the Dispatcher to determine if the
* previous listener halted propagation.
*
* @return bool
* True if the Event is complete and no further listeners should be called.
* False to continue calling listeners.
*/
public function isPropagationStopped(): bool;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Nuxed\EventDispatcher\EventListener;

use namespace Nuxed\EventDispatcher\Event;

/**
* Defines a listener for an event.
*/
interface IEventListener<T as Event\IEvent> {
/**
* Process the given event.
*/
public function process(T $event): Awaitable<void>;
}
15 changes: 15 additions & 0 deletions src/Nuxed/Contract/EventDispatcher/IEventDispatcher.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Nuxed\EventDispatcher;

/**
* Defines a dispatcher for events.
*/
interface IEventDispatcher {
/**
* Provide all relevant listeners with an event to process.
*
* @template T as IEvent
*
* @return T The Event that was passed, now modified by listeners.
*/
public function dispatch<T as Event\IEvent>(T $event): Awaitable<T>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Nuxed\EventDispatcher\ListenerProvider;

use namespace Nuxed\EventDispatcher\{Event, EventListener};

/**
* Mapper from an event to the listeners that are applicable to that event.
*/
interface IListenerProvider {
/**
* Retrieve all listeners for the given event.
*
* @param T $event
* An event for which to return the relevant listeners.
* @return AsyncIterator<EventListener\IEventListener<T>>
* An async iterator (usually an async generator) of listeners. Each
* listener MUST be type-compatible with $event.
*/
public function getListeners<T as Event\IEvent>(
T $event,
): AsyncIterator<EventListener\IEventListener<T>>;
}

0 comments on commit 79855d1

Please sign in to comment.