@@ -76,12 +76,23 @@ def schedule(intended_time, default_executor = :io, &task)
7676 ScheduledPromise . new ( default_executor , intended_time ) . future . then ( &task )
7777 end
7878
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
8283 # @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
8596 end
8697
8798 # Constructs new {Future} which is completed after first of the futures is complete.
@@ -670,7 +681,7 @@ def schedule(intended_time)
670681 # Zips with selected value form the suplied channels
671682 # @return [Future]
672683 def then_select ( *channels )
673- ZipPromise . new ( [ self , Concurrent . select ( *channels ) ] , @DefaultExecutor ) . future
684+ ZipFuturesPromise . new ( [ self , Concurrent . select ( *channels ) ] , @DefaultExecutor ) . future
674685 end
675686
676687 # Changes default executor for rest of the chain
@@ -1256,62 +1267,54 @@ def on_completable(done_future)
12561267 end
12571268
12581269 # @!visibility private
1259- class ZipPromise < BlockedPromise
1270+ class ZipFuturesPromise < BlockedPromise
12601271
12611272 private
12621273
12631274 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 )
12761276
1277- if blocked_by_futures . empty?
1278- on_completable nil
1279- end
1277+ on_completable nil if blocked_by_futures . empty?
12801278 end
12811279
12821280 def on_completable ( done_future )
12831281 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 )
12901284
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
12931291 end
1294-
1295- values << value
1296- reasons << reason
12971292 end
12981293
12991294 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 )
13091296 else
13101297 complete_with Future ::PartiallyFailed . new ( values , reasons )
13111298 end
13121299 end
13131300 end
13141301
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+
13151318 # @!visibility private
13161319 class AnyPromise < BlockedPromise
13171320
0 commit comments