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:, focus: nil) ⇒ Reporter

Returns a new instance of Reporter.



23
24
25
26
27
28
29
30
31
32
# File 'lib/require_profiler/reporter.rb', line 23

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

  start_processor
end

Instance Method Details

#finishObject



62
63
64
65
66
67
68
69
70
# File 'lib/require_profiler/reporter.rb', line 62

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



34
35
36
# File 'lib/require_profiler/reporter.rb', line 34

def handle_event(event)
  queue << event
end

#handle_event_sync(event) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/require_profiler/reporter.rb', line 38

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

    if parent
      node.parent = parent
      parent.children&.push(node)
    end

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

    last.focused! if focus && last.path.match?(focus)

    printer.flush(last) if stack.empty?

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