Class: TD::Client
- Inherits:
-
Object
- Object
- TD::Client
- Includes:
- Concurrent, ClientMethods
- Defined in:
- lib/tdlib/client.rb
Overview
Simple client for TDLib.
Constant Summary collapse
- TIMEOUT =
20
Class Method Summary collapse
Instance Method Summary collapse
- #alive? ⇒ Boolean
-
#broadcast(query) ⇒ Concurrent::Promises::Future
Sends asynchronous request to the TDLib client and returns Promise object.
-
#connect ⇒ Concurrent::Promises::Future
Adds initial authorization state handler and runs update manager Returns future that will be fulfilled when client is ready.
- #dead? ⇒ Boolean
-
#dispose ⇒ Object
Stops update manager and destroys TDLib client.
-
#execute(query) ⇒ Object
Synchronously executes TDLib request Only a few requests can be executed synchronously.
-
#fetch(query) ⇒ Hash
(also: #broadcast_and_receive)
Sends asynchronous request to the TDLib client and returns received update synchronously.
-
#initialize(td_client = TD::Api.client_create, update_manager = TD::UpdateManager.new(td_client), timeout: TIMEOUT, **extra_config) ⇒ Client
constructor
A new instance of Client.
-
#on(update_type) {|update| ... } ⇒ Object
Binds passed block as a handler for updates with type of update_type.
- #on_ready(&action) ⇒ Object deprecated Deprecated.
-
#ready ⇒ Concurrent::Promises::Future
returns future that will be fulfilled when client is ready.
- #ready? ⇒ Boolean
Constructor Details
#initialize(td_client = TD::Api.client_create, update_manager = TD::UpdateManager.new(td_client), timeout: TIMEOUT, **extra_config) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tdlib/client.rb', line 18 def initialize(td_client = TD::Api.client_create, update_manager = TD::UpdateManager.new(td_client), timeout: TIMEOUT, **extra_config) @td_client = td_client @ready = false @alive = true @update_manager = update_manager @timeout = timeout @config = TD.config.client.to_h.merge(extra_config) @ready_condition_mutex = Mutex.new @ready_condition = ConditionVariable.new end |
Class Method Details
.ready(*args) ⇒ Object
10 11 12 |
# File 'lib/tdlib/client.rb', line 10 def self.ready(*args) new(*args).connect end |
Instance Method Details
#alive? ⇒ Boolean
152 153 154 |
# File 'lib/tdlib/client.rb', line 152 def alive? @alive end |
#broadcast(query) ⇒ Concurrent::Promises::Future
Sends asynchronous request to the TDLib client and returns Promise object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/tdlib/client.rb', line 61 def broadcast(query) return dead_client_promise if dead? Promises.future do condition = ConditionVariable.new extra = SecureRandom.uuid result = nil mutex = Mutex.new @update_manager << TD::UpdateHandler.new(TD::Types::Base, extra, disposable: true) do |update| mutex.synchronize do result = update condition.signal end end query['@extra'] = extra mutex.synchronize do send_to_td_client(query) condition.wait(mutex, @timeout) error = nil error = result if result.is_a?(TD::Types::Error) error = timeout_error if result.nil? raise TD::Error.new(error) if error result end end end |
#connect ⇒ Concurrent::Promises::Future
Adds initial authorization state handler and runs update manager Returns future that will be fulfilled when client is ready
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tdlib/client.rb', line 35 def connect on TD::Types::Update::AuthorizationState do |update| case update. when TD::Types::AuthorizationState::WaitTdlibParameters set_tdlib_parameters(**@config) @ready_condition_mutex.synchronize do @ready = true @ready_condition.broadcast end else # do nothing end end @update_manager.run(callback: method(:handle_update)) ready end |
#dead? ⇒ Boolean
156 157 158 |
# File 'lib/tdlib/client.rb', line 156 def dead? !alive? end |
#dispose ⇒ Object
Stops update manager and destroys TDLib client
147 148 149 150 |
# File 'lib/tdlib/client.rb', line 147 def dispose return if dead? close.then { } end |
#execute(query) ⇒ Object
Synchronously executes TDLib request Only a few requests can be executed synchronously
103 104 105 106 |
# File 'lib/tdlib/client.rb', line 103 def execute(query) return dead_client_error if dead? TD::Api.client_execute(@td_client, query) end |
#fetch(query) ⇒ Hash Also known as: broadcast_and_receive
Sends asynchronous request to the TDLib client and returns received update synchronously
94 95 96 |
# File 'lib/tdlib/client.rb', line 94 def fetch(query) broadcast(query).value! end |
#on(update_type) {|update| ... } ⇒ Object
Binds passed block as a handler for updates with type of update_type
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/tdlib/client.rb', line 111 def on(update_type, &action) if update_type.is_a?(String) if (type_const = TD::Types::LOOKUP_TABLE[update_type]) update_type = TD::Types.const_get("TD::Types::#{type_const}") else raise ArgumentError.new("Can't find class for #{update_type}") end end unless update_type < TD::Types::Base raise ArgumentError.new("Wrong type specified (#{update_type}). Should be of kind TD::Types::Base") end @update_manager << TD::UpdateHandler.new(update_type, &action) end |
#on_ready(&action) ⇒ Object
142 143 144 |
# File 'lib/tdlib/client.rb', line 142 def on_ready(&action) ready.then(&action).value! end |
#ready ⇒ Concurrent::Promises::Future
returns future that will be fulfilled when client is ready
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/tdlib/client.rb', line 129 def ready return dead_client_promise if dead? return Promises.fulfilled_future(self) if ready? Promises.future do @ready_condition_mutex.synchronize do next self if @ready || (@ready_condition.wait(@ready_condition_mutex, @timeout) && @ready) raise TD::Error.new(timeout_error) end end end |
#ready? ⇒ Boolean
160 161 162 |
# File 'lib/tdlib/client.rb', line 160 def ready? @ready end |