libuv 3.3.0 → 4.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
2
  SHA1:
3
- metadata.gz: '092646a6b10dd6f3b39277326714b7087447d159'
4
- data.tar.gz: a774a2227b1b970fa38616cdd8d03c9ccc971935
3
+ metadata.gz: b07182db4afe88a245218f486d7f29091a6facf5
4
+ data.tar.gz: b90adcd03a747e4af99e02befa38901befea1351
5
5
  SHA512:
6
- metadata.gz: 7de8cf263154b94ca967ac1be2969e833ba3128d7f8891690a0d951562214b2ed253f57c7854297cb9011386e2157d6d496ced5344ea38df10a3cea8ad5951a5
7
- data.tar.gz: 5992ba91e3296e41751fa3d61f6ff433f21718673b62af60e8c8a40fca47ee105e7d15875604d74b83bf9d53a2d4a73047a943105568d387179ec7304e15904f
6
+ metadata.gz: e1a90d594b27b9d198781340e7ed1478e40fa41bf2e0b83e4bda195b5791445c12726df0d5e76f8318f6b1117ea145692fe9bd034b735004e866163a921186f7
7
+ data.tar.gz: 68c23d23198477dee0f5b31c04c8b2589b3af3adc79383a2e8a97a05c0fa3b859990e64147e2445ef6cb6d225ec03151fe5458111accc63f75aa7583584d5234
@@ -70,7 +70,11 @@ end
70
70
  class Object
71
71
  private
72
72
 
73
- def reactor(&blk)
74
- Libuv.reactor &blk
73
+ def reactor
74
+ if block_given?
75
+ Libuv.reactor { |thread| yield(thread) }
76
+ else
77
+ Libuv.reactor
78
+ end
75
79
  end
76
80
  end
@@ -8,9 +8,8 @@ module Libuv
8
8
 
9
9
 
10
10
  # @param reactor [::Libuv::Reactor] reactor this async callback will be associated
11
- def initialize(reactor, callback = nil, &blk)
11
+ def initialize(reactor)
12
12
  @reactor = reactor
13
- @callback = callback || blk
14
13
 
15
14
  async_ptr = ::Libuv::Ext.allocate_handle_async
16
15
  on_async = callback(:on_async, async_ptr.address)
@@ -30,8 +29,8 @@ module Libuv
30
29
  # Used to update the callback that will be triggered when async is called
31
30
  #
32
31
  # @param callback [Proc] the callback to be called on reactor prepare
33
- def progress(callback = nil, &blk)
34
- @callback = callback || blk
32
+ def progress(&callback)
33
+ @callback = callback
35
34
  self
36
35
  end
37
36
 
@@ -9,9 +9,8 @@ module Libuv
9
9
 
10
10
  # @param reactor [::Libuv::Reactor] reactor this check will be associated
11
11
  # @param callback [Proc] callback to be called on reactor check
12
- def initialize(reactor, callback = nil, &blk)
12
+ def initialize(reactor)
13
13
  @reactor = reactor
14
- @callback = callback || blk
15
14
 
16
15
  check_ptr = ::Libuv::Ext.allocate_handle_check
17
16
  error = check_result(::Libuv::Ext.check_init(reactor.handle, check_ptr))
@@ -38,8 +37,8 @@ module Libuv
38
37
  # Used to update the callback that will be triggered on reactor check
39
38
  #
40
39
  # @param callback [Proc] the callback to be called on reactor check
41
- def progress(callback = nil, &blk)
42
- @callback = callback || blk
40
+ def progress(&callback)
41
+ @callback = callback
43
42
  self
44
43
  end
45
44
 
@@ -6,17 +6,14 @@ class CoroutineRejection < RuntimeError
6
6
  attr_accessor :value
7
7
  end
8
8
 
9
- class Object
10
- private
11
-
12
-
9
+ module Libuv
13
10
  # Takes a Promise response and turns it into a co-routine
14
11
  # for code execution without using callbacks
15
12
  #
16
13
  # @param *promises [::Libuv::Q::Promise] a number of promises that will be combined into a single promise
17
14
  # @return [Object] Returns the result of a single promise or an array of results if provided multiple promises
18
15
  # @raise [Exception] if the promise is rejected
19
- def co(*yieldable, &block)
16
+ def co(*yieldable)
20
17
  on_reactor = Libuv::Reactor.current
21
18
  raise 'must be running on a reactor thread to use coroutines' unless on_reactor
22
19
 
@@ -27,7 +24,7 @@ class Object
27
24
  if yieldable.length == 1
28
25
  promise = yieldable[0]
29
26
  # Passed independently as this is often overwritten for performance
30
- promise.progress(block) if block_given?
27
+ promise.progress &Proc.new if block_given?
31
28
  else
32
29
  promise = on_reactor.all(*yieldable)
33
30
  end
@@ -77,4 +74,6 @@ class Object
77
74
  end
78
75
  result
79
76
  end
77
+
78
+ module_function :co
80
79
  end
@@ -52,7 +52,7 @@ module Libuv
52
52
  @defer.reject(error)
53
53
  end
54
54
 
55
- co(@defer.promise) if wait
55
+ @defer.promise.value if wait
56
56
  end
57
57
 
58
58
  # Indicates if the lookup has completed yet or not.
@@ -29,7 +29,7 @@ module Libuv
29
29
  attr_reader :fileno, :closed
30
30
 
31
31
 
32
- def initialize(thread, path, flags = 0, mode: 0, wait: true, &blk)
32
+ def initialize(thread, path, flags = 0, mode: 0)
33
33
  super(thread, thread.defer)
34
34
 
35
35
  @fileno = -1
