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

View File

@ -18,6 +18,8 @@ module Telepathy
@last_update_id = nil @last_update_id = nil
@command_hooks = {} of String => Update, Array(String) -> Void @command_hooks = {} of String => Update, Array(String) -> Void
@message_hooks = [] of Update -> Void @message_hooks = [] of Update -> Void
@poll_start_hooks = [] of -> Void
@poll_end_hooks = [] of -> Void
@poll_channel = Channel(Int64?|Control).new @poll_channel = Channel(Int64?|Control).new
@update_channel = Channel(Array(Update)|Control).new @update_channel = Channel(Array(Update)|Control).new
@poll_running = false @poll_running = false
@ -102,6 +104,14 @@ module Telepathy
@message_hooks.push(block) @message_hooks.push(block)
end 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) private def process_updates(updates)
updates.each do |update| updates.each do |update|
if message = update.message if message = update.message
@ -154,6 +164,7 @@ module Telepathy
@poll_running = true @poll_running = true
spawn_workers spawn_workers
@poll_channel.send nil @poll_channel.send nil
@poll_start_hooks.each &.call
end end
end end
@ -161,6 +172,7 @@ module Telepathy
if @poll_running if @poll_running
@poll_running = false @poll_running = false
@poll_channel.send Control::Done @poll_channel.send Control::Done
@poll_end_hooks.each &.call
end end
end end
end end