abstract struct EventWinder

Direct Known Subclasses

Defined in:

event-winder.cr

Constructors

Class Method Summary

Macro Summary

Instance Method Summary

Constructor Detail

def self.new #

Class Method Detail

def self.drain(timeout : Time::Span = 5.seconds) : Bool #

Waits until all handler calls emitted before this point finish, or until timeout expires. Returns true when the queue is idle.

Call this after stopping event producers during graceful shutdown.


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

def self.pending_handlings : Int32 #

Number of handler calls that were emitted but have not finished yet.


Macro Detail

macro emit(type, *payload) #

Emits an event, optionally with payload, evaluated once in the caller. Returns true if accepted, or false for an overflowing :drop event. Admission is all-or-nothing across the event's handlers.


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

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.


macro register(type, payload = nil, error_handler = nil, max_pending = nil, overflow = :raise) #

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). max_pending limits queued plus active handler calls for this event type. overflow is :raise (default), :block, or :drop.


Instance Method Detail

def initialize #