diff --git a/src/telepathy/bot.cr b/src/telepathy/bot.cr index 89950ce..f9e01ad 100644 --- a/src/telepathy/bot.cr +++ b/src/telepathy/bot.cr @@ -81,30 +81,19 @@ module Telepathy end end - private def spawn_workers + private def spawn_worker spawn do - loop do - action = @poll_channel.receive - case action - when Int64? - @last_update_id = action.nil? ? @last_update_id : action + 1 - @update_channel.send get_updates 10 - when Control - break + while @poll_running + begin + updates = get_updates 10 + process_updates updates + @last_update_id = updates.last?.try &.update_id.+(1) || @last_update_id + rescue end end - end - spawn do - loop do - item = @update_channel.receive - case item - when Array(Update) - process_updates(item) - @poll_channel.send item.last?.try &.update_id - when Control - @poll_channel.send item - break - end + + @poll_end_hooks.each do |hook| + hook.call end end end @@ -112,18 +101,15 @@ module Telepathy def poll if !@poll_running @poll_running = true - spawn_workers - @poll_channel.send nil - @poll_start_hooks.each &.call + @poll_start_hooks.each do |hook| + hook.call + end + spawn_worker end end def end_poll - if @poll_running - @poll_running = false - @poll_channel.send Control::Done - @poll_end_hooks.each &.call - end + @poll_running = false end end end