Class: ViewComponentDevtools::State::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/view_component_devtools/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_id:, configuration:) ⇒ Session

Returns a new instance of Session.



49
50
51
52
53
54
55
56
57
# File 'lib/view_component_devtools/state.rb', line 49

def initialize(request_id:, configuration:)
  @request_id = request_id
  @configuration = configuration
  @components = []
  @component_stack = []
  @event_stack = []
  @component_count = 0
  @normalizer = Normalizer.new(configuration)
end

Instance Attribute Details

#request_idObject (readonly)

Returns the value of attribute request_id.



47
48
49
# File 'lib/view_component_devtools/state.rb', line 47

def request_id
  @request_id
end

Instance Method Details

#finish_event(event_id, payload, finish_time) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/view_component_devtools/state.rb', line 77

def finish_event(event_id, payload, finish_time)
  entry = event_stack.pop
  raise "ViewComponent DevTools event stack mismatch" unless entry&.fetch(:event_id) == event_id

  node = entry[:node]
  return unless node

  node[:durationMs] = ((finish_time - node.delete(:startedAt)) * 1_000).round(3)
  node[:templateLocation] = payload[:view_identifier] if payload[:view_identifier]
end

#payloadObject



88
89
90
91
92
93
94
95
96
# File 'lib/view_component_devtools/state.rb', line 88

def payload
  {
    version: 2,
    requestId: request_id,
    generatedAt: Time.now.utc.iso8601(3),
    truncated: false,
    components:
  }
end

#pop_component(metadata) ⇒ Object



63
64
65
66
# File 'lib/view_component_devtools/state.rb', line 63

def pop_component()
  actual = component_stack.pop
  raise "ViewComponent DevTools component stack mismatch" unless actual.equal?()
end

#push_component(metadata) ⇒ Object



59
60
61
# File 'lib/view_component_devtools/state.rb', line 59

def push_component()
  component_stack << 
end

#start_event(event_id, payload, start_time) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/view_component_devtools/state.rb', line 68

def start_event(event_id, payload, start_time)
  node = build_node(payload, start_time)
  event_stack << {event_id:, node:}
  return unless node

  parent = event_stack.reverse_each.drop(1).find { |entry| entry[:node] }&.fetch(:node)
  (parent ? parent[:children] : components) << node
end