-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
247 lines (208 loc) · 7.07 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import React, { useState, useEffect, useRef } from 'react';
import {
Button,
SafeAreaView,
ScrollView,
StatusBar,
Text,
useColorScheme,
View,
StyleSheet,
} from 'react-native';
import {
Colors,
Header,
} from 'react-native/Libraries/NewAppScreen';
import { WebView } from 'react-native-webview';
function generateRandomString(length: number) {
const allowedChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += allowedChars.charAt(Math.floor(Math.random() * allowedChars.length));
}
return result;
}
const smallRandomString = generateRandomString(100);
const mediumRandomString = generateRandomString(10000);
const bigRandomString = generateRandomString(1000000);
// With postMessage
// 1. RN load a webview
// 2. RN send a start event to webview
// 3. Webview start a timer
// 4. Webview send a ping event to RN
// 5. RN send a pong event to webview with a payload
// 6. Webview end the timer
// 7. Webview send the result to RN
// 8. RN display the result
// With httpServer
// 1. RN load a webview
// 2. RN send a start event to webview
// 3. Webview start a timer
// 4. Webview a request to the http server
// 5. Webview get the answer from the http server
// 6. Webview end the timer
// 7. Webview send the result to RN
// 8. RN display the result
const MyWebView = ({ dataType }: { dataType: string }) => {
const webviewRef = useRef();
const [postMessageResults, setPostMessageResults] = React.useState<number[]>([]);
const [httpServerResults, setHttpServerResults] = React.useState<number[]>([]);
let testData: any;
if (dataType === 'small') {
testData = smallRandomString;
} else if (dataType === 'medium') {
testData = mediumRandomString;
} else if (dataType === 'big') {
testData = bigRandomString;
}
const html = `
<html>
<head></head>
<body>
<script>
const dataType = "${dataType}";
let t0 = 0;
let t1 = 0;
window.document.addEventListener('message', function ({ data }) {
const { message } = JSON.parse(data);
if (message === 'startPostMessage') {
startPostMessage();
} if (message === 'startHttpServer') {
startHttpServer();
} else if (message === 'pong') {
endPostMessage();
}
});
const startPostMessage = () => {
t0 = performance.now();
const data = JSON.stringify({ message: 'ping' });
window.ReactNativeWebView.postMessage(data);
}
const endPostMessage = () => {
t1 = performance.now();
const result = t1 - t0;
const data = JSON.stringify({ message: 'postMessageResult', result });
window.ReactNativeWebView.postMessage(data);
}
const startHttpServer = () => {
t0 = performance.now();
fetch('http://127.0.0.1:36666/' + dataType)
.then((response) => response.json())
.then((res) => {
t1 = performance.now();
const result = t1 - t0;
const data = JSON.stringify({ message: 'httpServerResult', result });
window.ReactNativeWebView.postMessage(data);
})
}
</script>
</body>
</html>
`;
const onMessage = (event) => {
const message = JSON.parse(event.nativeEvent.data);
if (message.message === 'ping') {
const pong = JSON.stringify({ message: 'pong', testData });
webviewRef.current.postMessage(pong);
} else if (message.message === 'postMessageResult') {
setPostMessageResults(prev => [...prev, message.result]);
} else if (message.message === 'httpServerResult') {
setHttpServerResults(prev => [...prev, message.result]);
}
};
// With postMessage
const onStartPostMessage = () => {
webviewRef.current.postMessage(JSON.stringify({ message: 'startPostMessage'}));
};
// With httpServer
const onStartHttpServer = () => {
webviewRef.current.postMessage(JSON.stringify({ message: 'startHttpServer'}));
};
return (
<View style={{ flex: 1 }}>
<Button
title="Start postMessage test"
onPress={onStartPostMessage}
/>
<Button
title="Start httpServer test"
onPress={onStartHttpServer}
/>
<Button
title="Clean results"
onPress={() => {
setPostMessageResults([]);
setHttpServerResults([]);
}}
/>
<View style={{ flex: 1 }}>
{/* ##### postMessage results ##### */}
<Text style={{ fontSize: 22, fontWeight: 700 }}>postMessage results:</Text>
{postMessageResults.map((postMessageResult, index) => (
<Text key={index}>{index}) {postMessageResult} ms</Text>
))}
{postMessageResults.length > 0 && <Text style={{ fontWeight: 700 }}>Average: {postMessageResults.reduce((a, b) => a + b, 0) / postMessageResults.length} ms</Text>}
{/* ##### httpServer results ##### */}
<Text style={{ fontSize: 22, fontWeight: 700 }}>httpServer results:</Text>
{httpServerResults.map((httpServerResult, index) => (
<Text key={index}>{index}) {httpServerResult} ms</Text>
))}
{httpServerResults.length > 0 && <Text style={{ fontWeight: 700 }}>Average: {httpServerResults.reduce((a, b) => a + b, 0) / httpServerResults.length} ms</Text>}
</View>
<WebView
ref={webviewRef}
source={{ html, baseUrl: 'http://127.0.0.1:36666' }}
originWhitelist={['*']}
onMessage={onMessage}
/>
</View>
);
};
function App(): React.JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
const [dataType, setDataType] = useState('small');
return (
<SafeAreaView style={backgroundStyle}>
<StatusBar
barStyle={isDarkMode ? 'light-content' : 'dark-content'}
backgroundColor={backgroundStyle.backgroundColor}
/>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={backgroundStyle}>
<Header />
<View
style={{
backgroundColor: isDarkMode ? Colors.black : Colors.white,
}}>
<Text>Using {global?.nativeFabricUIManager ? 'new' : 'old'} arch</Text>
<Text>Data type: {dataType}</Text>
<View style={styles.container}>
<View style={styles.buttonRow}>
<Button title="small" onPress={() => setDataType('small')} />
<Button title="medium" onPress={() => setDataType('medium')} />
<Button title="big" onPress={() => setDataType('big')} />
</View>
</View>
<MyWebView dataType={dataType}/>
</View>
</ScrollView>
</SafeAreaView>
);
}
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
buttonRow: {
flexDirection: 'row',
justifyContent: 'space-around',
width: '80%',
},
});