abstract struct EventWinder
- EventWinder
- Struct
- Value
- Object
Direct Known Subclasses
Defined in:
event-winder.crConstructors
Class Method Summary
Instance Method Summary
Macro Summary
-
emit(type, *payload)
Emits an event, optionally with payload
-
handle_errors_with(&block)
Defines global error handler for all events without error handlers defined on registration
-
on(type, capacity = 0, &block)
Creates a handler for the event type
-
register(type, payload = nil, error_handler = nil)
Registers a new event and it's payload types
Constructor Detail
Class Method Detail
Instance Method Detail
Macro Detail
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
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?
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
).