diff --git a/src/telepathy/bot.cr b/src/telepathy/bot.cr index 1a955c8..4856cf0 100644 --- a/src/telepathy/bot.cr +++ b/src/telepathy/bot.cr @@ -84,34 +84,38 @@ module Telepathy end end + private def spawn_workers + 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 + @update_channel.send action + break + 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 + break + end + end + end + end + def poll if !@poll_running @poll_running = true - 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 - @update_channel.send action - break - 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 - break - end - end - end + spawn_workers end @poll_channel.send nil end