Sha256: 35ee92ae91c9921e8cbb0a83b39038aae63e32467e29adb4f411568af6415a01

Contents?: true

Size: 769 Bytes

Versions: 1

Compression:

Stored size: 769 Bytes

Contents

class TD::UpdateManager
  attr_reader :handlers

  def initialize(td_client)
    @td_client = td_client
    @handlers = []
    @mutex = Mutex.new
    init_update_loop
  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 stop
    @stopped = true
  end

  def stopped?
    !!@stopped
  end

  private

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tdlib-ruby-0.4.0 lib/tdlib/update_manager.rb