class Cinch::HandlerList
@since 2.0.0
Public Class Methods
new()
click to toggle source
# File lib/cinch/handler_list.rb, line 11 def initialize @handlers = Hash.new { |h, k| h[k] = [] } @mutex = Mutex.new end
Public Instance Methods
dispatch(event, msg = nil, *arguments)
click to toggle source
@param [Symbol] event The event type @param [Message, nil] msg The message which is responsible for
and attached to the event, or nil.
@param [Array] arguments A list of additional arguments to pass
to event handlers
@return [Array<Thread>]
# File lib/cinch/handler_list.rb, line 54 def dispatch(event, msg = nil, *arguments) threads = [] if handlers = find(event, msg) already_run = Set.new handlers.each do |handler| next if already_run.include?(handler.block) already_run << handler.block # calling Message#match multiple times is not a problem # because we cache the result captures = if msg msg.match(handler.pattern.to_r(msg), event, handler.strip_colors).captures else [] end threads << handler.call(msg, captures, arguments) end end threads end
each(&block)
click to toggle source
@yield [handler] Yields all registered handlers @yieldparam [Handler] handler @return [void]
# File lib/cinch/handler_list.rb, line 81 def each(&block) @handlers.values.flatten.each(&block) end
find(type, msg = nil)
click to toggle source
@api private @return [Array<Handler>]
# File lib/cinch/handler_list.rb, line 36 def find(type, msg = nil) if handlers = @handlers[type] return handlers if msg.nil? handlers = handlers.select do |handler| msg.match(handler.pattern.to_r(msg), type, handler.strip_colors) end.group_by(&:group) handlers.values_at(*(handlers.keys - [nil])).map(&:first) + (handlers[nil] || []) end end
register(handler)
click to toggle source
# File lib/cinch/handler_list.rb, line 16 def register(handler) @mutex.synchronize do handler.bot.loggers.debug "[on handler] Registering handler with pattern `#{handler.pattern.inspect}`, reacting on `#{handler.event}`" @handlers[handler.event] << handler end end
stop_all()
click to toggle source
@api private
# File lib/cinch/handler_list.rb, line 86 def stop_all each(&:stop) end
unregister(*handlers)
click to toggle source
@param [Handler, Array<Handler>] handlers The handlers to unregister @return [void] @see Handler#unregister
# File lib/cinch/handler_list.rb, line 26 def unregister(*handlers) @mutex.synchronize do handlers.each do |handler| @handlers[handler.event].delete(handler) end end end