Class: Reacto::Operations::FlatMapLatest

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

Instance Method Summary collapse

Constructor Details

#initialize(transform) ⇒ FlatMapLatest

Returns a new instance of FlatMapLatest.



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

def initialize(transform)
  @transform = transform
  @last_active = nil
end

Instance Method Details

#call(tracker) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/reacto/operations/flat_map_latest.rb', line 11

def call(tracker)
  subscription = Subscriptions::FlatMapSubscription.new(tracker)
  value = lambda do |v|
    trackable = @transform.call(v)

    @last_active.unsubscribe if @last_active

    sub = subscription.subscription!
    trackable.do_track sub
    @last_active = sub
  end

  close = lambda do
    subscription.source_closed = true
    return unless subscription.closed?

    tracker.on_close
  end

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