module Enumerable
Provide asynchronous methods for enumerables.
Definitions
def async_map(parent: nil, &block)
This method is used to map the elements of an enumerable collection asynchronously.
Signature
-
parameter
parent
Interface(:async)
The parent to use for creating new tasks.
-
yields
{|item| ...}
The block to execute for each element in the collection.
Implementation
def async_map(parent: nil, &block)
Sync do |task|
parent ||= task
self.map do |*arguments|
parent.async(finished: false) do
yield(*arguments)
end
end.map(&:wait)
end
end
def async_each(parent: nil, &block)
This method is used to iterate over the elements of an enumerable collection asynchronously.
Signature
-
parameter
parent
Interface(:async)
The parent to use for creating new tasks.
-
yields
{|item| ...}
The block to execute for each element in the collection.
Implementation
def async_each(parent: nil, &block)
Sync do |task|
parent ||= task
self.each do |*arguments|
parent.async do
yield(*arguments)
end
end
end
return self
end