abstract struct Athena::EventDispatcher::Callable

Overview

Encapsulates everything required to represent an event listener. Including what event is being listened on, the callback itself, and its priority.

Each subclass represents a specific "type" of listener. See each subclass for more information.

TIP: These types can be manually instantiated and added via the related AED::EventDispatcherInterface#listener(callable) overload. This can be useful as a point of integration to other libraries, such as lazily instantiating listener instances.

Name

Each callable also has an optional name that can be useful for debugging to allow identifying a specific callable since there would be no way to tell apart two listeners on the same event, with the same priority.

class MyEvent < AED::Event; end

dispatcher = AED::EventDispatcher.new

dispatcher.listener(MyEvent) { }
dispatcher.listener(MyEvent, name: "block-listener") { }

class MyListener
  @[AEDA::AsEventListener]
  def on_my_event(event : MyEvent) : Nil
  end
end

dispatcher.listener MyListener.new

dispatcher.listeners(MyEvent).map &.name # => ["unknown callable", "block-listener", "MyListener#on_my_event"]

AED::Callable::EventListenerInstance instances registered via AED::EventDispatcherInterface#listener(listener) will automatically have a name including the method and listener class names in the format of ClassName#method_name.

Included Modules

Direct Known Subclasses

Defined in:

callable.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(event_class : AED::Event.class, name : String | Nil, priority : Int32) #

[View source]

Instance Method Detail

def event_class : AED::Event.class #

Returns what AED::Event class this callable represents.


[View source]
def name : String #

Returns the name of this callable. Useful for debugging to identify a specific callable added from a block, or which method an AED::Callable::EventListenerInstance is associated with.


[View source]
def priority : Int32 #

Returns the [listener priority][Athena::EventDispatcher::EventDispatcherInterface--listener-priority] of this callable.


[View source]