Class: Pull::Take

Inherits:
Object
  • Object
show all
Defined in:
lib/pull/through/take.rb

Instance Method Summary collapse

Constructor Details

#initialize(limit) ⇒ Take



5
6
7
8
# File 'lib/pull/through/take.rb', line 5

def initialize(limit)
  @limit = limit
  @index = 0
end

Instance Method Details

#call(read) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pull/through/take.rb', line 10

def call(read)
  -> (finish, callback) {
    if finish
      on_abort.()
      return nil
    end

    read.(nil, -> (value) {
      return nil if @index >= @limit
      callback.(value)
      @index += 1
    })
  }
end