From bfda135c193a0c78c91ca24bbd91261fda6428be Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 19 Jan 2018 12:15:10 -0800 Subject: [PATCH] Add hooks that get called when the polling mechanism starts or ends. --- src/telepathy/bot.cr | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/telepathy/bot.cr b/src/telepathy/bot.cr index e4d5766..53a2593 100644 --- a/src/telepathy/bot.cr +++ b/src/telepathy/bot.cr @@ -18,6 +18,8 @@ module Telepathy @last_update_id = nil @command_hooks = {} of String => Update, Array(String) -> Void @message_hooks = [] of Update -> Void + @poll_start_hooks = [] of -> Void + @poll_end_hooks = [] of -> Void @poll_channel = Channel(Int64?|Control).new @update_channel = Channel(Array(Update)|Control).new @poll_running = false @@ -102,6 +104,14 @@ module Telepathy @message_hooks.push(block) end + def poll_start(&block: -> Void) + @poll_start_hooks.push(block); + end + + def poll_end(&block: -> Void) + @poll_end_hooks.push(block); + end + private def process_updates(updates) updates.each do |update| if message = update.message @@ -154,6 +164,7 @@ module Telepathy @poll_running = true spawn_workers @poll_channel.send nil + @poll_start_hooks.each &.call end end @@ -161,6 +172,7 @@ module Telepathy if @poll_running @poll_running = false @poll_channel.send Control::Done + @poll_end_hooks.each &.call end end end