Skip to content

Commit 1314eda

Browse files
committed
Better documentation.
1 parent 65ee9f6 commit 1314eda

File tree

4 files changed

+65
-19
lines changed

4 files changed

+65
-19
lines changed

lib/concurrent/agent.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,7 @@ class Agent
2424
#
2525
# @param [Object] initial the initial value
2626
#
27-
# @!macro [attach] executor_and_deref_options
28-
#
29-
# @param [Hash] opts the options used to define the behavior at update and deref
30-
# and to specify the executor on which to perform actions
31-
# @option opts [Executor] :executor when set use the given `Executor` instance.
32-
# Three special values are also supported: `:task` returns the global task pool,
33-
# `:operation` returns the global operation pool, and `:immediate` returns a new
34-
# `ImmediateExecutor` object.
35-
# @option opts [Boolean] :dup_on_deref (false) call `#dup` before returning the data
36-
# @option opts [Boolean] :freeze_on_deref (false) call `#freeze` before returning the data
37-
# @option opts [Proc] :copy_on_deref (nil) call the given `Proc` passing
38-
# the internal value and returning the value returned from the proc
27+
# @!macro executor_and_deref_options
3928
def initialize(initial, opts = {})
4029
@value = initial
4130
@rescuers = []

lib/concurrent/delay.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,20 @@ class Delay < Synchronization::Object
5252

5353
# Create a new `Delay` in the `:pending` state.
5454
#
55-
# @yield the delayed operation to perform
55+
# @!macro [attach] executor_and_deref_options
56+
#
57+
# @param [Hash] opts the options used to define the behavior at update and deref
58+
# and to specify the executor on which to perform actions
59+
# @option opts [Executor] :executor when set use the given `Executor` instance.
60+
# Three special values are also supported: `:task` returns the global task pool,
61+
# `:operation` returns the global operation pool, and `:immediate` returns a new
62+
# `ImmediateExecutor` object.
63+
# @option opts [Boolean] :dup_on_deref (false) call `#dup` before returning the data
64+
# @option opts [Boolean] :freeze_on_deref (false) call `#freeze` before returning the data
65+
# @option opts [Proc] :copy_on_deref (nil) call the given `Proc` passing
66+
# the internal value and returning the value returned from the proc
5667
#
57-
# @!macro executor_and_deref_options
68+
# @yield the delayed operation to perform
5869
#
5970
# @raise [ArgumentError] if no block is given
6071
def initialize(opts = {}, &block)

lib/concurrent/future.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def self.execute(opts = {}, &block)
7676
Future.new(opts, &block).execute
7777
end
7878

79+
# @!macro ivar_set_method
7980
def set(value = IVar::NO_VALUE, &block)
8081
check_for_block_or_value!(block_given?, value)
8182
mutex.synchronize do

lib/concurrent/promise.rb

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ class Promise < IVar
197197
# @option opts [object, Array] :args zero or more arguments to be passed
198198
# the task block on execution
199199
#
200+
# @yield The block operation to be performed asynchronously.
201+
#
200202
# @raise [ArgumentError] if no block is given
201203
#
202204
# @see http://wiki.commonjs.org/wiki/Promises/A
@@ -217,17 +219,37 @@ def initialize(opts = {}, &block)
217219
@children = []
218220
end
219221

220-
# @return [Promise]
222+
# Create a new `Promise` and fulfill it immediately.
223+
#
224+
# @!macro executor_and_deref_options
225+
#
226+
# @!macro promise_init_options
227+
#
228+
# @raise [ArgumentError] if no block is given
229+
#
230+
# @return [Promise] the newly created `Promise`
221231
def self.fulfill(value, opts = {})
222232
Promise.new(opts).tap { |p| p.send(:synchronized_set_state!, true, value, nil) }
223233
end
224234

225-
# @return [Promise]
235+
# Create a new `Promise` and reject it immediately.
236+
#
237+
# @!macro executor_and_deref_options
238+
#
239+
# @!macro promise_init_options
240+
#
241+
# @raise [ArgumentError] if no block is given
242+
#
243+
# @return [Promise] the newly created `Promise`
226244
def self.reject(reason, opts = {})
227245
Promise.new(opts).tap { |p| p.send(:synchronized_set_state!, false, nil, reason) }
228246
end
229247

230-
# @return [Promise]
248+
# Execute an `:unscheduled` `Promise`. Immediately sets the state to `:pending` and
249+
# passes the block to a new thread/thread pool for eventual execution.
250+
# Does nothing if the `Promise` is in any state other than `:unscheduled`.
251+
#
252+
# @return [Promise] a reference to `self`
231253
def execute
232254
if root?
233255
if compare_and_set_state(:pending, :unscheduled)
@@ -240,6 +262,9 @@ def execute
240262
self
241263
end
242264

265+
# @!macro ivar_set_method
266+
#
267+
# @raise [Concurrent::PromiseExecutionError] if not the root promise
243268
def set(value = IVar::NO_VALUE, &block)
244269
raise PromiseExecutionError.new('supported only on root promise') unless root?
245270
check_for_block_or_value!(block_given?, value)
@@ -253,6 +278,9 @@ def set(value = IVar::NO_VALUE, &block)
253278
execute
254279
end
255280

281+
# @!macro ivar_fail_method
282+
#
283+
# @raise [Concurrent::PromiseExecutionError] if not the root promise
256284
def fail(reason = StandardError.new)
257285
set { raise reason }
258286
end
@@ -275,6 +303,13 @@ def self.execute(opts = {}, &block)
275303
new(opts, &block).execute
276304
end
277305

306+
# Chain a new promise off the current promise.
307+
#
308+
# @param [Proc] rescuer An optional rescue block to be executed if the
309+
# promise is rejected.
310+
#
311+
# @yield The block operation to be performed asynchronously.
312+
#
278313
# @return [Promise] the new promise
279314
def then(rescuer = nil, &block)
280315
raise ArgumentError.new('rescuers and block are both missing') if rescuer.nil? && !block_given?
@@ -296,13 +331,23 @@ def then(rescuer = nil, &block)
296331
child
297332
end
298333

299-
# @return [Promise]
334+
# Chain onto this promise an action to be undertaken on success
335+
# (fulfillment).
336+
#
337+
# @yield The block to execute
338+
#
339+
# @return [Promise] self
300340
def on_success(&block)
301341
raise ArgumentError.new('no block given') unless block_given?
302342
self.then(&block)
303343
end
304344

305-
# @return [Promise]
345+
# Chain onto this promise an action to be undertaken on failure
346+
# (rejection).
347+
#
348+
# @yield The block to execute
349+
#
350+
# @return [Promise] self
306351
def rescue(&block)
307352
self.then(block)
308353
end

0 commit comments

Comments
 (0)