Class: Reacto::Operations::TakeWhile

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

Instance Method Summary collapse

Constructor Details

#initialize(predicate) ⇒ TakeWhile

Returns a new instance of TakeWhile.



6
7
8
# File 'lib/reacto/operations/take_while.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/take_while.rb', line 10

def call(tracker)
  closed = false

  behaviour = -> (value) do
    return if closed

    if @predicate.call(value)
      tracker.on_value(value)
    else
      closed = true
      tracker.on_close
    end
  end

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