@@ -41,10 +41,10 @@ module Libuv
41
41
  pre_check @defer, request, ::Libuv::Ext.fs_open(@reactor, request, @path, @flags, @mode, callback(:on_open, request.address))
42
42
 
43
43
  if block_given?
44
- self.progress blk
45
- elsif wait
44
+ self.progress &Proc.new
45
+ else
46
46
  @coroutine = @reactor.defer
47
- co @coroutine.promise
47
+ @coroutine.promise.value
48
48
  end
49
49
  end
50
50
 
@@ -67,7 +67,7 @@ module Libuv
67
67
  @request_refs[request.address] = [deferred, buffer1]
68
68
 
69
69
  promise = pre_check(deferred, request, ::Libuv::Ext.fs_read(@reactor.handle, request, @fileno, buffer, 1, offset, callback(:on_read, request.address)))
70
- wait ? co(promise) : promise
70
+ wait ? promise.value : promise
71
71
  end
72
72
 
73
73
  def write(data, offset = 0, wait: true)
@@ -123,7 +123,7 @@ module Libuv
123
123
  @request_refs[request.address] = deferred
124
124
 
125
125
  promise = pre_check deferred, request, ::Libuv::Ext.fs_futime(@reactor.handle, request, @fileno, atime, mtime, callback(:on_utime, request.address))
126
- wait ? co(promise) : promise
126
+ wait ? promise.value : promise
127
127
  end
128
128
 
129
129
  def chmod(mode, wait: true)
@@ -148,10 +148,12 @@ module Libuv
148
148
  end
149
149
 
150
150
  def send_file(stream, using: :raw, chunk_size: 4096, wait: true)
151
- @transmit_failure ||= method(:transmit_failure)
152
- @transmit_data ||= method(:transmit_data)
153
- @start_transmit ||= method(:start_transmit)
154
- @next_chunk ||= method(:next_chunk)
151
+ @transmit_failure ||= proc { |reason| @sending_file.reject(reason) }
152
+ @start_transmit ||= proc { |stats|
153
+ @file_stream_total = stats[:st_size]
154
+ next_chunk
155
+ }
156
+ @transmit_data ||= proc { |data| transmit_data(data) }
155
157
 
156
158
  @sending_file = @reactor.defer
157
159
  @file_stream = stream
@@ -162,7 +164,7 @@ module Libuv
162
164
  stat(wait: false).then @start_transmit, @transmit_failure
163
165
 
164
166
  promise = @sending_file.promise
165
- promise.finally &method(:clean_up_send)
167
+ promise.finally { clean_up_send }
166
168
  respond wait, promise
167
169
  end
168
170
 
@@ -172,11 +174,6 @@ module Libuv
172
174
 
173
175
  ##
174
176
  # File transmit functions -------------
175
- def start_transmit(stats)
176
- @file_stream_total = stats[:st_size]
177
- next_chunk
178
- end
179
-
180
177
  def transmit_data(data)
181
178
  @file_chunk_count += 1
182
179
  if @file_stream_type == :http
@@ -186,11 +183,11 @@ module Libuv
186
183
  resp << CRLF
187
184
  data = resp
188
185
  end
189
- @file_stream.write(data, wait: :promise).then @next_chunk, @transmit_failure
186
+ @file_stream.write(data, wait: :promise).then(proc { next_chunk }, @transmit_failure)
190
187
  nil
191
188
  end
192
189
 
193
- def next_chunk(*args)
190
+ def next_chunk
194
191
  next_size = @file_chunk_size
195
192
  next_offset = @file_chunk_size * @file_chunk_count
196
193
 
@@ -211,10 +208,6 @@ module Libuv
211
208
  nil
212
209
  end
213
210
 
214
- def transmit_failure(reason)
215
- @sending_file.reject(reason)
216
- end
217
-
218
211
  def clean_up_send
219
212
  @sending_file = nil
220
213
  @file_stream = nil
@@ -9,9 +9,8 @@ module Libuv
9
9
 
10
10
  # @param reactor [::Libuv::Reactor] reactor this idle handler will be associated
11
11
  # @param callback [Proc] callback to be called when the reactor is idle
12
- def initialize(reactor, callback = nil, &blk)
12
+ def initialize(reactor)
13
13
  @reactor = reactor
14
- @callback = callback || blk
15
14
 
16
15
  idle_ptr = ::Libuv::Ext.allocate_handle_idle
17
16
  error = check_result(::Libuv::Ext.idle_init(reactor.handle, idle_ptr))
@@ -38,8 +37,8 @@ module Libuv
38
37
  # Used to update the callback that will be triggered on idle
39
38
  #
40
39
  # @param callback [Proc] the callback to be called on idle trigger
41
- def progress(callback = nil, &blk)
42
- @callback = callback || blk
40
+ def progress(&callback)
41
+ @callback = callback
43
42
  self
44
43
  end
45
44
 
@@ -22,7 +22,7 @@ module Libuv
22
22
  pre_check @stat_deferred, request, ::Libuv::Ext.fs_fstat(@reactor.handle, request, @fileno, callback(:on_stat, request.address))
23
23
  promise = @stat_deferred.promise
24
24
 
25
- wait ? co(promise) : promise
25
+ wait ? promise.value : promise
26
26
  end
27
27
 
28
28
 
@@ -31,7 +31,7 @@ module Libuv
31
31
 
32
32
  def respond(wait, promise)
33
33
  if wait
34
- co(promise)
34
+ promise.value
35
35
  self
36
36
  else
37
37
  promise
@@ -104,7 +104,7 @@ module Libuv
104
104
 
105
105
  if wait
106
106
  return deferred.promise if wait == :promise
107
- co deferred.promise
107
+ deferred.promise.value
108
108
  end
