Class: Core::Async::Enumerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Is::Async
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.



11
12
13
14
15
16
17
# File 'lib/core/async/enumerator.rb', line 11

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.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/core/async/enumerator.rb', line 21

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

  await do
    errored, stopped = false

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

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