Add hooks that get called when the polling mechanism starts or ends.

This commit is contained in:
Danila Fedorin 2018-01-19 12:15:10 -08:00
parent 806d8db44c
commit bfda135c19
1 changed files with 12 additions and 0 deletions

View File

@ -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