class TD::UpdateManager

Constants

TIMEOUT

Attributes

handlers[R]

Public Class Methods

new(td_client) click to toggle source
# File lib/tdlib/update_manager.rb, line 4
def initialize(td_client)
  @td_client = td_client
  @handlers = Concurrent::Array.new
  @mutex = Mutex.new
end

Public Instance Methods

<<(handler)
Alias for: add_handler
add_handler(handler) click to toggle source
# File lib/tdlib/update_manager.rb, line 10
def add_handler(handler)
  @mutex.synchronize { @handlers << handler }
end
Also aliased as: <<
run(callback: nil) click to toggle source
# File lib/tdlib/update_manager.rb, line 16
def run(callback: nil)
  Thread.start do
    catch(:client_closed) { loop { handle_update(callback: callback); sleep 0.001 } }
    @mutex.synchronize { @handlers = [] }
  end
end

Private Instance Methods

handle_update(callback: nil) click to toggle source
# File lib/tdlib/update_manager.rb, line 27
def handle_update(callback: nil)
  update = TD::Api.client_receive(@td_client, TIMEOUT)

  unless update.nil?
    extra  = update.delete('@extra')
    update = TD::Types.wrap(update)
    callback&.call(update)

    match_handlers!(update, extra).each { |h| h.async.run(update) }
  end
rescue StandardError => e
  warn("Uncaught exception in update manager: #{e.message}")
end
match_handlers!(update, extra) click to toggle source
# File lib/tdlib/update_manager.rb, line 41
def match_handlers!(update, extra)
  @mutex.synchronize do
    matched_handlers = handlers.select { |h| h.match?(update, extra) }
    matched_handlers.each { |h| handlers.delete(h) if h.disposable? }
    matched_handlers
  end
end