Last active
February 12, 2022 17:43
-
-
Save havenwood/ea5c27016ec2827f44b1bd667688f91f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'async' | |
class Enumerator | |
class Async | |
module Refinement | |
refine Enumerable do | |
def async | |
Enumerator::Async.new(self) | |
end | |
end | |
end | |
include Enumerable | |
def initialize(collection) | |
@enum = collection.to_enum | |
end | |
def eager | |
return @enum unless @enum.respond_to?(:eager) | |
@enum.eager | |
end | |
def lazy | |
@enum.lazy | |
end | |
def async | |
self | |
end | |
def each(&block) | |
Sync do |task| | |
@enum.each do |value| | |
task.async do | |
block.call(value) | |
end | |
end | |
end | |
self | |
end | |
end | |
end | |
using Enumerator::Async::Refinement | |
[1, 2, 3].async.map do |number| | |
sleep 2 | |
number + 42 | |
end |
@bruno- that makes sense and is really interesting. I hadn't thought of it being used inside an existing reactor. Thank you for your feedback! I updated the gist accordingly to use Sync
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Async
creates a new task if it runs within an existing reactor. So I would maybe change line 34 toSync do |task|
:Async
) if one doesn't yet existAsync
block) it will just run synchronously