Sha256: 33e69d36af08fab9c3cb69de415cd28e6892bf1167273e3db9a04a0e273e8448

Contents?: true

Size: 771 Bytes

Versions: 2

Compression:

Stored size: 771 Bytes

Contents

class TD::UpdateManager
  TIMEOUT = 30

  attr_reader :handlers

  def initialize(td_client)
    @td_client = td_client
    @handlers = []
    @mutex = Mutex.new
  end

  def add_handler(handler)
    @mutex.synchronize { @handlers << handler }
  end

  def remove_handler(handler)
    Thread.start do
      @mutex.synchronize { @handlers.delete(handler) }
    end
  end

  def run
    @update_loop_thread = Thread.start do
      loop { stopped? ? break : handle_update }
    end
  end

  def stop
    @stopped = true
  end

  def stopped?
    !!@stopped
  end

  private

  def handle_update
    update = TD::Api.client_receive(@td_client, TIMEOUT)
    @mutex.synchronize { handlers = @handlers.dup }
    handlers.each { |h| h.call(update) } unless update.nil?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tdlib-ruby-1.0.0 lib/tdlib/update_manager.rb
tdlib-ruby-0.9.4 lib/tdlib/update_manager.rb