From 6e18b9654dfb45f7dbec6760165d186c276dd6f9 Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Tue, 6 Apr 2021 07:24:00 -0700 Subject: [PATCH] RelayProfiler: remove support for "*" profile event Summary: We only have a single event used for `RelayProfiler.profile()`, this diff removes the support for the catch all `*` event name and enforces the event name with Flow. Reviewed By: jstejada Differential Revision: D27571844 fbshipit-source-id: 7c6d569d87a0f456696a9ebedef47a5bced1b042 --- packages/relay-runtime/util/RelayProfiler.js | 41 +++++++------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/packages/relay-runtime/util/RelayProfiler.js b/packages/relay-runtime/util/RelayProfiler.js index 332ad67041244..b2b180f4182ee 100644 --- a/packages/relay-runtime/util/RelayProfiler.js +++ b/packages/relay-runtime/util/RelayProfiler.js @@ -12,11 +12,12 @@ 'use strict'; -type ProfileHandler = (name: string, state?: any) => (error?: Error) => void; +type EventName = 'fetchRelayQuery'; +type ProfileHandler = (name: EventName, state?: any) => (error?: Error) => void; -const profileHandlersByName: {[name: string]: Array, ...} = { - '*': [], -}; +const profileHandlersByName: {| + [name: EventName]: Array, +|} = {}; const defaultProfiler = { stop() {}, @@ -64,28 +65,17 @@ const RelayProfiler = { * Arbitrary state can also be passed into `profile` as a second argument. The * attached profile handlers will receive this as the second argument. */ - profile(name: string, state?: any): {stop: (error?: Error) => void, ...} { - const hasCatchAllHandlers = profileHandlersByName['*'].length > 0; - const hasNamedHandlers = profileHandlersByName.hasOwnProperty(name); - if (hasNamedHandlers || hasCatchAllHandlers) { - const profileHandlers = - hasNamedHandlers && hasCatchAllHandlers - ? profileHandlersByName[name].concat(profileHandlersByName['*']) - : hasNamedHandlers - ? profileHandlersByName[name] - : profileHandlersByName['*']; - let stopHandlers; - for (let ii = profileHandlers.length - 1; ii >= 0; ii--) { - const profileHandler = profileHandlers[ii]; - const stopHandler = profileHandler(name, state); - stopHandlers = stopHandlers || []; + profile(name: EventName, state?: any): {stop: (error?: Error) => void, ...} { + const handlers = profileHandlersByName[name]; + if (handlers && handlers.length > 0) { + const stopHandlers = []; + for (let ii = handlers.length - 1; ii >= 0; ii--) { + const stopHandler = handlers[ii](name, state); stopHandlers.unshift(stopHandler); } return { stop(error?: Error): void { - if (stopHandlers) { - stopHandlers.forEach(stopHandler => stopHandler(error)); - } + stopHandlers.forEach(stopHandler => stopHandler(error)); }, }; } @@ -93,10 +83,9 @@ const RelayProfiler = { }, /** - * Attaches a handler to profiles with the supplied name. You can also - * attach to the special name '*' which is a catch all. + * Attaches a handler to profiles with the supplied name. */ - attachProfileHandler(name: string, handler: ProfileHandler): void { + attachProfileHandler(name: EventName, handler: ProfileHandler): void { if (!profileHandlersByName.hasOwnProperty(name)) { profileHandlersByName[name] = []; } @@ -106,7 +95,7 @@ const RelayProfiler = { /** * Detaches a handler attached via `attachProfileHandler`. */ - detachProfileHandler(name: string, handler: ProfileHandler): void { + detachProfileHandler(name: EventName, handler: ProfileHandler): void { if (profileHandlersByName.hasOwnProperty(name)) { removeFromArray(profileHandlersByName[name], handler); }