109
109
 
110
110
  self
@@ -122,8 +122,8 @@ module Libuv
122
122
  ::Libuv::Ext.is_writable(handle) > 0
123
123
  end
124
124
 
125
- def progress(callback = nil, &blk)
126
- @progress = callback || blk
125
+ def progress(&blk)
126
+ @progress = blk
127
127
  self
128
128
  end
129
129
 
@@ -147,7 +147,7 @@ module Libuv
147
147
  end
148
148
  end
149
149
 
150
- co @read_defer.promise
150
+ @read_defer.promise.value
151
151
  end
152
152
  alias_method :read_nonblock, :read
153
153
 
@@ -158,7 +158,7 @@ module Libuv
158
158
 
159
159
  @flush_defer = @reactor.defer
160
160
  check_flush_buffer
161
- co @flush_defer.promise
161
+ @flush_defer.promise.value
162
162
  end
163
163
 
164
164
 
@@ -22,10 +22,10 @@ module Libuv
22
22
  super(pipe_ptr, error)
23
23
  end
24
24
 
25
- def bind(name, callback = nil, &blk)
25
+ def bind(name, &callback)
26
26
  return if @closed
27
- @on_accept = callback || blk
28
- @on_listen = method(:accept)
27
+ @on_accept = callback
28
+ @on_listen = proc { accept }
29
29
 
30
30
  assert_type(String, name, "name must be a String")
31
31
  name = windows_path name if FFI::Platform.windows?
@@ -36,8 +36,7 @@ module Libuv
36
36
  self
37
37
  end
38
38
 
39
- def open(fileno, callback = nil, &blk)
40
- @callback = callback || blk
39
+ def open(fileno)
41
40
  assert_type(Integer, fileno, 'fileno must be an integer file descriptor')
42
41
 
43
42
  begin
@@ -45,24 +44,25 @@ module Libuv
45
44
  check_result! ::Libuv::Ext.pipe_open(handle, fileno)
46
45
 
47
46
  # Emulate on_connect behavior
48
- begin
49
- @callback.call(self) if @callback
50
- rescue Exception => e
51
- @reactor.log e, 'performing pipe connect callback'
52
- raise e unless @callback
47
+ if block_given?
48
+ begin
49
+ yield(self)
50
+ rescue Exception => e
51
+ @reactor.log e, 'performing pipe connect callback'
52
+ end
53
53
  end
54
54
  rescue Exception => e
55
55
  reject(e)
56
- raise e unless @callback
56
+ raise e unless block_given?
57
57
  end
58
58
 
59
59
  self
60
60
  end
61
61
 
62
- def connect(name, callback = nil, &blk)
62
+ def connect(name)
63
63
  return if @closed
64
- @callback = callback || blk
65
64
  assert_type(String, name, "name must be a String")
65
+
66
66
  begin
67
67
  name = windows_path name if FFI::Platform.windows?
68
68
  req = ::Libuv::Ext.allocate_request_connect
@@ -71,9 +71,11 @@ module Libuv
71
71
  reject(e)
72
72
  end
73
73
 
74
- if @callback.nil?
74
+ if block_given?
75
+ @callback = Proc.new
76
+ else
75
77
  @coroutine = @reactor.defer
76
- co @coroutine.promise
78
+ @coroutine.promise.value
77
79
  end
78
80
 
79
81
  self
@@ -111,7 +113,7 @@ module Libuv
111
113
 
112
114
  if wait
113
115
  return deferred.promise if wait == :promise
114
- co deferred.promise
116
+ deferred.promise.value
115
117
  end
116
118
 
117
119
  self
@@ -156,7 +158,7 @@ module Libuv
156
158
  private
157
159
 
158
160
 
159
- def accept(_)
161
+ def accept
160
162
  pipe = nil
161
163
  begin
162
164
  raise RuntimeError, CLOSED_HANDLE_ERROR if @closed
@@ -9,9 +9,8 @@ module Libuv
9
9
 
10
10
  # @param reactor [::Libuv::Reactor] reactor this prepare handle will be associated
11
11
  # @param callback [Proc] callback to be called on reactor preparation
12
- def initialize(reactor, callback = nil, &blk)
12
+ def initialize(reactor)
13
13
  @reactor = reactor
14
- @callback = callback || blk
15
14
 
16
15
  prepare_ptr = ::Libuv::Ext.allocate_handle_prepare
17
16
  error = check_result(::Libuv::Ext.prepare_init(reactor.handle, prepare_ptr))
@@ -38,8 +37,8 @@ module Libuv
38
37
  # Used to update the callback that will be triggered on reactor prepare
39
38
  #
40
39
  # @param callback [Proc] the callback to be called on reactor prepare
41
- def progress(callback = nil, &blk)
42
- @callback = callback || blk
40
+ def progress(&callback)
41
+ @callback = callback
43
42
  self
44
43
  end
45
44
 
@@ -13,36 +13,24 @@ module Libuv
13
13
  # Allows a backtrace to be included in any errors
14
14
  attr_accessor :trace
15
15
 
16
- # Used by finally method
17
- MAKE_PROMISE = proc { |value, resolved, reactor|
18
- result = Q.defer(reactor)
19
- if (resolved)
20
- result.resolve(value)
21
- else
22
- result.reject(value)
23
- end
24
- result.promise
25
- }.freeze
26
-
27
-
28
16
  #
29
17
  # regardless of when the promise was or will be resolved / rejected, calls
30
18
  # the error callback asynchronously if the promise is rejected.
31
19
  #
32
20
  # @param [Proc, &blk] callbacks error, error_block
33
21
  # @return [Promise] Returns an unresolved promise for chaining
