tdlib-ruby 0.9.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 81084785487847d98d74b1a27857235c9094031a
4
- data.tar.gz: 665dc58fe58969ddc4f1cd1ccb23f4d03c543192
2
+ SHA256:
3
+ metadata.gz: 4843f71309fcaf5799ce384c56d1df0a7d7d36131a5e162e3f3fd6d14dda68f7
4
+ data.tar.gz: eca6f03dd7e531f6a80595c562954427b71528c84d321a77e8babac791af9692
5
5
  SHA512:
6
- metadata.gz: dcf903551729e52eba8c6772b8bde87e9ffb350026ca9200c3fb67196f1a41a462ea6d15bf95e2d6b525d57c76e6cb481ef61669c240a6782bc9d792dcaf5bf3
7
- data.tar.gz: 8b704d60ef867628888dfaa87c3197cb81a4aad17155367df9545bac180ca724be821101ddd6b2534bc0dfc847cd0032e44a0f7b577c252fab500b642a2155b4
6
+ metadata.gz: 3618eb07efe5081c116efb1ee9cc0cf39ccff51bd1fd1933e3c0e1690b27e1be12087faf91096e1636fc00a55f3edb9797b8c616e38418187fd2a3b2b3bdb157
7
+ data.tar.gz: a87798327ce6f1995fbce7f2c5a216ed7c4baa01b65bb81cc109fbca7c757f8e20dce56b2e373a703e83e7ddaaf4af6e69cbc697e8fd845ef2d8b7c4c86c7ad3
@@ -1,3 +1,8 @@
1
+ ### 1.0.0 / 2018-05-27
2
+
3
+ * Return promises from TD::Client#broadcast
4
+ * Add #fetch as alias to #broadcast_and_receive
5
+
1
6
  ### 0.9.4 / 2018-05-16
2
7
 
3
8
  * Fix recursive locking in nested handlers
data/README.md CHANGED
@@ -100,6 +100,14 @@ end
100
100
  p @me
101
101
  ```
102
102
 
103
+ ## TD::Client#broadcast
104
+
105
+ From version 1.0 TD::Client##broadcast returns [Concurrent::Promise](http://ruby-concurrency.github.io/concurrent-ruby/Concurrent/Promise.html) object.
106
+
107
+ ```ruby
108
+ me = client.broadcast('@type' => 'getMe').then { |result| puts result }.rescue { |error| puts error }.value
109
+ ```
110
+
103
111
  ## Configuration
104
112
 
105
113
  ```ruby
@@ -58,6 +58,8 @@
58
58
  #
59
59
  # p @me
60
60
  class TD::Client
61
+ include Concurrent
62
+
61
63
  TIMEOUT = 20
62
64
 
63
65
  def initialize(td_client = TD::Api.client_create,
@@ -74,49 +76,47 @@ class TD::Client
74
76
  @update_manager.run
75
77
  end
76
78
 
77
- # Sends asynchronous request to the TDLib client
79
+ # Sends asynchronous request to the TDLib client and returns Promise object
80
+ # @see https://www.rubydoc.info/github/ruby-concurrency/concurrent-ruby/Concurrent/Promise)
81
+ # @example
82
+ # client.broadcast(some_query).then { |result| puts result }.rescue
78
83
  # @param [Hash] query
79
- # @yield [update] yields update to the block as soon as it's received
80
- def broadcast(query)
81
- if block_given?
84
+ # @param [Numeric] timeout
85
+ # @return [Concurrent::Promise]
86
+ def broadcast(query, timeout: TIMEOUT)
87
+ Promise.execute do
88
+ condition = ConditionVariable.new
82
89
  extra = TD::Utils.generate_extra(query)
90
+ result = nil
91
+ mutex = Mutex.new
83
92
  handler = ->(update) do
84
93
  return unless update['@extra'] == extra
85
- yield update
86
- @update_manager.remove_handler(handler)
94
+ mutex.synchronize do
95
+ result = update
96
+ @update_manager.remove_handler(handler)
97
+ condition.signal
98
+ end
87
99
  end
88
100
  @update_manager.add_handler(handler)
89
101
  query['@extra'] = extra
102
+ mutex.synchronize do
103
+ TD::Api.client_send(@td_client, query)
104
+ condition.wait(mutex, timeout)
105
+ raise TD::TimeoutError if result.nil?
106
+ result
107
+ end
90
108
  end
91
- TD::Api.client_send(@td_client, query)
92
109
  end
93
110
 
94
111
  # Sends asynchronous request to the TDLib client and returns received update synchronously
95
112
  # @param [Hash] query
96
113
  # @return [Hash]
97
- def broadcast_and_receive(query, timeout: TIMEOUT)
98
- condition = ConditionVariable.new
99
- extra = TD::Utils.generate_extra(query)
100
- result = nil
101
- mutex = Mutex.new
102
- handler = ->(update) do
103
- return unless update['@extra'] == extra
104
- mutex.synchronize do
105
- result = update
106
- @update_manager.remove_handler(handler)
107
- condition.signal
108
- end
109
- end
110
- @update_manager.add_handler(handler)
111
- query['@extra'] = extra
112
- mutex.synchronize do
113
- TD::Api.client_send(@td_client, query)
114
- condition.wait(mutex, timeout)
115
- raise TD::TimeoutError if result.nil?
116
- result
117
- end
114
+ def fetch(query, timeout: TIMEOUT)
115
+ broadcast(query, timeout: timeout).value
118
116
  end
119
117
 
118
+ alias broadcast_and_receive fetch
119
+
120
120
  # Synchronously executes TDLib request
121
121
  # Only a few requests can be executed synchronously
122
122
  # @param [Hash] query
@@ -1,4 +1,4 @@
1
1
  module TD
2
2
  # tdlib-ruby version
3
- VERSION = "0.9.4"
3
+ VERSION = "1.0.0"
4
4
  end
@@ -35,16 +35,9 @@ describe TD::Client do
35
35
  subject { client.on_ready { client.broadcast(payload) } }
36
36
 
37
37
  it { expect { subject }.not_to raise_error(Exception) }
38
- end
39
-
40
- context 'when block given' do
41
- subject { client.on_ready { client.broadcast(payload) { |update| @result = update } } }
42
-
43
- it 'runs block on update' do
44
- subject
45
- sleep 1
46
- expect(@result).to include('@type', 'entities')
47
- end
38
+ it { is_expected.to satisfy { |result| result.state == :pending } }
39
+ it { is_expected.to satisfy { |result| sleep 1; result.state == :fulfilled } }
40
+ it { is_expected.to satisfy { |result| sleep 1; result.value['@type'] == 'textEntities' } }
48
41
  end
49
42
  end
50
43
 
@@ -30,6 +30,7 @@ Gem::Specification.new do |gem|
30
30
  gem.require_paths = ['lib']
31
31
 
32
32
  gem.add_runtime_dependency 'dry-configurable', '~> 0.7'
33
+ gem.add_runtime_dependency 'concurrent-ruby', '~> 1.0'
33
34
 
34
35
  gem.add_development_dependency 'bundler', '~> 1.10'
35
36
  gem.add_development_dependency 'rake', '12.3.1'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tdlib-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Southbridge
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-16 00:00:00.000000000 Z
11
+ date: 2018-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: concurrent-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -157,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
171
  version: '0'
158
172
  requirements: []
159
173
  rubyforge_project:
160
- rubygems_version: 2.6.14
174
+ rubygems_version: 2.7.6
161
175
  signing_key:
162
176
  specification_version: 4
163
177
  summary: Ruby bindings and client for TDlib