Skip to content

Commit 895de29

Browse files
authored
Merge pull request #37138 from Expensify/Rory-AddOnyxGetToWindowForEasierDebugging
Expose Onyx.get to window for easier debugging
2 parents 004b1bc + e340b4e commit 895de29

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

src/App.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {PortalProvider} from '@gorhom/portal';
22
import React from 'react';
33
import {LogBox} from 'react-native';
44
import {GestureHandlerRootView} from 'react-native-gesture-handler';
5-
import Onyx from 'react-native-onyx';
65
import {PickerStateProvider} from 'react-native-picker-select';
76
import {SafeAreaProvider} from 'react-native-safe-area-context';
87
import '../wdyr';
@@ -31,8 +30,6 @@ import {WindowDimensionsProvider} from './components/withWindowDimensions';
3130
import Expensify from './Expensify';
3231
import useDefaultDragAndDrop from './hooks/useDefaultDragAndDrop';
3332
import OnyxUpdateManager from './libs/actions/OnyxUpdateManager';
34-
import * as Session from './libs/actions/Session';
35-
import * as Environment from './libs/Environment/Environment';
3633
import InitialUrlContext from './libs/InitialUrlContext';
3734
import {ReportAttachmentsProvider} from './pages/home/report/ReportAttachmentsContext';
3835
import type {Route} from './ROUTES';
@@ -42,12 +39,6 @@ type AppProps = {
4239
url?: Route;
4340
};
4441

45-
// For easier debugging and development, when we are in web we expose Onyx to the window, so you can more easily set data into Onyx
46-
if (window && Environment.isDevelopment()) {
47-
window.Onyx = Onyx;
48-
window.setSupportToken = Session.setSupportAuthToken;
49-
}
50-
5142
LogBox.ignoreLogs([
5243
// Basically it means that if the app goes in the background and back to foreground on Android,
5344
// the timer is lost. Currently Expensify is using a 30 minutes interval to refresh personal details.

src/libs/Environment/Environment.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ function isDevelopment(): boolean {
2424
return (Config?.ENVIRONMENT ?? CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.DEV;
2525
}
2626

27+
/**
28+
* Are we running the app in production?
29+
*/
30+
function isProduction(): Promise<boolean> {
31+
return getEnvironment().then((environment) => environment === CONST.ENVIRONMENT.PRODUCTION);
32+
}
33+
2734
/**
2835
* Are we running an internal test build?
2936
*/
@@ -47,4 +54,4 @@ function getOldDotEnvironmentURL(): Promise<string> {
4754
return getEnvironment().then((environment) => OLDDOT_ENVIRONMENT_URLS[environment]);
4855
}
4956

50-
export {getEnvironment, isInternalTestBuild, isDevelopment, getEnvironmentURL, getOldDotEnvironmentURL};
57+
export {getEnvironment, isInternalTestBuild, isDevelopment, isProduction, getEnvironmentURL, getOldDotEnvironmentURL};

src/setup/addUtilsToWindow.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Onyx from 'react-native-onyx';
2+
import * as Environment from '@libs/Environment/Environment';
3+
import * as Session from '@userActions/Session';
4+
5+
/**
6+
* This is used to inject development/debugging utilities into the window object on web and desktop.
7+
* We do this only on non-production builds - these should not be used in any application code.
8+
*/
9+
export default function addUtilsToWindow() {
10+
if (!window) {
11+
return;
12+
}
13+
14+
Environment.isProduction().then((isProduction) => {
15+
if (isProduction) {
16+
return;
17+
}
18+
19+
window.Onyx = Onyx;
20+
21+
// We intentionally do not offer an Onyx.get API because we believe it will lead to code patterns we don't want to use in this repo, but we can offer a workaround for the sake of debugging
22+
// @ts-expect-error TS233 - injecting additional utility for use in runtime debugging, should not be used in any compiled code
23+
window.Onyx.get = function (key) {
24+
return new Promise((resolve) => {
25+
// eslint-disable-next-line rulesdir/prefer-onyx-connect-in-libs
26+
const connectionID = Onyx.connect({
27+
key,
28+
callback: (value) => {
29+
Onyx.disconnect(connectionID);
30+
resolve(value);
31+
},
32+
waitForCollectionCallback: true,
33+
});
34+
});
35+
};
36+
37+
window.setSupportToken = Session.setSupportAuthToken;
38+
});
39+
}

src/setup/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Device from '@userActions/Device';
66
import exposeGlobalMemoryOnlyKeysMethods from '@userActions/MemoryOnlyKeys/exposeGlobalMemoryOnlyKeysMethods';
77
import CONST from '@src/CONST';
88
import ONYXKEYS from '@src/ONYXKEYS';
9+
import addUtilsToWindow from './addUtilsToWindow';
910
import initializeLastVisitedPath from './initializeLastVisitedPath';
1011
import platformSetup from './platformSetup';
1112

@@ -59,4 +60,6 @@ export default function () {
5960

6061
// Perform any other platform-specific setup
6162
platformSetup();
63+
64+
addUtilsToWindow();
6265
}

0 commit comments

Comments
 (0)