Class: Reacto::Operations::Append

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to_append, condition: nil) ⇒ Append

Returns a new instance of Append.



8
9
10
11
# File 'lib/reacto/operations/append.rb', line 8

def initialize(to_append, condition: nil)
  @to_append = to_append
  @condition = condition
end

Instance Attribute Details

#to_appendObject (readonly)

Returns the value of attribute to_append.



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

def to_append
  @to_append
end

Instance Method Details

#call(tracker) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/reacto/operations/append.rb', line 13

def call(tracker)
  empty = true

  on_value =
    if @condition == :source_empty
      -> (v) do
        empty = false if empty
        tracker.on_value(v)
      end
    else
      tracker.method(:on_value)
    end

  on_close = -> () do
    if (@condition == :source_empty && empty) || @condition.nil?
      if to_append.respond_to? :each
        to_append.each { |v| tracker.on_value(v) }
      else
        tracker.on_value(to_append)
      end
    end

    tracker.on_close
  end

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