Class RegisteringEventEmitter

java.lang.Object
com.brightcove.player.event.RegisteringEventEmitter
All Implemented Interfaces:
EventEmitter

public class RegisteringEventEmitter extends 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

See Also:
  • Constructor Details

  • Method Details

    • getAllowedEmittedEvents

      public List<String> getAllowedEmittedEvents()
      Retrieves a list of all Events that are allowed to be emitted in this RegisteringEventEmitter.
      Returns:
      a List of EventType strings for all allowed emits
    • getAllowedListenEvents

      public List<String> getAllowedListenEvents()
      Retrieves a list of all Events that are allowed to be listened in this RegisteringEventEmitter.
      Returns:
      a List of EventType strings for all allowed listened events
    • on

      public int on(String eventType, EventListener listener)
      Add a listener for an EventType, filtered by what the component is allowed to listen for. The response token should be cached by component for use later in the component lifecycle
      Specified by:
      on in interface EventEmitter
      Parameters:
      eventType -
      listener -
      Returns:
      an empty string if listener was not attached, a UUID in string form if was successfully attached
      See Also:
    • once

      public int once(String eventType, EventListener listener)
      Add a listener for an EventType, filtered by what the component is allowed to listen for. This listener will be handled only once, at which point it is removed. The response token should be cached by component for use later in the component lifecycle
      Specified by:
      once in interface EventEmitter
      Parameters:
      eventType -
      listener -
      Returns:
      an empty string if listener was not attached, a UUID in string form if was successfully attached
      See Also:
    • off

      public void off()
      Remove all listeners.
      Specified by:
      off in interface EventEmitter
    • off

      public void off(String eventType, int token)
      Remove a listener from handling an event of EventType. Requires a token given by on() or once()
      Specified by:
      off in interface EventEmitter
      Parameters:
      eventType -
      token -
    • emit

      public void emit(String eventType)
      Emits an Event of EventType. However, the Event will only be emitted if the component is allowed to do so
      Specified by:
      emit in interface EventEmitter
      Parameters:
      eventType -
    • emit

      public void emit(String eventType, Map<String,Object> properties)
      Emits and Event of EventType, provided the Component is allowed to do so. Will also pass along a set of String properties.
      Specified by:
      emit in interface EventEmitter
      Parameters:
      eventType -
      properties -
    • emitNow

      public void emitNow(String eventType, Map<String,Object> properties)
      Description copied from interface: EventEmitter
      Emits an event immediately instead of waiting for the handler.
      Specified by:
      emitNow in interface EventEmitter
      Parameters:
      eventType - The event type.
      properties - The properties to send when processing the event.
    • getRootEmitter

      public EventEmitter getRootEmitter()
      Retrieve the underlying EventEmitter provided when initializing this emitter.
      Returns:
      the root EventEmitter
    • build

      public static RegisteringEventEmitter build(EventEmitter incomingEmitter, Class<? extends Component> componentClass)
      Factory method to create a new RegisteringEventEmitter given an existing EventEmitter and a Component implementation. All RegisteringEventEmitters should be created with this method.
      Parameters:
      incomingEmitter - an existing EventEmitter, could also be a RegisteringEventEmitter
      componentClass - the Component implementation to be managed by this emitter
      Returns:
      a properly initialized RegisteringEventEmitter
    • request

      public void request(String eventType, EventListener listener)
      Requests a response from the first listener to respond. The response is handled by the provided listener, after which the listener is removed.
      Specified by:
      request in interface EventEmitter
      Parameters:
      eventType - The type of Event to emit and expect a response from
      listener - A listener to process the response. After processing, the response should be removed
      See Also:
    • request

      public void request(String eventType, Map<String,Object> properties, EventListener listener)
      Requests a response from the first listener to respond. The response is handled by the provided listener, after which the listener is removed. The provided properties map is passed on to the responding listener
      Specified by:
      request in interface EventEmitter
      Parameters:
      eventType - The type of Event to emit and expect a response from
      properties - A Map of properties to pass along the event chain
      listener - A listener to process the response. After processing, the response should be removed
      See Also:
    • respond

      public void respond(Map<String,Object> properties)
      Respond to a request. Should be placed within an EventListener and via some means (e.g. the token of an InvocationContainer) know which Request Listener to respond to. See
      Specified by:
      respond in interface EventEmitter
      Parameters:
      properties -
    • respond

      public void respond(Event event)
      Respond to a request. Should be called within an EventListener and given an Event object with the appropriate REQUEST_TOKEN value
      Specified by:
      respond in interface EventEmitter
      Parameters:
      event - an Event object with, at minimum, the necessary REQUEST_TOKEN value
    • enable

      public void enable()
      Enables listener registration and event emitting.
      Specified by:
      enable in interface EventEmitter
    • disable

      public void disable()
      Disables listener registration and event emitting.
      Specified by:
      disable in interface EventEmitter
    • convertEventsFromAnnotation

      protected List<String> convertEventsFromAnnotation(Class<? extends Component> componentClass, Class<? extends 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.
      Parameters:
      componentClass - The component's class.
      annotationClass - The annotation's class.
      Returns:
      A list of event names.