Class: Reacto::Operations::FindIndex

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

Instance Method Summary collapse

Constructor Details

#initialize(predicate) ⇒ FindIndex

Returns a new instance of FindIndex.



6
7
8
# File 'lib/reacto/operations/find_index.rb', line 6

def initialize(predicate)
  @predicate = predicate
end

Instance Method Details

#call(tracker) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/reacto/operations/find_index.rb', line 10

def call(tracker)
  index = 0

  behaviour = -> (value) do
    found = @predicate.call(value)

    if found
      tracker.on_value(index)
      tracker.on_close
    else
      index += 1
    end
  end

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