Class: Reacto::Operations::Inject

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

Instance Method Summary collapse

Constructor Details

#initialize(injector, initial = NO_VALUE) ⇒ Inject

Returns a new instance of Inject.



7
8
9
10
# File 'lib/reacto/operations/inject.rb', line 7

def initialize(injector, initial = NO_VALUE)
  @injector = injector
  @initial = initial
end

Instance Method Details

#call(tracker) ⇒ Object



12
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
42
43
44
45
46
# File 'lib/reacto/operations/inject.rb', line 12

def call(tracker)
  @current = @initial
  @has_values = false

  inject = -> (v) do
    if @current == NO_VALUE
      @current = v
    else
      @current = @injector.call(@current, v)
    end

    @has_values = true
    tracker.on_value(@current)
  end

  close = -> () do
    unless @has_values || @current == NO_VALUE
      tracker.on_value(@current)
    end

    tracker.on_close
  end

  error = -> (e) do
    unless @has_values || @current == NO_VALUE
      tracker.on_value(@current)
    end

    tracker.on_error(e)
  end

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