Skip to content

Commit 59f9e46

Browse files
committed
Documentation and hiding constants
1 parent 75b8186 commit 59f9e46

File tree

1 file changed

+55
-42
lines changed

1 file changed

+55
-42
lines changed

lib/concurrent/edge/promises.rb

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def future(*args, &task)
3131
future_on(:io, *args, &task)
3232
end
3333

34+
# As {#future} but takes default_executor as first argument
3435
def future_on(default_executor, *args, &task)
3536
ImmediateEventPromise.new(default_executor).future.then(*args, &task)
3637
end
@@ -68,6 +69,7 @@ def delay(*args, &task)
6869
delay_on :io, *args, &task
6970
end
7071

72+
# As {#delay} but takes default_executor as first argument
7173
def delay_on(default_executor, *args, &task)
7274
DelayPromise.new(default_executor).future.then(*args, &task)
7375
end
@@ -79,6 +81,7 @@ def schedule(intended_time, *args, &task)
7981
schedule_on :io, intended_time, *args, &task
8082
end
8183

84+
# As {#schedule} but takes default_executor as first argument
8285
def schedule_on(default_executor, intended_time, *args, &task)
8386
ScheduledPromise.new(default_executor, intended_time).future.then(*args, &task)
8487
end
@@ -92,6 +95,7 @@ def zip_futures(*futures_and_or_events)
9295
zip_futures_on :io, *futures_and_or_events
9396
end
9497

98+
# As {#zip_futures} but takes default_executor as first argument
9599
def zip_futures_on(default_executor, *futures_and_or_events)
96100
ZipFuturesPromise.new(futures_and_or_events, default_executor).future
97101
end
@@ -106,6 +110,7 @@ def zip_events(*futures_and_or_events)
106110
zip_events_on :io, *futures_and_or_events
107111
end
108112

113+
# As {#zip_events} but takes default_executor as first argument
109114
def zip_events_on(default_executor, *futures_and_or_events)
110115
ZipEventsPromise.new(futures_and_or_events, default_executor).future
111116
end
@@ -117,6 +122,7 @@ def any_complete_future(*futures)
117122
any_complete_future_on :io, *futures
118123
end
119124

125+
# As {#any_complete_future} but takes default_executor as first argument
120126
def any_complete_future_on(default_executor, *futures)
121127
AnyCompleteFuturePromise.new(futures, default_executor).future
122128
end
@@ -131,14 +137,17 @@ def any_successful_future(*futures)
131137
any_successful_future_on :io, *futures
132138
end
133139

140+
# As {#any_succesful_future} but takes default_executor as first argument
134141
def any_successful_future_on(default_executor, *futures)
135142
AnySuccessfulFuturePromise.new(futures, default_executor).future
136143
end
137144

145+
# Constructs new {Event} which becomes complete after first if the events completes.
138146
def any_event(*events)
139147
any_event_on :io, *events
140148
end
141149

150+
# As {#any_event} but takes default_executor as first argument
142151
def any_event_on(default_executor, *events)
143152
AnyCompleteEventPromise.new(events, default_executor).event
144153
end
@@ -147,14 +156,7 @@ def any_event_on(default_executor, *events)
147156
# TODO consider adding zip_by(slice, *futures) processing futures in slices
148157
end
149158

150-
# Represents an event which will happen in future (will be completed). It has to always happen.
151-
class Event < Synchronization::Object
152-
safe_initialization!
153-
private(*attr_atomic(:internal_state))
154-
# @!visibility private
155-
public :internal_state
156-
include Concern::Logging
157-
159+
module InternalStates
158160
class State
159161
def completed?
160162
raise NotImplementedError
@@ -238,13 +240,17 @@ def to_sym
238240
end
239241
end
240242

243+
private_constant :Success
244+
241245
# @!visibility private
242246
class SuccessArray < Success
243247
def apply(args, block)
244248
block.call(*value, *args)
245249
end
246250
end
247251