34
- def catch(callback = nil, &blk)
35
- self.then(nil, callback || blk)
22
+ def catch(&blk)
23
+ self.then(nil, blk)
36
24
  end
37
25
 
38
26
 
39
- def progress(callback = nil, &blk)
40
- self.then(nil, nil, callback || blk)
27
+ def progress(&blk)
28
+ self.then(nil, nil, blk)
41
29
  end
42
30
 
43
31
  # A future that provides the value or raises an error if a rejection occurs
44
32
  def value
45
- co self
33
+ ::Libuv.co(self)
46
34
  end
47
35
 
48
36
  #
@@ -52,26 +40,24 @@ module Libuv
52
40
  #
53
41
  # @param [Proc, &blk] callbacks finally, finally_block
54
42
  # @return [Promise] Returns an unresolved promise for chaining
55
- def finally(callback = nil, &blk)
56
- callback ||= blk
57
-
58
- handleCallback = lambda {|value, isResolved|
43
+ def finally
44
+ handleCallback = lambda { |value, isResolved|
59
45
  callbackOutput = nil
60
46
  begin
61
- callbackOutput = callback.call
47
+ callbackOutput = yield
62
48
  rescue Exception => e
63
49
  @reactor.log e, 'performing promise finally callback', @trace
64
- return MAKE_PROMISE.call(e, false, @reactor)
50
+ return make_promise(e, false, @reactor)
65
51
  end
66
52
 
67
53
  if callbackOutput.is_a?(Promise)
68
54
  return callbackOutput.then(proc {
69
- MAKE_PROMISE.call(value, isResolved, @reactor)
55
+ make_promise(value, isResolved, @reactor)
70
56
  }, proc { |err|
71
- MAKE_PROMISE.call(err, false, @reactor)
57
+ make_promise(err, false, @reactor)
72
58
  })
73
59
  else
74
- return MAKE_PROMISE.call(value, isResolved, @reactor)
60
+ return make_promise(value, isResolved, @reactor)
75
61
  end
76
62
  }
77
63
 
@@ -81,6 +67,18 @@ module Libuv
81
67
  handleCallback.call(err, false)
82
68
  })
83
69
  end
70
+
71
+ protected
72
+
73
+ def make_promise(value, resolved, reactor)
74
+ result = Q.defer(reactor)
75
+ if (resolved)
76
+ result.resolve(value)
77
+ else
78
+ result.reject(value)
79
+ end
80
+ result.promise
81
+ end
84
82
  end
85
83
 
86
84
 
@@ -94,7 +92,7 @@ module Libuv
94
92
  def initialize(reactor, defer)
95
93
  raise ArgumentError unless defer.is_a?(Deferred)
96
94
  super()
97
-
95
+
98
96
  @reactor = reactor
99
97
  @defer = defer
100
98
  end
@@ -106,14 +104,13 @@ module Libuv
106
104
  #
107
105
  # @param [Proc, Proc, Proc, &blk] callbacks error, success, progress, success_block
108
106
  # @return [Promise] Returns an unresolved promise for chaining
109
- def then(callback = nil, errback = nil, progback = nil, &blk)
107
+ def then(callback = nil, errback = nil, progback = nil)
110
108
  result = Q.defer(@reactor)
111
-
112
- callback ||= blk
109
+ callback = Proc.new if block_given?
113
110
 
