Skip to content

Commit 7ca8a48

Browse files
rubennortefacebook-github-bot
authored andcommitted
Warn only when Fantom root creation changes global dimensions
Summary: Avoid repeated warnings when a new root matches the current global dimensions, even if older roots have different metrics. Continue warning when another root is alive and the new root changes the current metrics. Changelog: [Internal] Compare Fantom root dimensions against current global metrics. Differential Revision: D127606984
1 parent 4b2aef2 commit 7ca8a48

3 files changed

Lines changed: 20 additions & 18 deletions

File tree

‎private/react-native-fantom/__docs__/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ describe('My feature', () => {
9595
Creating a root sets global `Dimensions` for both `window` and `screen` from its
9696
`viewportWidth`, `viewportHeight`, `devicePixelRatio`, and optional `fontScale`
9797
(default `1`). `useWindowDimensions()` and `PixelRatio` report the new root's
98-
values. If another root is still alive with different display metrics, Fantom
99-
applies the new values and emits a warning. Call `root.destroy()` when a root is
100-
no longer needed.
98+
values. If another root is still alive and the new metrics differ from the
99+
current global dimensions, Fantom applies the new values and emits a warning.
100+
Call `root.destroy()` when a root is no longer needed.
101101

102102
For a full API reference, please see the [inline documentation](../src/index.js)
103103
defined for the methods in the `@react-native/fantom` [module](../src/index.js).

‎private/react-native-fantom/src/__tests__/FantomDimensions-itest.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,15 @@ for (const [name, config] of conflictingConfigs) {
116116
});
117117
}
118118

119-
it('checks all active roots, including one that differs from the latest root', () => {
119+
it('compares new metrics only with the current global dimensions', () => {
120120
createRoot();
121121
createRoot({viewportWidth: 640});
122122
createRoot({viewportWidth: 640});
123-
expect(warn).toHaveBeenCalledTimes(2);
123+
expect(warn).toHaveBeenCalledTimes(1);
124124
expect(Dimensions.get('window').width).toBe(640);
125+
createRoot();
126+
expect(warn).toHaveBeenCalledTimes(2);
127+
expect(Dimensions.get('window').width).toBe(390);
125128
});
126129

127130
it('does not warn for a destroyed root', () => {

‎private/react-native-fantom/src/index.js‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,20 @@ function pushDimensions(
8484
surfaceId: RootTag,
8585
displayMetrics: DisplayMetrics,
8686
): void {
87+
const previousDisplayMetrics = Dimensions.get('window');
8788
activeRootDisplayMetrics.set(surfaceId, displayMetrics);
8889
Dimensions.set({window: displayMetrics, screen: displayMetrics});
89-
for (const metrics of activeRootDisplayMetrics.values()) {
90-
if (
91-
metrics.width !== displayMetrics.width ||
92-
metrics.height !== displayMetrics.height ||
93-
metrics.scale !== displayMetrics.scale ||
94-
metrics.fontScale !== displayMetrics.fontScale
95-
) {
96-
console.warn(
97-
'Fantom.createRoot() updated global Dimensions while another active root has different display metrics. Dimensions and PixelRatio now reflect the new root.',
98-
{existingRoot: metrics, newRoot: displayMetrics},
99-
);
100-
break;
101-
}
90+
if (
91+
activeRootDisplayMetrics.size > 1 &&
92+
(previousDisplayMetrics.width !== displayMetrics.width ||
93+
previousDisplayMetrics.height !== displayMetrics.height ||
94+
previousDisplayMetrics.scale !== displayMetrics.scale ||
95+
previousDisplayMetrics.fontScale !== displayMetrics.fontScale)
96+
) {
97+
console.warn(
98+
'Fantom.createRoot() updated global Dimensions while another active root has different display metrics. Dimensions and PixelRatio now reflect the new root.',
99+
{existingRoot: previousDisplayMetrics, newRoot: displayMetrics},
100+
);
102101
}
103102
}
104103

0 commit comments

Comments
 (0)