-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebViewProvider.tsx
78 lines (74 loc) · 2.18 KB
/
WebViewProvider.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
import React, {createContext, useRef} from 'react';
import {fromByteArray} from 'react-native-quick-base64';
import {View} from 'react-native';
import {WebView} from 'react-native-webview';
import {witnessStr} from './witness';
export const WebViewContext = createContext<{
witnessCalculationWebView: Function;
}>({witnessCalculationWebView: () => ({})});
export const WebViewWrapperProvider = (props: any) => {
const webViewRef = useRef(null);
let resolveMethod: any;
const eventHandler = (event: {nativeEvent: {data: string}}) => {
const data = JSON.parse(event.nativeEvent.data);
console.log('WebView event: ', data.event);
if (!resolveMethod) return;
if (data.event === '@EXECUTION_RESULT') {
resolveMethod(data.witnessCalculationResult);
}
};
const witnessCalculationWebView = async (
binary: Uint8Array,
inputs: any,
): Promise<Uint8Array> => {
// @ts-ignore
webViewRef.current.postMessage(
JSON.stringify({
event: '@EXECUTE_WASM',
binary: fromByteArray(binary),
inputs,
}),
);
console.log('sent to webView');
return await new Promise(resolve => {
resolveMethod = resolve;
});
};
return (
<WebViewContext.Provider
value={{
witnessCalculationWebView,
}}>
<View style={{flex: 0}}>
<WebView
ref={webViewRef}
style={{height: 0}}
source={{
html: `
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<h1>Silver area is a Webview</h1>
</body>
</html>
`,
}}
injectedJavaScriptBeforeContentLoaded={witnessStr}
onMessage={event => {
eventHandler(event);
}}
allowFileAccessFromFileURLs
allowUniversalAccessFromFileURLs
allowFileAccess
onError={syntheticEvent => {
const {nativeEvent} = syntheticEvent;
console.warn('WebView error: ', nativeEvent);
}}
/>
</View>
{props.children}
</WebViewContext.Provider>
);
};