abstract struct EventWinder

Direct Known Subclasses

Defined in:

event-winder.cr

Constructors

Class Method Summary

Instance Method Summary

Macro Summary

Constructor Detail

def self.new #

[View source]

Class Method Detail

def self.handle_error(error : Exception, payload_inspect : String = "no payload") #

[View source]

Instance Method Detail

def initialize #

[View source]

Macro Detail

macro emit(type, *payload) #

Emits an event, optionally with payload


[View source]
macro handle_errors_with(&block) #

Defines global error handler for all events without error handlers defined on registration

See block arguments description at register macro.

EventWinder.handle_errors_with do
  puts "#{self.name} event failed to handle with #{error} error, payload was #{payload_inspect}"
end

[View source]
macro on(type, capacity = 0, &block) #

Creates a handler for the event type

The block should be provided where the arguments depend on the payload for this event type:

  • with no payload block should not have arguments
  • with non-tuple payload block should have one argument
  • with tuple payload number of arguments should match the tuple's size

You can use capacity argument in the same way as in buffered channels: https://crystal-lang.org/reference/latest/guides/concurrency.html#buffered-channels

Usually you should not use it because every emit performs in its own fiber.

TODO specs for capacity argument, is it possible?


[View source]
macro register(type, payload = nil, error_handler = nil) #

Registers a new event and it's payload types

The payload should be a type or tuple of types. It cannot be explicit Nil because it's used implicitly for no-payload events.

The error_handler should be a proc where you can access error variable with an exception and payload_inspect variable with a string representation of a payload provided on emit. You also can use self.name to get the current event type, which is useful for global error handler (see handle_errors_with).


[View source]