Hooks::Hook(T)
Thread-safe event handling system
# Set up hooks with ordered handlers
hook = Hook(Int32).new
hook.add(Handler(Int32).new { |n| puts "First: #{n}" })
hook.pre_add(Handler(Int32).new { |n| puts "Before: #{n}" })
# Add one-off handlers during trigger
hook.trigger(42,
Handler(Int32).new { |n| puts "One-time: #{n}" },
Handler(Int32).new { |n| raise StopPropagation.new } # Stop processing
)
# Handle errors without stopping
result = hook.trigger_with_continuation(42)
puts result if result.is_a?(Array(Exception))
Instance methods
Registers a new handler to the hook by prepending it to the existing queue
Triggers all registered hook handlers sequentially with data as an argument.
It also supports adding temporary, one-off handlers for a single trigger execution.
Execution halts when either StopPropagation is raised or any other exception occurs.
Triggers all registered hook handlers sequentially with data as an argument.
It also supports adding temporary, one-off handlers for a single trigger execution.
Execution halts when either StopPropagation is raised or any other exception occurs.
Triggers all registered hook handlers sequentially with data as an argument.
This variant continues execution after encountering exceptions, except for StopPropagation,
which halts further execution.