@@ -76,12 +76,23 @@ def schedule(intended_time, default_executor = :io, &task)
76
76
ScheduledPromise . new ( default_executor , intended_time ) . future . then ( &task )
77
77
end
78
78
79
- # Constructs new {Future} which is completed after all futures are complete. Its value is array
80
- # of dependent future values. If there is an error it fails with the first one.
81
- # @param [Event] futures
79
+ # Constructs new {Future} which is completed after all futures_and_or_events are complete. Its value is array
80
+ # of dependent future values. If there is an error it fails with the first one. Event does not
81
+ # have a value so it's represented by nil in the array of values.
82
+ # @param [Event] futures_and_or_events
82
83
# @return [Future]
83
- def zip ( *futures )
84
- ZipPromise . new ( futures , :io ) . future
84
+ def zip_futures ( *futures_and_or_events )
85
+ ZipFuturesPromise . new ( futures_and_or_events , :io ) . future
86
+ end
87
+
88
+ alias_method :zip , :zip_futures
89
+
90
+ # Constructs new {Event} which is completed after all futures_and_or_events are complete
91
+ # (Future is completed when Success or Failed).
92
+ # @param [Event] futures_and_or_events
93
+ # @return [Event]
94
+ def zip_events ( *futures_and_or_events )
95
+ ZipEventsPromise . new ( futures_and_or_events , :io ) . future
85
96
end
86
97
87
98
# Constructs new {Future} which is completed after first of the futures is complete.
@@ -670,7 +681,7 @@ def schedule(intended_time)
670
681
# Zips with selected value form the suplied channels
671
682
# @return [Future]
672
683
def then_select ( *channels )
673
- ZipPromise . new ( [ self , Concurrent . select ( *channels ) ] , @DefaultExecutor ) . future
684
+ ZipFuturesPromise . new ( [ self , Concurrent . select ( *channels ) ] , @DefaultExecutor ) . future
674
685
end
675
686
676
687
# Changes default executor for rest of the chain
@@ -1256,62 +1267,54 @@ def on_completable(done_future)
1256
1267
end
1257
1268
1258
1269
# @!visibility private
1259
- class ZipPromise < BlockedPromise
1270
+ class ZipFuturesPromise < BlockedPromise
1260
1271
1261
1272
private
1262
1273
1263
1274
def initialize ( blocked_by_futures , default_executor )
1264
- klass = Event
1265
- blocked_by_futures . each do |f |
1266
- if f . is_a? ( Future )
1267
- if klass == Event
1268
- klass = Future
1269
- break
1270
- end
1271
- end
1272
- end
1273
-
1274
- # noinspection RubyArgCount
1275
- super ( klass . new ( self , default_executor ) , blocked_by_futures , blocked_by_futures . size )
1275
+ super ( Future . new ( self , default_executor ) , blocked_by_futures , blocked_by_futures . size )
1276
1276
1277
- if blocked_by_futures . empty?
1278
- on_completable nil
1279
- end
1277
+ on_completable nil if blocked_by_futures . empty?
1280
1278
end
1281
1279
1282
1280
def on_completable ( done_future )
1283
1281
all_success = true
1284
- values = [ ]
1285
- reasons = [ ]
1286
-
1287
- blocked_by . each do |future |
1288
- next unless future . is_a? ( Future )
1289
- success , value , reason = future . result
1282
+ values = Array . new ( blocked_by . size )
1283
+ reasons = Array . new ( blocked_by . size )
1290
1284
1291
- unless success
1292
- all_success = false
1285
+ blocked_by . each_with_index do |future , i |
1286
+ if future . is_a? ( Future )
1287
+ success , values [ i ] , reasons [ i ] = future . result
1288
+ all_success &&= success
1289
+ else
1290
+ values [ i ] = reasons [ i ] = nil
1293
1291
end
1294
-
1295
- values << value
1296
- reasons << reason
1297
1292
end
1298
1293
1299
1294
if all_success
1300
- if values . empty?
1301
- complete_with Event ::COMPLETED
1302
- else
1303
- if values . size == 1
1304
- complete_with Future ::Success . new ( values . first )
1305
- else
1306
- complete_with Future ::SuccessArray . new ( values )
1307
- end
1308
- end
1295
+ complete_with Future ::SuccessArray . new ( values )
1309
1296
else
1310
1297
complete_with Future ::PartiallyFailed . new ( values , reasons )
1311
1298
end
1312
1299
end
1313
1300
end
1314
1301
1302
+ # @!visibility private
1303
+ class ZipEventsPromise < BlockedPromise
1304
+
1305
+ private
1306
+
1307
+ def initialize ( blocked_by_futures , default_executor )
1308
+ super ( Event . new ( self , default_executor ) , blocked_by_futures , blocked_by_futures . size )
1309
+
1310
+ on_completable nil if blocked_by_futures . empty?
1311
+ end
1312
+
1313
+ def on_completable ( done_future )
1314
+ complete_with Event ::COMPLETED
1315
+ end
1316
+ end
1317
+
1315
1318
# @!visibility private
1316
1319
class AnyPromise < BlockedPromise
1317
1320
0 commit comments