Class: Reacto::Operations::BlockingEnumerable

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

Instance Method Summary collapse

Constructor Details

#initialize(method_name, block) ⇒ BlockingEnumerable

Returns a new instance of BlockingEnumerable.



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

def initialize(method_name, block)
  @method_name = method_name
  @block = block
end

Instance Method Details

#call(tracker) ⇒ Object



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

def call(tracker)
  data = []

  value = -> (val) { data << val }
  close = -> do
    emit(tracker, data)
    tracker.on_close
  end
  error = ->(e) do
    emit(tracker, data)
    tracker.on_error(e)
  end

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

#emit(tracker, data) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/reacto/operations/blocking_enumerable.rb', line 29

def emit(tracker, data)
  result = data.send(@method_name, &@block)

  if result.is_a?(Enumerable)
    result.each { |value| tracker.on_value(value) }
  else
    tracker.on_value(result)
  end
end