Class: Reacto::Operations::ChunkWhile

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

Instance Method Summary collapse

Constructor Details

#initialize(func, executor: nil) ⇒ ChunkWhile

Returns a new instance of ChunkWhile.



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

def initialize(func, executor: nil)
  @func = func
  @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
38
39
40
41
42
43
44
45
46
47
# File 'lib/reacto/operations/chunk_while.rb', line 12

def call(tracker)
  current_data = []
  prev_value = NO_VALUE

  value = ->(v) do
    if prev_value == NO_VALUE
      prev_value = v
      current_data << v

      return
    end

    should_continue = @func.call(prev_value, v)
    prev_value = v

    unless should_continue
      flush_current(tracker, current_data)
      current_data = []
    end
    current_data << v
  end

  error = ->(e) do
    flush_current(tracker, current_data)
    tracker.on_error(e)
  end

  close = ->() do
    flush_current(tracker, current_data)
    tracker.on_close
  end

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

#flush_current(tracker, current_data) ⇒ Object



49
50
51
52
53
# File 'lib/reacto/operations/chunk_while.rb', line 49

def flush_current(tracker, current_data)
  tracker.on_value(
    Trackable.enumerable(current_data, executor: @executor)
  )
end