Class: Reacto::Operations::Take

Inherits:
Object
  • Object
show all
Defined in:
lib/reacto/operations/take.rb

Instance Method Summary collapse

Constructor Details

#initialize(how_many_to_take) ⇒ Take

Returns a new instance of Take.



6
7
8
9
10
11
12
# File 'lib/reacto/operations/take.rb', line 6

def initialize(how_many_to_take)
  if how_many_to_take < 0
    raise ArgumentError.new('Attempt to take negative size!')
  end

  @how_many_to_take = how_many_to_take
end

Instance Method Details

#call(tracker) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/reacto/operations/take.rb', line 14

def call(tracker)
  taken = 0
  closed = false

  behaviour = -> (value) do
    return if closed
    if taken < @how_many_to_take
      tracker.on_value(value)
      taken += 1
    else
      closed = true
      tracker.on_close
    end
  end

  Subscriptions::OperationSubscription.new(tracker, value: behaviour)
end