Class: Reacto::Operations::Partition

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

Instance Method Summary collapse

Constructor Details

#initialize(predicate, executor: nil) ⇒ Partition

Returns a new instance of Partition.



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

def initialize(predicate, executor: nil)
  @predicate = predicate
  @executor = executor
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
# File 'lib/reacto/operations/partition.rb', line 12

def call(tracker)
  true_array = []
  false_array = []

  behavior = -> (val) do
    if @predicate.call(val)
      true_array << val
    else
      false_array << val
    end
  end

  error = -> (e) do
    emit_trackables(tracker, true_array, false_array)
    tracker.on_error(e)
  end

  close = -> () do
    emit_trackables(tracker, true_array, false_array)
    tracker.on_close
  end

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

#emit_trackables(tracker, true_array, false_array) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/reacto/operations/partition.rb', line 39

def emit_trackables(tracker, true_array, false_array)
  true_trackable = LabeledTrackable.new(
    true, @executor, &Behaviours.enumerable(true_array)
  )
  false_trackable = LabeledTrackable.new(
    false, @executor, &Behaviours.enumerable(false_array)
  )

  tracker.on_value(true_trackable)
  tracker.on_value(false_trackable)
end