Files
react-native/Libraries/Utilities/withPerformanceLoggerContext.js
Alexey Lang a9b3ca7fa7 Use global or scoped performance loggers everywhere
Summary:
I'm changing all callsites to use either global or scoped perf logger explicitly in one diff.
`GlobalPerformanceLogger` is basically a singleton
`scopedPerformanceLogger` is scoped to the React tree by using a React Context

Reviewed By: sahrens

Differential Revision: D14186694

fbshipit-source-id: 062c76eea8fce9d9b531f0eddf153bb79d52f68d
2019-03-06 09:51:56 -08:00

54 lines
1.6 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
const React = require('React');
const PerformanceLoggerContext = require('PerformanceLoggerContext');
import type {IPerformanceLogger} from 'createPerformanceLogger';
export type PerformanceLoggerContextProps = {
scopedPerformanceLogger: IPerformanceLogger,
};
/**
* If you already have one React Context on your component, you can't use
* PerformanceLoggerContext without a consumer for it. This function helps to
* do that. Here's how to use it:
* 1) Intersect Props of your component with PerformanceLoggerContextProps.
* 2) Call this function with Props and RequiredProps of your component.
* You can figure out RequiredProps as $Diff<Props, DefaultProps>.
*/
function withPerformanceLoggerContext<
TProps: PerformanceLoggerContextProps,
TRequiredProps: PerformanceLoggerContextProps,
>(
Component: React.ComponentType<TProps>,
): React.ComponentType<$Diff<TRequiredProps, PerformanceLoggerContextProps>> {
return class WrappedComponent extends React.Component<
$Diff<TProps, PerformanceLoggerContextProps>,
> {
render() {
return (
<PerformanceLoggerContext.Consumer>
{scopedPerformanceLogger => (
<Component
{...this.props}
scopedPerformanceLogger={scopedPerformanceLogger}
/>
)}
</PerformanceLoggerContext.Consumer>
);
}
};
}
module.exports = withPerformanceLoggerContext;