From d020c0c9bf56d0bd6e95842d2f533d9c33fded6d Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 12 Jan 2018 22:07:32 -0800 Subject: [PATCH] Move the spawn code out of the poll method. --- src/telepathy/bot.cr | 54 ++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 25 deletions(-) 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