Files
react-native/Libraries/Utilities/Platform.android.js
T
Kevin Gozali a89e9323fc Cache Platform constants in JS
Summary: For better perf with TurboModule, cache the return value of NativePlatformConstants*.getConstants() in JS so that we avoid going back into native (from JS) for each call. This specific method is called very frequently throughout RN codebase.

Reviewed By: mdvacca

Differential Revision: D15893289

fbshipit-source-id: ce8016ed7d3efb420df93e27dbfa77d7d4f06cf8
2019-06-18 23:10:50 -07:00

46 lines
997 B
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.
*
* @format
* @flow
*/
'use strict';
import NativePlatformConstantsAndroid from './NativePlatformConstantsAndroid';
export type PlatformSelectSpec<A, D> = {
android?: A,
default?: D,
};
const Platform = {
__constants: null,
OS: 'android',
get Version() {
return this.constants.Version;
},
get constants() {
if (this.__constants == null) {
this.__constants = NativePlatformConstantsAndroid.getConstants();
}
return this.__constants;
},
get isTesting(): boolean {
if (__DEV__) {
return this.constants.isTesting;
}
return false;
},
get isTV(): boolean {
return this.constants.uiMode === 'tv';
},
select: <A, D>(spec: PlatformSelectSpec<A, D>): A | D =>
'android' in spec ? spec.android : spec.default,
};
module.exports = Platform;