Class: RequireProfiler::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/require_profiler/reporter.rb

Defined Under Namespace

Classes: Event, Node

Instance Method Summary collapse

Constructor Details

#initialize(printer:) ⇒ Reporter

Returns a new instance of Reporter.



17
18
19
20
21
22
23
24
25
# File 'lib/require_profiler/reporter.rb', line 17

def initialize(printer:)
  @stack = []
  @totals = {count: 0, time: 0.0}
  @printer = printer
  @processor = nil
  @queue = Queue.new

  start_processor
end

Instance Method Details

#finishObject



48
49
50
51
52
53
54
55
56
# File 'lib/require_profiler/reporter.rb', line 48

def finish
  handle_event(Event.new(type: :stop))
  processor.join

  warn "Finished in the middle of requiring a file" unless stack.empty?

  printer.finish
  totals
end

#handle_event(event) ⇒ Object



27
28
29
# File 'lib/require_profiler/reporter.rb', line 27

def handle_event(event)
  queue << event
end

#handle_event_sync(event) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/require_profiler/reporter.rb', line 31

def handle_event_sync(event)
  if event.type == :start
    node = Node.new(path: event.path, children: [])
    stack.last&.children&.push(node)

    stack << node
  elsif event.type == :end
    last = stack.pop
    last.time = event.time

    printer.flush(last) if stack.empty?

    totals[:count] += 1
    totals[:time] += event.time if stack.empty?
  end
end