Skip to content

Commit fe0c67d

Browse files
authored
Allow mobile to restart (keybase#6381)
* try changing load * fix android app start
1 parent 5bd2cfd commit fe0c67d

7 files changed

Lines changed: 58 additions & 57 deletions

File tree

‎shared/index.android.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
// @flow
22
// React-native tooling assumes this file is here, so we just require our real entry point
3-
43
import 'core-js/es6/object' // required for babel-plugin-transform-builtin-extend in RN Android
54
import 'core-js/es6/array' // required for emoji-mart in RN Android
65
import 'core-js/es6/string' // required for emoji-mart in RN Android
6+
import {load} from './index.native'
77

8-
require('./index.native')
9-
10-
module.hot && module.hot.accept(() => {
11-
console.log('accepted update in entry index.android.js')
12-
})
8+
load()

‎shared/index.ios.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
// @flow
22
// React-native tooling assumes this file is here, so we just require our real entry point
3-
require('./index.native')
3+
import {load} from './index.native'
4+
5+
load()

‎shared/index.native.js‎

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,47 @@
11
// @flow
22
import 'core-js/es6/reflect' // required for babel-plugin-transform-builtin-extend in RN iOS and Android
33
import './globals.native'
4+
45
import DumbSheet from './dev/dumb-sheet'
56
import DumbChatOnly from './dev/chat-only.native'
67
import Main from './main'
78
import React, {Component} from 'react'
89
import configureStore from './store/configure-store'
9-
import {AppRegistry, NativeAppEventEmitter, AsyncStorage} from 'react-native'
10+
import {AppRegistry} from 'react-native'
1011
import {Provider} from 'react-redux'
1112
import {makeEngine} from './engine'
12-
import {serializeRestore, serializeSave, timeTravel, timeTravelForward, timeTravelBack} from './constants/dev'
1313
import {setup as setupLocalDebug, dumbSheetOnly, dumbChatOnly} from './local-debug'
14-
import {stateKey} from './constants/reducer'
1514
import routeDefs from './routes'
1615
import {setRouteDef} from './actions/route-tree'
1716
import {setupSource} from './util/forward-logs'
1817

1918
module.hot && module.hot.accept(() => {
2019
console.log('accepted update in shared/index.native')
21-
if (global.devStore) {
20+
if (global.store) {
2221
// We use global.devStore because module scope variables seem to be cleared
2322
// out after a hot reload. Wacky.
2423
console.log('updating route defs due to hot reload')
25-
global.devStore.dispatch(setRouteDef(require('./routes').default))
24+
global.store.dispatch(setRouteDef(require('./routes').default))
2625
}
2726
})
2827

2928
class Keybase extends Component {
3029
store: any;
31-
subscriptions: Array<{remove: () => void}>
32-
componentWillMount () {
33-
setupSource()
34-
this.store = configureStore()
35-
setupLocalDebug(this.store)
36-
this.store.dispatch(setRouteDef(routeDefs))
37-
makeEngine()
38-
this.subscriptions = []
39-
if (__DEV__) {
40-
global.devStore = this.store
41-
AsyncStorage.getItem(stateKey, (err, stateJSON) => {
42-
if (err != null) {
43-
console.warn('Error in reading state:', err)
44-
}
45-
if (stateJSON != null) {
46-
this.store.dispatch({type: serializeRestore, payload: stateJSON})
47-
}
48-
})
4930

50-
this.subscriptions = [
51-
NativeAppEventEmitter.addListener('backInTime', () => this.store.dispatch({type: timeTravel, payload: {direction: timeTravelBack}})),
52-
NativeAppEventEmitter.addListener('forwardInTime', () => this.store.dispatch({type: timeTravel, payload: {direction: timeTravelForward}})),
53-
NativeAppEventEmitter.addListener('saveState', () => this.store.dispatch({type: serializeSave})),
54-
NativeAppEventEmitter.addListener('clearState', () => AsyncStorage.removeItem(stateKey)),
55-
]
56-
}
57-
}
31+
constructor (props: any) {
32+
super(props)
5833

59-
componentWillUnmount () {
60-
this.subscriptions.forEach(s => s.remove())
34+
if (!global.keybaseLoaded) {
35+
global.keybaseLoaded = true
36+
setupSource()
37+
this.store = configureStore()
38+
global.store = this.store
39+
setupLocalDebug(this.store)
40+
this.store.dispatch(setRouteDef(routeDefs))
41+
makeEngine()
42+
} else {
43+
this.store = global.store
44+
}
6145
}
6246

6347
render () {
@@ -79,4 +63,10 @@ class Keybase extends Component {
7963
}
8064
}
8165

82-
AppRegistry.registerComponent('Keybase', () => Keybase)
66+
function load () {
67+
AppRegistry.registerComponent('Keybase', () => Keybase)
68+
}
69+
70+
export {
71+
load,
72+
}

‎shared/main.android.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ class Main extends Component {
2121
constructor (props) {
2222
super(props)
2323

24-
initAvatarLookup(getUserImageMap)
25-
initAvatarLoad(loadUserImageMap)
26-
this.props.bootstrap()
27-
this.props.listenForNotifications()
24+
if (!global.mainLoaded) {
25+
global.mainLoaded = true
26+
initAvatarLookup(getUserImageMap)
27+
initAvatarLoad(loadUserImageMap)
28+
this.props.bootstrap()
29+
this.props.listenForNotifications()
2830

29-
// Introduce ourselves to the service
30-
hello(0, 'Android app', [], '0.0.0', true) // TODO real version
31+
// Introduce ourselves to the service
32+
hello(0, 'Android app', [], '0.0.0', true) // TODO real version
33+
}
3134
}
3235

3336
componentWillMount () {

‎shared/main.ios.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ class Main extends Component {
2020
constructor (props) {
2121
super(props)
2222

23-
initAvatarLookup(getUserImageMap)
24-
initAvatarLoad(loadUserImageMap)
25-
this.props.bootstrap()
26-
this.props.listenForNotifications()
23+
if (!global.mainLoaded) {
24+
global.mainLoaded = true
25+
initAvatarLookup(getUserImageMap)
26+
initAvatarLoad(loadUserImageMap)
27+
this.props.bootstrap()
28+
this.props.listenForNotifications()
2729

28-
// Introduce ourselves to the service
29-
hello(0, 'iOS app', [], '0.0.0', true) // TODO real version
30+
// Introduce ourselves to the service
31+
hello(0, 'iOS app', [], '0.0.0', true) // TODO real version
32+
}
3033
}
3134

3235
render () {

‎shared/push/push.native.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ class Push extends Component<void, Props, void> {
1717
console.log('Skipping push config due to simulator')
1818
return
1919
}
20-
this.props.configurePush()
20+
21+
if (!global.pushLoaded) {
22+
global.pushLoaded = true
23+
this.props.configurePush()
24+
}
2125
}
2226

2327
render () {

‎shared/react-native/android/app/src/main/java/io/keybase/ossifrage/modules/KeybaseEngine.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class KeybaseEngine extends ReactContextBaseJavaModule implements Killabl
2424

2525
private static final String NAME = "KeybaseEngine";
2626
private static final String RPC_EVENT_NAME = "RPC";
27-
private final ExecutorService executor;
27+
private ExecutorService executor;
2828

2929
private class ReadFromKBLib implements Runnable {
3030
private final ReactApplicationContext reactContext;
@@ -56,12 +56,14 @@ public void run() {
5656
public KeybaseEngine(final ReactApplicationContext reactContext) {
5757
super(reactContext);
5858

59-
executor = Executors.newSingleThreadExecutor();
60-
executor.execute(new ReadFromKBLib(reactContext));
6159

6260
reactContext.addLifecycleEventListener(new LifecycleEventListener() {
6361
@Override
6462
public void onHostResume() {
63+
if (executor == null) {
64+
executor = Executors.newSingleThreadExecutor();
65+
executor.execute(new ReadFromKBLib(reactContext));
66+
}
6567
}
6668

6769
@Override
@@ -80,6 +82,7 @@ public void destroy(){
8082
executor.shutdownNow();
8183
reset();
8284
executor.awaitTermination(30, TimeUnit.SECONDS);
85+
executor = null;
8386
} catch (Exception e) {
8487
e.printStackTrace();
8588
}

0 commit comments

Comments
 (0)