Skip to content

Commit f4c4f44

Browse files
J.T. Yimfacebook-github-bot
J.T. Yim
authored andcommitted
Remove AsyncStorage from rn-tester and fix InternalSettings Example
Summary: Changelog: [**General**][**Removed**] - Removed AsyncStorage usage from RNTester As part of the "Lean Core" efforts (see react-native-community/discussions-and-proposals#6) to remove outdated and/or unused components (status: https://gist.github.com/Simek/88a9f1a014a47c37f4fce3738864d2e1), this diff removes usage of the deprecated AsyncStorage API from RNTester. RNTester is intended as a reference to showcase various components and APIs. The implications of the replacement of AsyncStorage in RNTester with in-memory management of state is a tradeoff of persistance to a lighter weight implementation and user predictable behavior. 1. Removed AsyncStorage from rn-tester - removed Navigation and bookmark persisting from reducer - moved JS Stalls and tracking to application state with context and reducer 2. Fixed InternalSettings Example bugs Reviewed By: lunaleaps Differential Revision: D35435562 fbshipit-source-id: a879787d8683a1c452e5b6b75a9e01f3ceadfe5d
1 parent 361b9a8 commit f4c4f44

11 files changed

+323
-322
lines changed

packages/rn-tester/js/RNTesterAppShared.js

+52-61
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,29 @@ import RNTesterNavBar, {navBarHeight} from './components/RNTesterNavbar';
2323
import RNTesterList from './utils/RNTesterList';
2424
import {
2525
Screens,
26-
initialState,
26+
initialNavState,
2727
getExamplesListWithBookmarksAndRecentlyUsed,
28-
getInitialStateFromAsyncStorage,
2928
} from './utils/testerStateUtils';
30-
import {useAsyncStorageReducer} from './utils/useAsyncStorageReducer';
31-
import {RNTesterReducer, RNTesterActionsType} from './utils/RNTesterReducer';
29+
import {
30+
RNTesterNavReducer,
31+
RNTesterNavActionsType,
32+
} from './utils/RNTesterNavReducer';
3233
import {RNTesterThemeContext, themes} from './components/RNTesterTheme';
3334
import RNTTitleBar from './components/RNTTitleBar';
3435
import {RNTesterEmptyBookmarksState} from './components/RNTesterEmptyBookmarksState';
36+
import {RNTesterJsStallsProvider} from './utils/RNTesterJsStallsContext';
3537

36-
const APP_STATE_KEY = 'RNTesterAppState.v3';
37-
38-
// RNTester App currently uses AsyncStorage from react-native for storing navigation state
38+
// RNTester App currently uses in memory storage for storing navigation state
3939
// and bookmark items.
40-
// TODO: Vendor AsyncStorage or create our own.
40+
// TODO: Identify/implement solution for async device storage.
4141
LogBox.ignoreLogs([/AsyncStorage has been extracted from react-native/]);
4242

4343
const RNTesterApp = (): React.Node => {
44-
const [state, dispatch] = useAsyncStorageReducer(
45-
RNTesterReducer,
46-
initialState,
47-
APP_STATE_KEY,
44+
const [state, dispatch] = React.useReducer<Function, Object>(
45+
RNTesterNavReducer,
46+
initialNavState,
4847
);
48+
4949
const colorScheme = useColorScheme();
5050

5151
const {
@@ -57,17 +57,6 @@ const RNTesterApp = (): React.Node => {
5757
recentlyUsed,
5858
} = state;
5959

60-
React.useEffect(() => {
61-
getInitialStateFromAsyncStorage(APP_STATE_KEY).then(
62-
initialStateFromStorage => {
63-
dispatch({
64-
type: RNTesterActionsType.INIT_FROM_STORAGE,
65-
data: initialStateFromStorage,
66-
});
67-
},
68-
);
69-
}, [dispatch]);
70-
7160
const examplesList = React.useMemo(
7261
() =>
7362
getExamplesListWithBookmarksAndRecentlyUsed({bookmarks, recentlyUsed}),
@@ -76,7 +65,7 @@ const RNTesterApp = (): React.Node => {
7665

7766
const handleBackPress = React.useCallback(() => {
7867
if (activeModuleKey != null) {
79-
dispatch({type: RNTesterActionsType.BACK_BUTTON_PRESS});
68+
dispatch({type: RNTesterNavActionsType.BACK_BUTTON_PRESS});
8069
}
8170
}, [dispatch, activeModuleKey]);
8271

@@ -103,7 +92,7 @@ const RNTesterApp = (): React.Node => {
10392
const handleModuleCardPress = React.useCallback(
10493
({exampleType, key, title}) => {
10594
dispatch({
106-
type: RNTesterActionsType.MODULE_CARD_PRESS,
95+
type: RNTesterNavActionsType.MODULE_CARD_PRESS,
10796
data: {exampleType, key, title},
10897
});
10998
},
@@ -113,7 +102,7 @@ const RNTesterApp = (): React.Node => {
113102
const handleModuleExampleCardPress = React.useCallback(
114103
exampleName => {
115104
dispatch({
116-
type: RNTesterActionsType.EXAMPLE_CARD_PRESS,
105+
type: RNTesterNavActionsType.EXAMPLE_CARD_PRESS,
117106
data: {key: exampleName},
118107
});
119108
},
@@ -123,7 +112,7 @@ const RNTesterApp = (): React.Node => {
123112
const toggleBookmark = React.useCallback(
124113
({exampleType, key}) => {
125114
dispatch({
126-
type: RNTesterActionsType.BOOKMARK_PRESS,
115+
type: RNTesterNavActionsType.BOOKMARK_PRESS,
127116
data: {exampleType, key},
128117
});
129118
},
@@ -133,7 +122,7 @@ const RNTesterApp = (): React.Node => {
133122
const handleNavBarPress = React.useCallback(
134123
args => {
135124
dispatch({
136-
type: RNTesterActionsType.NAVBAR_PRESS,
125+
type: RNTesterNavActionsType.NAVBAR_PRESS,
137126
data: {screen: args.screen},
138127
});
139128
},
@@ -170,40 +159,42 @@ const RNTesterApp = (): React.Node => {
170159

171160
return (
172161
<RNTesterThemeContext.Provider value={theme}>
173-
<RNTTitleBar
174-
title={title}
175-
theme={theme}
176-
onBack={activeModule ? handleBackPress : null}
177-
documentationURL={activeModule?.documentationURL}
178-
/>
179-
<View
180-
style={StyleSheet.compose(styles.container, {
181-
backgroundColor: theme.GroupedBackgroundColor,
182-
})}>
183-
{activeModule != null ? (
184-
<RNTesterModuleContainer
185-
module={activeModule}
186-
example={activeModuleExample}
187-
onExampleCardPress={handleModuleExampleCardPress}
188-
/>
189-
) : screen === Screens.BOOKMARKS &&
190-
examplesList.bookmarks.length === 0 ? (
191-
<RNTesterEmptyBookmarksState />
192-
) : (
193-
<RNTesterModuleList
194-
sections={activeExampleList}
195-
toggleBookmark={toggleBookmark}
196-
handleModuleCardPress={handleModuleCardPress}
197-
/>
198-
)}
199-
</View>
200-
<View style={styles.bottomNavbar}>
201-
<RNTesterNavBar
202-
screen={screen || Screens.COMPONENTS}
203-
isExamplePageOpen={!!activeModule}
204-
handleNavBarPress={handleNavBarPress}
162+
<RNTesterJsStallsProvider>
163+
<RNTTitleBar
164+
title={title}
165+
theme={theme}
166+
onBack={activeModule ? handleBackPress : null}
167+
documentationURL={activeModule?.documentationURL}
205168
/>
206-
</View>
169+
<View
170+
style={StyleSheet.compose(styles.container, {
171+
backgroundColor: theme.GroupedBackgroundColor,
172+
})}>
173+
{activeModule != null ? (
174+
<RNTesterModuleContainer
175+
module={activeModule}
176+
example={activeModuleExample}
177+
onExampleCardPress={handleModuleExampleCardPress}
178+
/>
179+
) : screen === Screens.BOOKMARKS &&
180+
examplesList.bookmarks.length === 0 ? (
181+
<RNTesterEmptyBookmarksState />
182+
) : (
183+
<RNTesterModuleList
184+
sections={activeExampleList}
185+
toggleBookmark={toggleBookmark}
186+
handleModuleCardPress={handleModuleCardPress}
187+
/>
188+
)}
189+
</View>
190+
<View style={styles.bottomNavbar}>
191+
<RNTesterNavBar
192+
screen={screen || Screens.COMPONENTS}
193+
isExamplePageOpen={!!activeModule}
194+
handleNavBarPress={handleNavBarPress}
195+
/>
196+
</View>
197+
</RNTesterJsStallsProvider>
207198
</RNTesterThemeContext.Provider>
208199
);
209200
};

packages/rn-tester/js/components/RNTesterSettingSwitchRow.js

+19-40
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,33 @@
88
* @flow strict-local
99
*/
1010

11-
'use strict';
12-
13-
const RNTesterStatePersister = require('../utils/RNTesterStatePersister');
1411
const React = require('react');
1512

1613
const {StyleSheet, Switch, Text, View} = require('react-native');
1714

18-
class RNTesterSettingSwitchRow extends React.Component<
19-
$FlowFixMeProps,
20-
$FlowFixMeState,
21-
> {
22-
UNSAFE_componentWillReceiveProps(newProps: $FlowFixMeProps) {
23-
const {onEnable, onDisable, persister} = this.props;
24-
if (newProps.persister.state !== persister.state) {
25-
newProps.persister.state ? onEnable() : onDisable();
26-
}
27-
}
28-
render(): React.Node {
29-
const {label, persister} = this.props;
30-
return (
31-
<View style={styles.row}>
32-
<Text>{label}</Text>
33-
<Switch
34-
value={persister.state}
35-
onValueChange={value => {
36-
persister.setState(() => value);
37-
}}
38-
/>
39-
</View>
40-
);
41-
}
42-
}
4315
const styles = StyleSheet.create({
4416
row: {
4517
padding: 10,
4618
flexDirection: 'row',
4719
justifyContent: 'space-between',
4820
},
4921
});
50-
/* $FlowFixMe[cannot-reassign-export] (>=0.85.0 site=react_native_fb) This
51-
* comment suppresses an error found when Flow v0.85 was deployed. To see the
52-
* error, delete this comment and run Flow. */
53-
// $FlowFixMe[cannot-reassign]
54-
RNTesterSettingSwitchRow = RNTesterStatePersister.createContainer(
55-
RNTesterSettingSwitchRow,
56-
{
57-
cacheKeySuffix: ({label}) => 'Switch:' + label,
58-
getInitialState: ({initialValue}) => initialValue,
59-
},
60-
);
61-
module.exports = RNTesterSettingSwitchRow;
22+
23+
export const RNTesterSettingSwitchRow = ({
24+
label,
25+
onEnable,
26+
onDisable,
27+
active,
28+
}: $FlowFixMeProps): React.Node => {
29+
return (
30+
<View style={styles.row}>
31+
<Text>{label}</Text>
32+
<Switch
33+
value={active}
34+
onValueChange={() => {
35+
active ? onDisable() : onEnable();
36+
}}
37+
/>
38+
</View>
39+
);
40+
};

packages/rn-tester/js/examples/NativeAnimation/NativeAnimationsExample.js

+39-68
Original file line numberDiff line numberDiff line change
@@ -158,74 +158,45 @@ class LoopExample extends React.Component<{...}, $FlowFixMeState> {
158158
}
159159
}
160160

161-
const RNTesterSettingSwitchRow = require('../../components/RNTesterSettingSwitchRow');
162-
class InternalSettings extends React.Component<
163-
{...},
164-
{
165-
busyTime: number | string,
166-
filteredStall: number,
167-
...
168-
},
169-
> {
170-
_stallInterval: ?number;
171-
render() {
172-
return (
173-
<View>
174-
<RNTesterSettingSwitchRow
175-
initialValue={false}
176-
label="Force JS Stalls"
177-
onEnable={() => {
178-
/* $FlowFixMe[incompatible-type] (>=0.63.0 site=react_native_fb)
179-
* This comment suppresses an error found when Flow v0.63 was
180-
* deployed. To see the error delete this comment and run Flow. */
181-
this._stallInterval = setInterval(() => {
182-
const start = Date.now();
183-
console.warn('burn CPU');
184-
while (Date.now() - start < 100) {}
185-
}, 300);
186-
}}
187-
onDisable={() => {
188-
/* $FlowFixMe[incompatible-call] (>=0.63.0 site=react_native_fb)
189-
* This comment suppresses an error found when Flow v0.63 was
190-
* deployed. To see the error delete this comment and run Flow. */
191-
clearInterval(this._stallInterval || 0);
192-
}}
193-
/>
194-
<RNTesterSettingSwitchRow
195-
initialValue={false}
196-
label="Track JS Stalls"
197-
onEnable={() => {
198-
require('react-native/Libraries/Interaction/JSEventLoopWatchdog').install(
199-
{
200-
thresholdMS: 25,
201-
},
202-
);
203-
this.setState({busyTime: '<none>'});
204-
require('react-native/Libraries/Interaction/JSEventLoopWatchdog').addHandler(
205-
{
206-
onStall: ({busyTime}) =>
207-
this.setState(state => ({
208-
busyTime,
209-
filteredStall:
210-
(state.filteredStall || 0) * 0.97 + busyTime * 0.03,
211-
})),
212-
},
213-
);
214-
}}
215-
onDisable={() => {
216-
console.warn('Cannot disable yet....');
217-
}}
218-
/>
219-
{this.state && (
220-
<Text>
221-
{`JS Stall filtered: ${Math.round(this.state.filteredStall)}, `}
222-
{`last: ${this.state.busyTime}`}
223-
</Text>
224-
)}
225-
</View>
226-
);
227-
}
228-
}
161+
const RNTesterSettingSwitchRow =
162+
require('../../components/RNTesterSettingSwitchRow').RNTesterSettingSwitchRow;
163+
const RNTesterJsStallsContext = require('../../utils/RNTesterJsStallsContext');
164+
165+
const InternalSettings = () => {
166+
const {
167+
state,
168+
onDisableForceJsStalls,
169+
onEnableForceJsStalls,
170+
onEnableJsStallsTracking,
171+
onDisableJsStallsTracking,
172+
} = React.useContext(RNTesterJsStallsContext.RNTesterJsStallsContext);
173+
174+
const {stallInterval, busyTime, filteredStall, tracking} = state;
175+
return (
176+
<View>
177+
<RNTesterSettingSwitchRow
178+
initialValue={false}
179+
label="Force JS Stalls"
180+
active={stallInterval !== null}
181+
onEnable={onEnableForceJsStalls}
182+
onDisable={onDisableForceJsStalls}
183+
/>
184+
<RNTesterSettingSwitchRow
185+
initialValue={false}
186+
label="Track JS Stalls"
187+
active={tracking}
188+
onEnable={onEnableJsStallsTracking}
189+
onDisable={onDisableJsStallsTracking}
190+
/>
191+
{tracking && (
192+
<Text>
193+
{`JS Stall filtered: ${Math.round(filteredStall)}, `}
194+
{`last: ${busyTime !== null ? busyTime.toFixed(8) : '<none>'}`}
195+
</Text>
196+
)}
197+
</View>
198+
);
199+
};
229200

230201
class EventExample extends React.Component<{...}, $FlowFixMeState> {
231202
state = {

packages/rn-tester/js/types/RNTesterTypes.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,18 @@ export type ScreenTypes = 'components' | 'apis' | 'bookmarks' | null;
5959

6060
export type ComponentList = null | {components: string[], apis: string[]};
6161

62-
export type RNTesterState = {
62+
export type RNTesterNavState = {
6363
activeModuleKey: null | string,
6464
activeModuleTitle: null | string,
6565
activeModuleExampleKey: null | string,
6666
screen: ScreenTypes,
6767
bookmarks: ComponentList,
6868
recentlyUsed: ComponentList,
6969
};
70+
71+
export type RNTesterJsStallsState = {
72+
stallInterval: null | number,
73+
busyTime: null | number,
74+
filteredStall: number,
75+
tracking: boolean,
76+
};

0 commit comments

Comments
 (0)