Skip to content

Commit

Permalink
fix(rn): implement devtools button to restart daemon
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Nov 5, 2018
1 parent cee0b06 commit d5f3bde
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
@@ -1,9 +1,14 @@
import { ActivityIndicator, NativeModules } from 'react-native'
import React, { PureComponent } from 'react'
import { Header, Menu } from '../../../Library'

import { Flex, Header, Menu, Screen, Text } from '../../../Library'
import { colors } from '../../../../constants'

const { CoreModule } = NativeModules

export default class List extends PureComponent {
static navigationOptions = ({ navigation }) => ({
header: (
header: navigation.getParam('restartDaemon') ? null : (
<Header
navigation={navigation}
title='Developer Tools'
Expand All @@ -14,8 +19,43 @@ export default class List extends PureComponent {
tabBarVisible: false,
})

state = {
restartDaemon: false,
}

restartDaemon = async () => {
this.props.navigation.setParams({ restartDaemon: true })
this.setState({ restartDaemon: true }, async () => {
try {
await CoreModule.restart()
this.props.navigation.setParams({
restartDaemon: false,
})
this.setState({ restartDaemon: false })
} catch (err) {
console.error(err)
}
})
}

render () {
const { navigation } = this.props
console.log(navigation)
const { restartDaemon } = this.state
if (restartDaemon) {
return (
<Screen style={{ backgroundColor: colors.white }}>
<Flex.Rows align='center'>
<Flex.Cols align='end'>
<ActivityIndicator size='large' />
</Flex.Cols>
<Text center margin align='start'>
Daemon is restarting, please wait ...
</Text>
</Flex.Rows>
</Screen>
)
}
return (
<Menu>
<Menu.Section customMarginTop={1}>
Expand All @@ -28,10 +68,8 @@ export default class List extends PureComponent {
<Menu.Section>
<Menu.Item
icon='refresh-ccw'
title='Restart daemon (not implemented)'
onPress={() => {
console.log('Restart')
}}
title='Restart daemon'
onPress={this.restartDaemon}
/>
<Menu.Item
icon='list'
Expand Down
27 changes: 13 additions & 14 deletions client/react-native/common/relay/environment.js
Expand Up @@ -10,14 +10,15 @@ if (__DEV__) {

// @TODO: patch web CoreModule
if (Platform.OS === 'web') {
NativeModules.CoreModule = {
const CoreModule = {
start: async () => {},
restart: async () => {},
getPort: async () => {
const url = new URL(window.location.href)
return url.searchParams.get('gql-port') || '8700'
},
}
NativeModules.CoreModule = CoreModule
}

const { CoreModule } = NativeModules
Expand Down Expand Up @@ -63,20 +64,18 @@ let getIP = () =>
}
})

const getPort = () =>
new Promise(resolve => {
let port = 0
const interval = setInterval(async () => {
try {
port = await CoreModule.getPort()

resolve(port)
clearInterval(interval)
} catch (error) {
console.warn(error)
}
const getPortInterval = async resolve => {
try {
const port = await CoreModule.getPort()
resolve(port)
} catch (error) {
console.warn(error)
setTimeout(() => {
getPortInterval(resolve)
}, 1000)
})
}
}
const getPort = () => new Promise(resolve => getPortInterval(resolve))

getIP().then(console.log)
getPort().then(console.log)
Expand Down

0 comments on commit d5f3bde

Please sign in to comment.