Sha256: 51ac3d1972366b0c3cb8d64e09ec93e6029e65f18537e8a9f88022d76d29ef39
Contents?: true
Size: 1.12 KB
Versions: 2
Compression:
Stored size: 1.12 KB
Contents
class TD::UpdateManager TIMEOUT = 30 def initialize(td_client) @td_client = td_client @handlers = Concurrent::Array.new @mutex = Mutex.new end def add_handler(handler) @mutex.synchronize { @handlers << handler } end alias << add_handler def run(callback: nil) Thread.start do catch(:client_closed) { loop { handle_update(callback: callback); sleep 0.001 } } @mutex.synchronize { @handlers = [] } end end private attr_reader :handlers 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 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 end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
tdlib-ruby-3.1.0 | lib/tdlib/update_manager.rb |
tdlib-ruby-3.0.2 | lib/tdlib/update_manager.rb |