public class RegisteringEventEmitter extends java.lang.Object implements EventEmitter
The RegisteringEventEmitter allows developers to constrain which events their Components are intended to listen for and emit. If a component attempts to emit or listen for an event that it hasn't explicitly listed, an exception will be thrown.
Registration occurs by creating a new instance of RegisteringEventEmitter, using a shared EventEmitter (typically EventEmitterImpl), and the class to be registered. The class in question MUST be decorated with two annotations: ListensFor and Emits. Each takes a String[], populated with EventType strings, attached to their 'events' property. Each annotation MUST be included, even if the component has no intention of listening or emitting any events.
For example, the following component class declartion:
@Emits(events = {EventType.PLAY, EventType.STOP})
@ListensFor(events = {EventType.PLAY})
public class SampleComponent implements Component
EventEmitter
,
EventEmitterImpl
,
Component
,
Emits
,
ListensFor
Constructor and Description |
---|
RegisteringEventEmitter(EventEmitter emitter,
java.lang.Class<? extends Component> component)
Main constructor.
|
Modifier and Type | Method and Description |
---|---|
static RegisteringEventEmitter |
build(EventEmitter incomingEmitter,
java.lang.Class<? extends Component> componentClass)
Factory method to create a new RegisteringEventEmitter given an existing EventEmitter and
a Component implementation.
|
protected java.util.List<java.lang.String> |
convertEventsFromAnnotation(java.lang.Class<? extends Component> componentClass,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Convenience method on BaseComponent which converts the eventTypes in the ListensFor or Emits annotations to
a List of event names for ease of use.
|
void |
disable()
Disables listener registration and event emitting.
|
void |
emit(java.lang.String eventType)
Emits an Event of EventType.
|
void |
emit(java.lang.String eventType,
java.util.Map<java.lang.String,java.lang.Object> properties)
Emits and Event of EventType, provided the Component is allowed to do so.
|
void |
enable()
Enables listener registration and event emitting.
|
java.util.List<java.lang.String> |
getAllowedEmittedEvents()
Retrieves a list of all Events that are allowed to be emitted in this
RegisteringEventEmitter.
|
java.util.List<java.lang.String> |
getAllowedListenEvents()
Retrieves a list of all Events that are allowed to be listened in this
RegisteringEventEmitter.
|
EventEmitter |
getRootEmitter()
Retrieve the underlying EventEmitter provided when initializing this emitter.
|
void |
off()
Remove all listeners.
|
void |
off(java.lang.String eventType,
int token)
Remove a listener from handling an event of EventType.
|
int |
on(java.lang.String eventType,
EventListener listener)
Add a listener for an EventType, filtered by what the component is allowed to listen for.
|
int |
once(java.lang.String eventType,
EventListener listener)
Add a listener for an EventType, filtered by what the component is allowed to listen for.
|
void |
request(java.lang.String eventType,
EventListener listener)
Requests a response from the first listener to respond.
|
void |
request(java.lang.String eventType,
java.util.Map<java.lang.String,java.lang.Object> properties,
EventListener listener)
Requests a response from the first listener to respond.
|
void |
respond(Event event)
Respond to a request.
|
void |
respond(java.util.Map<java.lang.String,java.lang.Object> properties)
Respond to a request.
|
public RegisteringEventEmitter(EventEmitter emitter, java.lang.Class<? extends Component> component) throws java.lang.IllegalArgumentException
emitter
- component
- java.lang.IllegalArgumentException
public java.util.List<java.lang.String> getAllowedEmittedEvents()
public java.util.List<java.lang.String> getAllowedListenEvents()
public int on(java.lang.String eventType, EventListener listener)
on
in interface EventEmitter
eventType
- listener
- EventListener
public int once(java.lang.String eventType, EventListener listener)
once
in interface EventEmitter
eventType
- listener
- EventListener
public void off()
off
in interface EventEmitter
public void off(java.lang.String eventType, int token)
off
in interface EventEmitter
eventType
- token
- public void emit(java.lang.String eventType)
emit
in interface EventEmitter
eventType
- public void emit(java.lang.String eventType, java.util.Map<java.lang.String,java.lang.Object> properties)
emit
in interface EventEmitter
eventType
- properties
- public EventEmitter getRootEmitter()
public static RegisteringEventEmitter build(EventEmitter incomingEmitter, java.lang.Class<? extends Component> componentClass)
incomingEmitter
- an existing EventEmitter, could also be a RegisteringEventEmittercomponentClass
- the Component implementation to be managed by this emitterpublic void request(java.lang.String eventType, EventListener listener)
request
in interface EventEmitter
eventType
- The type of Event to emit and expect a response fromlistener
- A listener to process the response. After processing, the response should be removedEventListener
,
EventType
public void request(java.lang.String eventType, java.util.Map<java.lang.String,java.lang.Object> properties, EventListener listener)
request
in interface EventEmitter
eventType
- The type of Event to emit and expect a response fromproperties
- A Map of properties to pass along the event chainlistener
- A listener to process the response. After processing, the response should be removedEventListener
,
EventType
public void respond(java.util.Map<java.lang.String,java.lang.Object> properties)
respond
in interface EventEmitter
properties
- public void respond(Event event)
respond
in interface EventEmitter
event
- an Event object with, at minimum, the necessary REQUEST_TOKEN valuepublic void enable()
enable
in interface EventEmitter
public void disable()
disable
in interface EventEmitter
protected java.util.List<java.lang.String> convertEventsFromAnnotation(java.lang.Class<? extends Component> componentClass, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
componentClass
- The component's class.annotationClass
- The annotation's class.