252+
private_constant :SuccessArray
253+
248254
# @!visibility private
249255
class Failed < CompletedWithResult
250256
def initialize(reason)
@@ -272,6 +278,8 @@ def apply(args, block)
272278
end
273279
end
274280

281+
private_constant :Failed
282+
275283
# @!visibility private
276284
class PartiallyFailed < CompletedWithResult
277285
def initialize(value, reason)
@@ -301,12 +309,26 @@ def apply(args, block)
301309
end
302310
end
303311

312+
private_constant :PartiallyFailed
304313

305-
# @!visibility private
306314
PENDING = Pending.new
307-
# @!visibility private
308315
COMPLETED = Success.new(nil)
309316

317+
private_constant :PENDING, :COMPLETED
318+
end
319+
320+
private_constant :InternalStates
321+
322+
# Represents an event which will happen in future (will be completed). It has to always happen.
323+
class Event < Synchronization::Object
324+
safe_initialization!
325+
private(*attr_atomic(:internal_state))
326+
# @!visibility private
327+
public :internal_state
328+
329+
include Concern::Logging
330+
include InternalStates
331+
310332
def initialize(promise, default_executor)
311333
super()
312334
@Lock = Mutex.new
@@ -913,6 +935,7 @@ def with_hidden_completable
913935
# @abstract
914936
class AbstractPromise < Synchronization::Object
915937
safe_initialization!
938+
include InternalStates
916939
include Concern::Logging
917940

918941
def initialize(future)
@@ -953,12 +976,12 @@ def complete_with(new_state, raise_on_reassign = true)
953976

954977
# @return [Future]
955978
def evaluate_to(*args, block)
956-
complete_with Future::Success.new(block.call(*args))
979+
complete_with Success.new(block.call(*args))
957980
rescue StandardError => error
958-
complete_with Future::Failed.new(error)
981+
complete_with Failed.new(error)
959982
rescue Exception => error
960983
log(ERROR, 'Promises::Future', error)
961-
complete_with Future::Failed.new(error)
984+
complete_with Failed.new(error)
962985
end
963986
end
964987

@@ -973,35 +996,24 @@ def initialize(default_executor)
973996
super CompletableFuture.new(self, default_executor)
974997
end
975998

976-
# Set the `Future` to a value and wake or notify all threads waiting on it.
977-
#
978-
# @param [Object] value the value to store in the `Future`
979-
# @raise [Concurrent::MultipleAssignmentError] if the `Future` has already been set or otherwise completed
980-
# @return [Future]
981999
def success(value)
982-
complete_with Future::Success.new(value)
1000+
complete_with Success.new(value)
9831001
end
9841002

9851003
def try_success(value)
986-
!!complete_with(Future::Success.new(value), false)
1004+
!!complete_with(Success.new(value), false)
9871005
end
9881006

989-
# Set the `Future` to failed due to some error and wake or notify all threads waiting on it.
990-
#
991-
# @param [Object] reason for the failure
992-
# @raise [Concurrent::MultipleAssignmentError] if the `Future` has already been set or otherwise completed
993-
# @return [Future]
9941007
def fail(reason = StandardError.new)
995-
complete_with Future::Failed.new(reason)
1008+
complete_with Failed.new(reason)
9961009
end
9971010

9981011
def try_fail(reason = StandardError.new)
999-
!!complete_with(Future::Failed.new(reason), false)
1012+
!!complete_with(Failed.new(reason), false)
10001013
end
10011014

10021015
public :evaluate_to
10031016

1004-
# @return [Future]
10051017
def evaluate_to!(*args, block)
10061018
evaluate_to(*args, block).wait!
10071019
end
@@ -1152,14 +1164,14 @@ def on_completable(done_future)
11521164
# will be immediately completed
11531165
class ImmediateEventPromise < InnerPromise
11541166
def initialize(default_executor)
1155-
super Event.new(self, default_executor).complete_with(Event::COMPLETED)
1167+
super Event.new(self, default_executor).complete_with(COMPLETED)
11561168
end
11571169
end
11581170

