Skip to content

Commit 68991d3

Browse files
yungstersfacebook-github-bot
authored andcommitted
StyleSheet: Lazily Compute hairlineWidth
Summary: Changes `StyleSheet` to lazily compute the `hairlineWidth` property, removing the synchronous dependency of importing `StyleSheet` on the initialization of the `PixelRatio` TurboModule. Changelog: [General][Changed] - Importing `hairlineWidth` no longer eagerly initializes the `PixelRatio` TurboModule. Differential Revision: D90339801
1 parent 1c94a36 commit 68991d3

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

packages/react-native/Libraries/StyleSheet/StyleSheetExports.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

@@ -13,16 +13,10 @@
1313
import type {____Styles_Internal} from './StyleSheetTypes';
1414

1515
import composeStyles from '../../src/private/styles/composeStyles';
16+
import ReactNativeStyleAttributes from '../Components/View/ReactNativeStyleAttributes';
1617
import flatten from './flattenStyle';
1718

18-
const ReactNativeStyleAttributes =
19-
require('../Components/View/ReactNativeStyleAttributes').default;
20-
const PixelRatio = require('../Utilities/PixelRatio').default;
21-
22-
let hairlineWidth: number = PixelRatio.roundToNearestPixel(0.4);
23-
if (hairlineWidth === 0) {
24-
hairlineWidth = 1 / PixelRatio.get();
25-
}
19+
let _hairlineWidth: ?number;
2620

2721
const absoluteFill = {
2822
position: 'absolute',
@@ -95,7 +89,18 @@ export default {
9589
*
9690
* A line with hairline width may not be visible if your simulator is downscaled.
9791
*/
98-
hairlineWidth,
92+
// $FlowFixMe[unsafe-getters-setters] - Lazily import dependencies.
93+
get hairlineWidth(): number {
94+
if (_hairlineWidth == null) {
95+
const PixelRatio = require('../Utilities/PixelRatio').default;
96+
97+
_hairlineWidth = PixelRatio.roundToNearestPixel(0.4);
98+
if (_hairlineWidth === 0) {
99+
_hairlineWidth = 1 / PixelRatio.get();
100+
}
101+
}
102+
return _hairlineWidth;
103+
},
99104

100105
/**
101106
* A very common pattern is to create overlays with position absolute and zero positioning,

0 commit comments

Comments
 (0)