Files
react-native/Libraries/Utilities/NativeDeviceInfo.js
T
Ramanpreet Nara 4e9c428328 Cache constants for MP Home NativeModules
Summary: TurboModules doesn't cache invocations of getConstants(). This diff looks at which NativeModules' getConstants() methods gets repeated called in Marketplace, and caches those invocations in JS.

Reviewed By: fkgozali

Differential Revision: D21874124

fbshipit-source-id: 03d2318e1b6d00236ef707f9f19a640bf8c08786
2020-06-03 20:48:27 -07:00

61 lines
1.3 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 strict-local
* @format
*/
'use strict';
import type {TurboModule} from '../TurboModule/RCTExport';
import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';
type DisplayMetricsAndroid = {|
width: number,
height: number,
scale: number,
fontScale: number,
densityDpi: number,
|};
export type DisplayMetrics = {|
width: number,
height: number,
scale: number,
fontScale: number,
|};
export type DimensionsPayload = {|
window?: DisplayMetrics,
screen?: DisplayMetrics,
windowPhysicalPixels?: DisplayMetricsAndroid,
screenPhysicalPixels?: DisplayMetricsAndroid,
|};
export interface Spec extends TurboModule {
+getConstants: () => {|
+Dimensions: DimensionsPayload,
+isIPhoneX_deprecated?: boolean,
|};
}
const NativeModule: Spec = TurboModuleRegistry.getEnforcing<Spec>('DeviceInfo');
let constants = null;
const NativeDeviceInfo = {
getConstants(): {|
+Dimensions: DimensionsPayload,
+isIPhoneX_deprecated?: boolean,
|} {
if (constants == null) {
constants = NativeModule.getConstants();
}
return constants;
},
};
export default NativeDeviceInfo;