Class: Reacto::Operations::Chunk

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

Instance Method Summary collapse

Constructor Details

#initialize(func, executor: nil) ⇒ Chunk

Returns a new instance of Chunk.



8
9
10
11
# File 'lib/reacto/operations/chunk.rb', line 8

def initialize(func, executor: nil)
  @func = func
  @executor = executor
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/reacto/operations/chunk.rb', line 13

def call(tracker)
  @current_key = NO_VALUE
  @current_data = []

  value = ->(v) do
    key = @func.call(v)

    if key == nil || key == :_separator
      flush_current!(tracker)
      return
    end

    if key == :_alone
      flush_current!(tracker)
      tracker.on_value(LabeledTrackable.new(
        key, @executor, &Behaviours.single_value(v)
      ))

      return
    end

    if key.to_s.start_with?('_')
      flush_current!(tracker)
      tracker.on_error(RuntimeError.new(
        'symbols beginning with an underscore are reserved'
      ))
      return
    end

    if @current_key == NO_VALUE || @current_key == key
      @current_key = key
      @current_data << v
      return
    end

    flush_current!(tracker)

    @current_key = key
    @current_data = [v]
  end

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

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

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

#flush_current!(tracker) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/reacto/operations/chunk.rb', line 69

def flush_current!(tracker)
  if @current_key != NO_VALUE
    tracker.on_value(LabeledTrackable.new(
      @current_key, @executor, &Behaviours.enumerable(@current_data)
    ))
  end

  @current_key = NO_VALUE
  @current_data = []
end