11591171
class ImmediateFuturePromise < InnerPromise
11601172
def initialize(default_executor, success, value, reason)
11611173
super Future.new(self, default_executor).
1162-
complete_with(success ? Future::Success.new(value) : Future::Failed.new(reason))
1174+
complete_with(success ? Success.new(value) : Failed.new(reason))
11631175
end
11641176
end
11651177

@@ -1226,7 +1238,7 @@ def initialize(event1, event2, default_executor)
12261238
end
12271239

12281240
def on_completable(done_future)
1229-
complete_with Event::COMPLETED
1241+
complete_with COMPLETED
12301242
end
12311243
end
12321244

@@ -1253,9 +1265,9 @@ def on_completable(done_future)
12531265
success2, value2, reason2 = @Future2Result.result
12541266
success = success1 && success2
12551267
new_state = if success
1256-
Future::SuccessArray.new([value1, value2])
1268+
SuccessArray.new([value1, value2])
12571269
else
1258-
Future::PartiallyFailed.new([value1, value2], [reason1, reason2])
1270+
PartiallyFailed.new([value1, value2], [reason1, reason2])
12591271
end
12601272
complete_with new_state
12611273
end
@@ -1267,7 +1279,7 @@ def initialize(event, default_executor)
12671279
end
12681280

12691281
def on_completable(done_future)
1270-
complete_with Event::COMPLETED
1282+
complete_with COMPLETED
12711283
end
12721284
end
12731285

@@ -1306,9 +1318,9 @@ def on_completable(done_future)
13061318
end
13071319

13081320
if all_success
1309-
complete_with Future::SuccessArray.new(values)
1321+
complete_with SuccessArray.new(values)
13101322
else
1311-
complete_with Future::PartiallyFailed.new(values, reasons)
1323+
complete_with PartiallyFailed.new(values, reasons)
13121324
end
13131325
end
13141326
end
@@ -1324,10 +1336,11 @@ def initialize(blocked_by_futures, default_executor)
13241336
end
13251337

13261338
def on_completable(done_future)
1327-
complete_with Event::COMPLETED
1339+
complete_with COMPLETED
13281340
end
13291341
end
13301342

1343+
# @abstract
13311344
class AbstractAnyPromise < BlockedPromise
13321345
def touch
13331346
blocked_by.each(&:touch) unless @Future.completed?
@@ -1364,7 +1377,7 @@ def completable?(countdown, future)
13641377
end
13651378

13661379
def on_completable(done_future)
1367-
complete_with Event::COMPLETED, false
1380+
complete_with COMPLETED, false
13681381
end
13691382
end
13701383

@@ -1373,14 +1386,15 @@ class AnySuccessfulFuturePromise < AnyCompleteFuturePromise
13731386
private
13741387

13751388
def completable?(countdown, future)
1376-
future.success? || super(countdown, future)
1389+
future.success? ||
1390+
# inlined super from BlockedPromise
13771391
countdown.zero?
13781392
end
13791393
end
13801394

13811395
class DelayPromise < InnerPromise
13821396
def touch
1383-
@Future.complete_with Event::COMPLETED
1397+
@Future.complete_with COMPLETED
13841398
end
13851399

13861400
private
@@ -1390,7 +1404,6 @@ def initialize(default_executor)
13901404
end
13911405
end
13921406

1393-
# will be evaluated to task in intended_time
13941407
class ScheduledPromise < InnerPromise
13951408
def intended_time
13961409
@IntendedTime
@@ -1418,7 +1431,7 @@ def initialize(default_executor, intended_time)
14181431
end
14191432

14201433
Concurrent.global_timer_set.post(in_seconds) do
1421-
@Future.complete_with Event::COMPLETED
1434+
@Future.complete_with COMPLETED
14221435
end
14231436
end
14241437
end

0 commit comments

Comments
 (0)