Skip to content

Commit

Permalink
fix: Set CryptoWebView as ready only when it is really ready
Browse files Browse the repository at this point in the history
We use react-native-webview onLoadEnd callback to set the
CryptoWebView as ready. It is an important action because other
webviews are rendered only when the CryptoWebView is ready. But the
onLoadEnd can be triggered before the javascript, where we add
our crypto functions, is executed.
It is also executed two times (I do not understand why). So here I
send a post message from the CryptoWebView to indicate that it is
ready.

It fixes a race condition we may encounter more and more.
  • Loading branch information
zatteo committed Sep 6, 2024
1 parent 77ee36f commit 980fe77
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/components/webviews/CryptoWebView/CryptoWebView.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export const CryptoWebView = ({ setHasCrypto }) => {
const processAnswer = event => {
const webviewAnswer = JSON.parse(event.nativeEvent.data)

if (webviewAnswer.isReady) {
log.debug('Ready')
setIsLoading(false)
return
}

if (webviewAnswer.isError) {
log.error(`Error : ${event.nativeEvent.data}`)
return
Expand Down Expand Up @@ -61,7 +67,6 @@ export const CryptoWebView = ({ setHasCrypto }) => {
ref={webviewRef}
javaScriptEnabled={true}
onMessage={processAnswer}
onLoadEnd={() => setIsLoading(false)}
originWhitelist={['*']}
source={{ html, baseUrl: 'https://localhost' }}
supervisionShowProgress={false}
Expand Down
8 changes: 7 additions & 1 deletion src/components/webviews/CryptoWebView/CryptoWebView.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ jest.mock('react-native-webview', () => {
})
}
componentDidMount() {
this.props.onLoadEnd()
this.props.onMessage({
nativeEvent: {
data: JSON.stringify({
isReady: true
})
}
})
}
render() {
return <div>WebView</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const jsCode = `
generateHttpServerSecurityKey,
sublteProxy
}
postMessage(JSON.stringify({ isReady: true }))
`

export const html = `
Expand Down

0 comments on commit 980fe77

Please sign in to comment.