Class: Reacto::Operations::GroupByLabel

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

Instance Method Summary collapse

Constructor Details

#initialize(chose_label, executor = nil) ⇒ GroupByLabel

Returns a new instance of GroupByLabel.



6
7
8
9
10
11
# File 'lib/reacto/operations/group_by_label.rb', line 6

def initialize(chose_label, executor = nil)
  @chose_label = chose_label
  @executor = executor

  @labeled_values = {}
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
# File 'lib/reacto/operations/group_by_label.rb', line 13

def call(tracker)
  value = lambda do |v|
    label, val = @chose_label.call(v)

    @labeled_values[label] ||= []
    @labeled_values[label] << val
  end

  close = lambda do
    emit_values(tracker)
    tracker.on_close
  end

  error = lambda do |err|
    emit_values(tracker)
    tracker.on_error(err)
  end

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

#emit_values(tracker) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/reacto/operations/group_by_label.rb', line 36

def emit_values(tracker)
  @labeled_values.each do |label, values|
    trackable =
      LabeledTrackable.new(label, @executor) do |subscriber|
        values.each { |val| subscriber.on_value(val) }
        subscriber.on_close
      end
    tracker.on_value(trackable)
  end
end