114
111
  wrappedCallback = proc { |val|
115
112
  begin
116
- result.resolve(callback.nil? ? val : callback.call(val))
113
+ result.resolve(callback ? callback.call(val) : val)
117
114
  rescue Exception => e
118
115
  result.reject(e)
119
116
  @reactor.log e, 'performing promise resolution callback', @trace
@@ -122,7 +119,7 @@ module Libuv
122
119
 
123
120
  wrappedErrback = proc { |reason|
124
121
  begin
125
- result.resolve(errback.nil? ? Q.reject(@reactor, reason) : errback.call(reason))
122
+ result.resolve(errback ? errback.call(reason) : Q.reject(@reactor, reason))
126
123
  rescue Exception => e
127
124
  result.reject(e)
128
125
  @reactor.log e, 'performing promise rejection callback', @trace
@@ -131,7 +128,7 @@ module Libuv
131
128
 
132
129
  wrappedProgback = proc { |*progress|
133
130
  begin
134
- result.notify(progback.nil? ? progress : progback.call(*progress))
131
+ result.notify(progback ? progback.call(*progress) : progress)
135
132
  rescue Exception => e
136
133
  @reactor.log e, 'performing promise progress callback', @trace
137
134
  end
@@ -150,7 +147,7 @@ module Libuv
150
147
  pending_array << [wrappedCallback, wrappedErrback, wrappedProgback]
151
148
  end
152
149
  end
153
-
150
+
154
151
  result.promise
155
152
  end
156
153
 
@@ -185,22 +182,21 @@ module Libuv
185
182
  @response = response
186
183
  end
187
184
 
188
- def then(callback = nil, errback = nil, progback = nil, &blk)
185
+ def then(callback = nil, errback = nil, progback = nil)
189
186
  result = Q.defer(@reactor)
190
-
191
- callback ||= blk
187
+ callback = Proc.new if block_given?
192
188
 
193
189
  @reactor.next_tick {
194
190
  if @error
195
191
  begin
196
- result.resolve(errback.nil? ? Q.reject(@reactor, @response) : errback.call(@response))
192
+ result.resolve(errback ? errback.call(@response) : Q.reject(@reactor, @response))
197
193
  rescue Exception => e
198
194
  result.reject(e)
199
195
  @reactor.log e, 'performing promise rejection callback', @trace
200
196
  end
201
197
  else
202
198
  begin
203
- result.resolve(callback.nil? ? @response : callback.call(@response))
199
+ result.resolve(callback ? callback.call(@response) : @response)
204
200
  rescue Exception => e
205
201
  result.reject(e)
206
202
  @reactor.log e, 'performing promise resolution callback', @trace
@@ -296,7 +292,7 @@ module Libuv
296
292
  end
297
293
 
298
294
  def value
299
- co self.promise
295
+ ::Libuv.co(self.promise)
300
296
  end
301
297
 
302
298
  # Overwrite to prevent inspecting errors hanging the VM
@@ -62,30 +62,30 @@ module Libuv
62
62
 
63
63
  # Create an async call for scheduling work from other threads
64
64
  @run_queue = Queue.new
65
- @process_queue = @reactor.async method(:process_queue_cb)
65
+ @process_queue = @reactor.async { process_queue_cb }
66
66
  @process_queue.unref
67
67
 
68
68
  # Create a next tick timer
69
- @next_tick = @reactor.timer method(:next_tick_cb)
69
+ @next_tick = @reactor.timer { next_tick_cb }
70
70
  @next_tick.unref
71
71
 
72
72
  # Create an async call for ending the reactor
73
- @stop_reactor = @reactor.async method(:stop_cb)
73
+ @stop_reactor = @reactor.async { stop_cb }
74
74
  @stop_reactor.unref
75
75
 
76
76
  # Libuv can prevent the application shutting down once the main thread has ended
77
77
  # The addition of a prepare function prevents this from happening.
78
- @reactor_prep = Libuv::Prepare.new(@reactor, method(:noop))
78
+ @reactor_prep = prepare {}
79
79
  @reactor_prep.unref
80
80
  @reactor_prep.start
81
81
 
82
82
  # LibUV ingnores program interrupt by default.
83
83
  # We provide normal behaviour and allow this to be overriden
84
84
  @on_signal = []
85
- sig_callback = method(:signal_cb)
86
- self.signal(:INT, sig_callback).unref
87
- self.signal(:HUP, sig_callback).unref
88
- self.signal(:TERM, sig_callback).unref
85
+ sig_callback = proc { signal_cb }
86
+ self.signal(:INT, &sig_callback).unref
87
+ self.signal(:HUP, &sig_callback).unref
88
+ self.signal(:TERM, &sig_callback).unref
89
89
 
90
90
  # Notify of errors
91
91
  @throw_on_exit = nil
@@ -101,8 +101,6 @@ module Libuv
101
101
  protected
102
102
 
103
103
 
104
- def noop; end
105
-
106
104
  def stop_cb
107
105
  Thread.current.thread_variable_set(:reactor, nil)
108
106
  @reactor_running = false
@@ -110,7 +108,7 @@ module Libuv
110
108
  ::Libuv::Ext.stop(@pointer)
111
109
  end
112
110
 
113
- def signal_cb(_)
111
+ def signal_cb
114
112
  if @on_signal.empty?
115
113
  stop_cb
116
114
  else
@@ -226,8 +224,12 @@ module Libuv
226
224
  # Provides a promise notifier for receiving un-handled exceptions
227
225
  #
228
226
  # @return [::Libuv::Q::Promise]
229
- def notifier(callback = nil, &blk)
230
- @reactor_notify = callback || blk || @reactor_notify_default
227
+ def notifier
228
+ @reactor_notify = if block_given?
229
+ Proc.new
230
+ else
231
+ @reactor_notify_default
232
+ end
231
233
  self
232
234
  end
233
235
 
@@ -325,15 +327,15 @@ module Libuv
325
327
  # Get a new TCP instance
326
328
  #
327
329
  # @return [::Libuv::TCP]
328
- def tcp(callback = nil, **opts, &blk)
329
- TCP.new(@reactor, progress: callback || blk, **opts)
330
+ def tcp(**opts, &callback)
331
+ TCP.new(@reactor, progress: callback, **opts)
330
332
  end
331
333
 
332
334
  # Get a new UDP instance
333
335
  #
334
336
  # @return [::Libuv::UDP]
335
- def udp(callback = nil, **opts, &blk)
336
- UDP.new(@reactor, progress: callback || blk, **opts)
337
+ def udp(**opts, &callback)
338
+ UDP.new(@reactor, progress: callback, **opts)
337
339
  end
338
340
 
339
341
  # Get a new TTY instance
@@ -359,56 +361,62 @@ module Libuv
359
361
  #
360
362
  # @param callback [Proc] the callback to be called on timer trigger
361
363
  # @return [::Libuv::Timer]
362
- def timer(callback = nil, &blk)
363
- Timer.new(@reactor, callback || blk)
364
+ def timer
365
+ handle = Timer.new(@reactor)
366
+ handle.progress &Proc.new if block_given?
367
+ handle
364
368
  end
365
369
 
366
370
  # Get a new Prepare handle
367
371
  #
368
372
  # @return [::Libuv::Prepare]
369
- def prepare(callback = nil, &blk)
370
- Prepare.new(@reactor, callback || blk)
373
+ def prepare
374
+ handle = Prepare.new(@reactor)
375
+ handle.progress &Proc.new if block_given?
376
+ handle
371
377
  end
372
378
 
373
379
  # Get a new Check handle
374
380
  #
375
381
  # @return [::Libuv::Check]
376
- def check(callback = nil, &blk)
377
- Check.new(@reactor, callback || blk)
382
+ def check
383
+ handle = Check.new(@reactor)
384
+ handle.progress &Proc.new if block_given?
385
+ handle
378
386
  end
379
387
 
380
388
  # Get a new Idle handle
381
389
  #
382
390
  # @param callback [Proc] the callback to be called on idle trigger
383
391
  # @return [::Libuv::Idle]
384
- def idle(callback = nil, &block)
385
- Idle.new(@reactor, callback || block)
392
+ def idle
393
+ handle = Idle.new(@reactor)
394
+ handle.progress &Proc.new if block_given?
395
+ handle
386
396
  end
387
397
 
388
398
  # Get a new Async handle
389
399
  #
390
400
  # @return [::Libuv::Async]
391
- def async(callback = nil, &block)
392
- callback ||= block
401
+ def async
393
402
  handle = Async.new(@reactor)
394
- handle.progress callback if callback
403
+ handle.progress &Proc.new if block_given?
395
404
  handle
396
405
  end
397
406
 
398
407
  # Get a new signal handler
399
408
  #
400
409
  # @return [::Libuv::Signal]
401
- def signal(signum = nil, callback = nil, &block)
402
- callback ||= block
410
+ def signal(signum = nil)
403
411
  handle = Signal.new(@reactor)
404
- handle.progress callback if callback
412
+ handle.progress &Proc.new if block_given?
405
413
  handle.start(signum) if signum
406
414
  handle
407
415
  end
408
416
 
409
417
  # Allows user defined behaviour when sig int is received
410
- def on_program_interrupt(callback = nil, &block)
411
- @on_signal << (callback || block)
418
+ def on_program_interrupt(&callback)
419
+ @on_signal << callback
412
420
  self
413
421
  end
414
422
 
@@ -417,10 +425,9 @@ module Libuv
417
425
  # @param callback [Proc] the callback to be called in the thread pool
418
426
  # @return [::Libuv::Work]
419
427
  # @raise [ArgumentError] if block is not given
420
- def work(callback = nil, &block)
421
- callback ||= block
428
+ def work(&callback)
422
429
  assert_block(callback)
423
- Work.new(@reactor, callback) # Work is a promise object
430
+ Work.new(@reactor, &callback)
424
431
  end
425
432
 
426
433
  # Lookup a hostname
@@ -429,13 +436,13 @@ module Libuv
429
436
  # @param port [Integer, String] the service being connected too
430
437
  # @param callback [Proc] the callback to be called on success
431
438
  # @return [::Libuv::Dns]
432
- def lookup(hostname, hint = :IPv4, port = 9, wait: true, &block)
439
+ def lookup(hostname, hint = :IPv4, port = 9, wait: true)
433
440
  dns = Dns.new(@reactor, hostname, port, hint, wait: wait) # Work is a promise object
434
- if block_given? || !wait
435
- dns.then block
436
- dns
437
- else
441
+ if wait
438
442
  dns.results
443
+ else
444
+ dns.then &Proc.new if block_given?
445
+ dns
439
446
  end
440
447
  end
441
448
 
@@ -93,8 +93,8 @@ module Libuv
93
93
  end
94
94
 
95
95
  # Provide a callback once the TLS handshake has completed
96
- def on_handshake(callback = nil, &blk)
97
- @on_handshake = callback || blk
96
+ def on_handshake(&blk)
97
+ @on_handshake = blk
98
98
  self
99
99
  end
100
100
 
@@ -168,8 +168,8 @@ module Libuv
168
168
  end
169
169
 
170
170
  # Verify peers will be called for each cert in the chain
171
- def verify_peer(callback = nil, &blk)
172
- @on_verify = callback || blk
171
+ def verify_peer(&blk)
172
+ @on_verify = blk
173
173
  self
174
174
  end
175
175
 
@@ -187,7 +187,7 @@ module Libuv
187
187
 
188
188
  if wait
189
189
  return deferred.promise if wait == :promise
190
- co deferred.promise
190
+ deferred.promise.value
191
191
  end
192
192
 
193
193
  self
@@ -199,7 +199,7 @@ module Libuv
199
199
  alias_method :do_shutdown, :shutdown
200
200
  def shutdown
201
201
  if @pending_writes && @pending_writes.length > 0
202
- @pending_writes[-1][0].finally method(:do_shutdown)
202
+ @pending_writes[-1][0].finally { do_shutdown }
203
203
  else
204
204
  do_shutdown
205
205
  end
@@ -228,11 +228,11 @@ module Libuv
228
228
  # --------------------------------------
229
229
  #
230
230
 
231
- def bind(ip, port, callback = nil, **tls_options, &blk)
231
+ def bind(ip, port, **tls_options, &blk)
232
232
  return self if @closed
233
233
 
234
- @on_accept = callback || blk
235
- @on_listen = method(:accept)
234
+ @on_accept = blk
235
+ @on_listen = proc { accept }
236
236
 
237
237
  assert_type(String, ip, IP_ARGUMENT_ERROR)
238
238
  assert_type(Integer, port, PORT_ARGUMENT_ERROR)
@@ -249,27 +249,27 @@ module Libuv
249
249
  self
250
250
  end
251
251
 
252
- def open(fd, binding = true, callback = nil, &blk)
252
+ def open(fd, binding = true)
253
253
  return self if @closed
254
254
 
255
255
  if binding
256
- @on_listen = method(:accept)
257
- @on_accept = callback || blk
256
+ @on_listen = proc { accept }
257
+ @on_accept = Proc.new
258
+ elsif block_given?
259
+ @callback = Proc.new
258
260
  else
259
- @callback = callback || blk
260
- @coroutine = @reactor.defer if @callback.nil?
261
+ @coroutine = @reactor.defer
261
262
  end
262
263
  error = check_result ::Libuv::Ext.tcp_open(handle, fd)
263
264
  reject(error) if error
264
- co @coroutine.promise if @coroutine
265
+ @coroutine.promise.value if @coroutine
265
266
 
266
267
  self
267
268
  end
268
269
 
269
- def connect(ip, port, callback = nil, &blk)
270
+ def connect(ip, port)
270
271
  return self if @closed
271
272
 
272
- @callback = callback || blk
273
273
  assert_type(String, ip, IP_ARGUMENT_ERROR)
274
274
  assert_type(Integer, port, PORT_ARGUMENT_ERROR)
275
275
 
@@ -280,9 +280,11 @@ module Libuv
280
280
  reject(e)
281
281
  end
282
282
 
283
- if @callback.nil?
283
+ if block_given?
284
+ @callback = Proc.new
285
+ else
284
286
  @coroutine = @reactor.defer
285
- co @coroutine.promise
287
+ @coroutine.promise.value
286
288
  end
287
289
 
288
290
  self
@@ -380,7 +382,7 @@ module Libuv
380
382
  end
381
383
  end
382
384
 
383
- def accept(_)
385
+ def accept
384
386
  begin
385
387
  raise RuntimeError, CLOSED_HANDLE_ERROR if @closed
386
388
  tcp = TCP.new(reactor, handle, **@tls_options)
@@ -9,9 +9,8 @@ module Libuv
9
9
 
10
10
  # @param reactor [::Libuv::Reactor] reactor this timer will be associated
11
11
  # @param callback [Proc] callback to be called when the timer is triggered
12
- def initialize(reactor, callback = nil, &blk)
12
+ def initialize(reactor)
13
13
  @reactor = reactor
14
- @callback = callback || blk
15
14
 
16
15
  timer_ptr = ::Libuv::Ext.allocate_handle_timer
17
16
  error = check_result(::Libuv::Ext.timer_init(reactor.handle, timer_ptr))
@@ -86,9 +85,8 @@ module Libuv
86
85
  # Used to update the callback to be triggered by the timer
87
86
  #
88
87
  # @param callback [Proc] the callback to be called by the timer
89
- def progress(callback = nil, &blk)
90
- @callback = callback || blk
91
-
88
+ def progress(&callback)
89
+ @callback = callback
92
90
  self
93
91
  end
94
92
 
@@ -167,7 +167,7 @@ module Libuv
167
167
 
168
168
  if wait
169
169
  return deferred.promise if wait == :promise
170
- co deferred.promise
170
+ deferred.promise.value
171
171
  end
172
172
 
173
173
  self
@@ -217,8 +217,8 @@ module Libuv
217
217
  self
218
218
  end
219
219
 
220
- def progress(callback = nil, &blk)
221
- @progress = callback || blk
220
+ def progress(&callback)
221
+ @progress = callback
222
222
  self
223
223
  end
224
224
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Libuv
4
- VERSION = '3.3.0'
4
+ VERSION = '4.0.0'
5
5
  end
@@ -15,7 +15,7 @@ module Libuv
15
15
 
16
16
  # @param thread [::Libuv::Reactor] thread this work request will be associated
17
17
  # @param work [Proc] callback to be called in the thread pool
18
- def initialize(thread, work)
18
+ def initialize(thread, &work)
19
19
  super(thread, thread.defer)
20
20
 
21
21
  @work = work
@@ -54,9 +54,9 @@ describe Libuv::Async do
54
54
  callback.close
55
55
  end
56
56
 
57
- @reactor.work(proc {
57
+ @reactor.work {
58
58
  callback.call
59
- }).catch do |err|
59
+ }.catch do |err|
60
60
  @general_failure << err
61
61
  end
62
62
  }
@@ -31,10 +31,10 @@ if RUBY_PLATFORM != 'java'
31
31
  it "should wait for work to complete and return the result" do
32
32
  @reactor.run { |reactor|
33
33
 
34
- @log << co(@reactor.work(proc {
34
+ @log << @reactor.work {
35
35
  sleep 1
36
36
  'work done'
37
- }))
37
+ }.value
38
38
  @log << 'after work'
39
39
 
40
40
  @timeout.close
@@ -47,9 +47,9 @@ if RUBY_PLATFORM != 'java'
47
47
  it "should raise an error if the promise is rejected" do
48
48
  @reactor.run { |reactor|
49
49
  begin
50
- @log << co(@reactor.work(proc {
50
+ @log << @reactor.work {
51
51
  raise 'rejected'
52
- }))
52
+ }.value
53
53
  @log << 'after work'
54
54
  rescue => e
55
55
  @log << e.message
@@ -64,18 +64,18 @@ if RUBY_PLATFORM != 'java'
64
64
 
65
65
  it "should return the results of multiple promises" do
66
66
  @reactor.run { |reactor|
67
- job1 = @reactor.work(proc {
67
+ job1 = @reactor.work {
68
68
  sleep 1
69
69
  'job1'
70
- })
70
+ }
71
71
 
72
- job2 = @reactor.work(proc {
72
+ job2 = @reactor.work {
73
73
  sleep 1
74
74
  'job2'
75
- })
75
+ }
76
76
 
77
77
  # Job1 and Job2 are executed in parallel
78
- result1, result2 = co(job1, job2)
78
+ result1, result2 = ::Libuv.co(job1, job2)
79
79
 
80
80
  @log << result1
81
81
  @log << result2
@@ -88,12 +88,11 @@ if RUBY_PLATFORM != 'java'
88
88
  expect(@log).to eq(['job1', 'job2', 'after work'])
89
89
  end
90
90
 
91
-
92
91
  it "should provide a callback option for progress events" do
93
92
  @reactor.run { |reactor|
94
93
  timer = @reactor.timer
95
94
  timer.start(0)
96
- co(timer) do
95
+ ::Libuv.co(timer) do
97
96
  @log << 'in timer'
98
97
  timer.close # close will resolve the promise
99
98
  end
@@ -312,9 +312,9 @@ describe Libuv::Q do
312
312
 
313
313
  it "should allow registration of an errback without a success callback and reject" do
314
314
  @reactor.run {
315
- @promise.catch(proc {|reason|
315
+ @promise.catch { |reason|
316
316
  @log << reason
317
- })
317
+ }
318
318
 
319
319
  @deferred.reject(:foo)
320
320
  }
@@ -325,9 +325,9 @@ describe Libuv::Q do
325
325
 
326
326
  it "should allow registration of an errback without a success callback and resolve" do
327
327
  @reactor.run {
328
- @promise.catch(proc {|reason|
328
+ @promise.catch { |reason|
329
329
  @log << reason
330
- })
330
+ }
331
331
 
332
332
  @deferred.resolve(:foo)
333
333
  }
@@ -447,22 +447,22 @@ describe Libuv::Q do
447
447
  end
448
448
 
449
449
 
450
- @promise.progress(proc { |result|
450
+ @promise.progress { |result|
451
451
  @log << result
452
452
  :bar
453
- }).progress(proc { |result|
453
+ }.progress { |result|
454
454
  @log << result
455
455
  result
456
- }).progress(proc {|result|
456
+ }.progress { |result|
457
457
  @log << result
458
458
  result
459
- }).progress(proc {|result|
459
+ }.progress { |result|
460
460
  @log << result
461
461
  :done
462
- }).progress(proc { |result|
462
+ }.progress { |result|
463
463
  @log << result
464
464
  result
465
- })
465
+ }
466
466
 
467
467
 
468
468
  @deferred.notify(:foo)
@@ -479,23 +479,23 @@ describe Libuv::Q do
479
479
  end
480
480
 
481
481
 
482
- @promise.progress(proc { |result|
482
+ @promise.progress { |result|
483
483
  @log << result
484
484
  :bar
485
- }).progress(proc { |result|
485
+ }.progress { |result|
486
486
  @log << result
487
487
  raise 'err'
488
488
  result
489
- }).progress(proc {|result|
489
+ }.progress {|result|
490
490
  @log << result
491
491
  result
492
- }).progress(proc {|result|
492
+ }.progress {|result|
493
493
  @log << result
494
494
  :done
495
- }).progress(proc { |result|
495
+ }.progress { |result|
496
496
  @log << result
497
497
  result
498
- })
498
+ }
499
499
 
500
500
 
501
501
  @deferred.notify(:foo)
@@ -509,9 +509,9 @@ describe Libuv::Q do
509
509
  @reactor.run {
510
510
  @deferred.reject(:foo)
511
511
 
512
- @promise.catch(proc {|reason|
512
+ @promise.catch { |reason|
513
513
  @log << reason
514
- })
514
+ }
515
515
 
516
516
  @reactor.next_tick do
517
517
  @reactor.stop
@@ -543,10 +543,10 @@ describe Libuv::Q do
543
543
 
544
544
  it "should fulfill with the original value" do
545
545
  @reactor.run {
546
- @promise.finally(proc {
546
+ @promise.finally {
547
547
  @log << :finally
548
548
  :finally
549
- }).then do |result|
549
+ }.then do |result|
550
550
  @log << result
551
551
  end
552
552
 
@@ -559,25 +559,25 @@ describe Libuv::Q do
559
559
 
560
560
  it "should fulfill with the original value (larger test)" do
561
561
  @reactor.run {
562
- @promise.then(proc { |result|
562
+ @promise.then { |result|
563
563
  @log << result
564
564
  result
565
- }).finally(proc {
565
+ }.finally {
566
566
  @log << :finally
567
567
  :finally
568
- }).then(proc { |result|
568
+ }.then { |result|
569
569
  @log << result
570
570
  :change
571
- }).then(proc { |result|
571
+ }.then { |result|
572
572
  @log << result
573
573
  result
574
- }).finally(proc {
574
+ }.finally {
575
575
  @log << :finally
576
576
  :finally
577
- }).then(proc { |result|
577
+ }.then { |result|
578
578
  @log << result
579
579
  result
580
- })
580
+ }
581
581
 
582
582
 
583
583
  @deferred.resolve(:foo)
@@ -589,10 +589,10 @@ describe Libuv::Q do
589
589
  describe "when the callback throws an exception" do
590
590
  it "should reject with this new exception" do
591
591
  @reactor.run {
592
- @promise.finally(proc {
592
+ @promise.finally {
593
593
  @log << :finally
594
594
  raise 'error'
595
- }).catch do |reason|
595
+ }.catch do |reason|
596
596
  @log.push reason.is_a?(Exception)
597
597
  end
598
598
 
@@ -608,10 +608,10 @@ describe Libuv::Q do
608
608
  @reactor.run {
609
609
  deferred2 = @reactor.defer
610
610
 
611
- @promise.finally(proc {
611
+ @promise.finally {
612
612
  @log << :finally
613
613
  deferred2.promise
614
- }).then do |result|
614
+ }.then do |result|
615
615
  @log << result
616
616
  end
617
617
 
@@ -637,10 +637,10 @@ describe Libuv::Q do
637
637
  @reactor.run {
638
638
  deferred2 = @reactor.defer
639
639
 
640
- @promise.finally(proc {
640
+ @promise.finally {
641
641
  @log << :finally
642
642
  deferred2.promise
643
- }).catch do |result|
643
+ }.catch do |result|
644
644
  @log << result
645
645
  end
646
646
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libuv
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen von Takach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-18 00:00:00.000000000 Z
11
+ date: 2017-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi