Skip to content

Commit 3997fc1

Browse files
rshestfacebook-github-bot
authored andcommitted
Lazy init for event name mapping registry
Summary: ## Changelog: [Internal] - Noticed this when setting up C++ unit tests for WebPerformance - the change in this diff both makes sure there is no "global constructor" warning generated when `-Wglobal-constructors` is enabled, and also makes this bit potentially a little bit more efficient, as we don't pay for the registry construction if we don't use the WebPerformance API. Reviewed By: christophpurrer Differential Revision: D43663521 fbshipit-source-id: 59952f2415f49bb455a3443b3bfd8971108ac72b
1 parent a0adf57 commit 3997fc1

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

Libraries/WebPerformance/PerformanceEntryReporter.cpp

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -218,53 +218,58 @@ struct StrKeyHash {
218218
// https://www.w3.org/TR/event-timing/#sec-events-exposed
219219
// Not all of these are currently supported by RN, but we map them anyway for
220220
// future-proofing.
221-
static const std::unordered_map<StrKey, const char *, StrKeyHash>
222-
SUPPORTED_EVENTS = {
223-
{"topAuxClick", "auxclick"},
224-
{"topClick", "click"},
225-
{"topContextMenu", "contextmenu"},
226-
{"topDblClick", "dblclick"},
227-
{"topMouseDown", "mousedown"},
228-
{"topMouseEnter", "mouseenter"},
229-
{"topMouseLeave", "mouseleave"},
230-
{"topMouseOut", "mouseout"},
231-
{"topMouseOver", "mouseover"},
232-
{"topMouseUp", "mouseup"},
233-
{"topPointerOver", "pointerover"},
234-
{"topPointerEnter", "pointerenter"},
235-
{"topPointerDown", "pointerdown"},
236-
{"topPointerUp", "pointerup"},
237-
{"topPointerCancel", "pointercancel"},
238-
{"topPointerOut", "pointerout"},
239-
{"topPointerLeave", "pointerleave"},
240-
{"topGotPointerCapture", "gotpointercapture"},
241-
{"topLostPointerCapture", "lostpointercapture"},
242-
{"topTouchStart", "touchstart"},
243-
{"topTouchEnd", "touchend"},
244-
{"topTouchCancel", "touchcancel"},
245-
{"topKeyDown", "keydown"},
246-
{"topKeyPress", "keypress"},
247-
{"topKeyUp", "keyup"},
248-
{"topBeforeInput", "beforeinput"},
249-
{"topInput", "input"},
250-
{"topCompositionStart", "compositionstart"},
251-
{"topCompositionUpdate", "compositionupdate"},
252-
{"topCompositionEnd", "compositionend"},
253-
{"topDragStart", "dragstart"},
254-
{"topDragEnd", "dragend"},
255-
{"topDragEnter", "dragenter"},
256-
{"topDragLeave", "dragleave"},
257-
{"topDragOver", "dragover"},
258-
{"topDrop", "drop"},
259-
};
221+
using SupportedEventTypeRegistry =
222+
std::unordered_map<StrKey, const char *, StrKeyHash>;
223+
224+
static const SupportedEventTypeRegistry &getSupportedEvents() {
225+
static SupportedEventTypeRegistry SUPPORTED_EVENTS = {
226+
{"topAuxClick", "auxclick"},
227+
{"topClick", "click"},
228+
{"topContextMenu", "contextmenu"},
229+
{"topDblClick", "dblclick"},
230+
{"topMouseDown", "mousedown"},
231+
{"topMouseEnter", "mouseenter"},
232+
{"topMouseLeave", "mouseleave"},
233+
{"topMouseOut", "mouseout"},
234+
{"topMouseOver", "mouseover"},
235+
{"topMouseUp", "mouseup"},
236+
{"topPointerOver", "pointerover"},
237+
{"topPointerEnter", "pointerenter"},
238+
{"topPointerDown", "pointerdown"},
239+
{"topPointerUp", "pointerup"},
240+
{"topPointerCancel", "pointercancel"},
241+
{"topPointerOut", "pointerout"},
242+
{"topPointerLeave", "pointerleave"},
243+
{"topGotPointerCapture", "gotpointercapture"},
244+
{"topLostPointerCapture", "lostpointercapture"},
245+
{"topTouchStart", "touchstart"},
246+
{"topTouchEnd", "touchend"},
247+
{"topTouchCancel", "touchcancel"},
248+
{"topKeyDown", "keydown"},
249+
{"topKeyPress", "keypress"},
250+
{"topKeyUp", "keyup"},
251+
{"topBeforeInput", "beforeinput"},
252+
{"topInput", "input"},
253+
{"topCompositionStart", "compositionstart"},
254+
{"topCompositionUpdate", "compositionupdate"},
255+
{"topCompositionEnd", "compositionend"},
256+
{"topDragStart", "dragstart"},
257+
{"topDragEnd", "dragend"},
258+
{"topDragEnter", "dragenter"},
259+
{"topDragLeave", "dragleave"},
260+
{"topDragOver", "dragover"},
261+
{"topDrop", "drop"},
262+
};
263+
return SUPPORTED_EVENTS;
264+
}
260265

261266
EventTag PerformanceEntryReporter::onEventStart(const char *name) {
262267
if (!isReportingEvents()) {
263268
return 0;
264269
}
265-
266-
auto it = SUPPORTED_EVENTS.find(name);
267-
if (it == SUPPORTED_EVENTS.end()) {
270+
const auto &supportedEvents = getSupportedEvents();
271+
auto it = supportedEvents.find(name);
272+
if (it == supportedEvents.end()) {
268273
return 0;
269274
}
270275

0 commit comments

Comments
 (0)