Class: Core::Async::Enumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Is::Async, Is::Inspectable
Defined in:
lib/core/async/enumerator.rb

Instance Method Summary collapse

Methods included from Is::Async

#async

Constructor Details

#initialize(object) ⇒ Enumerator

Returns a new instance of Enumerator.



14
15
16
17
18
19
20
# File 'lib/core/async/enumerator.rb', line 14

def initialize(object)
  unless object.respond_to?(:each)
    raise ArgumentError, "object is not enumerable"
  end

  @object = object
end

Instance Method Details

#eachObject

public

Yields each value within its own async context, waiting on the enumeration to complete.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/core/async/enumerator.rb', line 24

def each
  unless block_given?
    return to_enum(:each)
  end

  errored = false
  stopped = false
  futures = []

  @object.each do |value|
    break if errored || stopped

    futures << async do
      yield value
    rescue LocalJumpError
      stopped = true
    rescue => error
      errored = true
      raise error
    end
  end

  futures.each(&:wait)
end