Skip to content

Commit

Permalink
Have HandlerList#dispatch return an array of threads started by the e…
Browse files Browse the repository at this point in the history
…vent.

This allows plugins to wait for handler threads to complete, if desired.
  • Loading branch information
nickrw committed Jun 12, 2013
1 parent d3c8975 commit 62931f9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/cinch/handler.rb
Expand Up @@ -74,11 +74,11 @@ def stop
# @param [Message] message Message that caused the invocation
# @param [Array] captures Capture groups of the pattern that are
# being passed as arguments
# @return [void]
# @return [Thread]
def call(message, captures, arguments)
bargs = captures + arguments

@thread_group.add Thread.new {
thread = Thread.new {
@bot.loggers.debug "[New thread] For #{self}: #{Thread.current} -- #{@thread_group.list.size} in total."

begin
Expand All @@ -93,6 +93,9 @@ def call(message, captures, arguments)
@bot.loggers.debug "[Thread done] For #{self}: #{Thread.current} -- #{@thread_group.list.size - 1} remaining."
end
}

@thread_group.add thread
thread
end

# @return [String]
Expand Down
6 changes: 4 additions & 2 deletions lib/cinch/handler_list.rb
Expand Up @@ -51,8 +51,9 @@ def find(type, msg = nil)
# and attached to the event, or nil.
# @param [Array] *arguments A list of additional arguments to pass
# to event handlers
# @return [void]
# @return [Array<Thread>]
def dispatch(event, msg = nil, *arguments)
threads = Array.new
if handlers = find(event, msg)
already_run = Set.new
handlers.each do |handler|
Expand All @@ -66,9 +67,10 @@ def dispatch(event, msg = nil, *arguments)
captures = []
end

handler.call(msg, captures, arguments)
threads << handler.call(msg, captures, arguments)
end
end
threads
end

# @yield [handler] Yields all registered handlers
Expand Down

0 comments on commit 62931f9

Please sign in to comment.