Skip to content

Commit

Permalink
RelayProfiler: remove support for "*" profile event
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kassens authored and facebook-github-bot committed Apr 6, 2021
1 parent cc2758b commit 6e18b96
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions packages/relay-runtime/util/RelayProfiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProfileHandler>, ...} = {
'*': [],
};
const profileHandlersByName: {|
[name: EventName]: Array<ProfileHandler>,
|} = {};

const defaultProfiler = {
stop() {},
Expand Down Expand Up @@ -64,39 +65,27 @@ 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));
},
};
}
return defaultProfiler;
},

/**
* 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] = [];
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 6e18b96

Please sign in to comment.