-
Notifications
You must be signed in to change notification settings - Fork 27
/
app.js
125 lines (117 loc) · 3.05 KB
/
app.js
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
import { eventChannel } from 'redux-saga'
import {
call,
put,
race,
select,
spawn,
take,
takeLatest,
} from 'redux-saga/effects'
import { AppState } from 'react-native'
import NetInfo from '@react-native-community/netinfo'
import QB from 'quickblox-react-native-sdk'
import {
appStartFail,
appStartSuccess,
chatConnect,
chatConnectAndSubscribe,
chatDisconnect,
connectionStateChanged,
webrtcInit,
webrtcRelease,
} from '../actionCreators'
import {
CHAT_CONNECT_AND_SUBSCRIBE,
CHAT_CONNECT_FAIL,
CHAT_CONNECT_SUCCESS,
INIT_QB_REQUEST,
} from '../constants'
import { isChatConnected } from './chat'
export function* appStart(action = {}) {
const config = action.payload
try {
yield call(QB.settings.init, config)
yield put(appStartSuccess())
} catch (e) {
yield put(appStartFail(e.message))
}
}
export function* connectAndSubscribe() {
const { user } = yield select(state => state.auth)
if (!user) return
const chatConnected = yield call(isChatConnected)
const { networkConnected, loading } = yield select(({ app, chat }) => ({
networkConnected: app.connected,
loading: chat.loading,
}))
if (!networkConnected) return
if (!chatConnected) {
if (!loading) {
yield put(chatConnect({
userId: user.id,
password: user.password,
}))
}
const { fail } = yield race({
success: take(CHAT_CONNECT_SUCCESS),
fail: take(CHAT_CONNECT_FAIL),
})
if (fail) {
return
}
}
yield put(webrtcInit())
}
function createAppStateChannel() {
return eventChannel(emit => {
AppState.addEventListener('change', emit)
return () => AppState.removeEventListener('change', emit)
})
}
export function* startAppStateListener() {
try {
const appStateChannel = yield call(createAppStateChannel)
while (true) {
const nextAppState = yield take(appStateChannel)
yield call(appStateChangeHandler, nextAppState)
}
} catch (e) {
yield put({ type: 'ERROR', error: e.message })
}
}
export function* startNetInfoStateListener() {
try {
const channel = eventChannel(NetInfo.addEventListener)
while (true) {
const state = yield take(channel)
yield put(connectionStateChanged(state.isConnected))
const wasConnected = yield select(({ chat }) => chat.connected)
if (!wasConnected && state.isConnected) {
yield put(chatConnectAndSubscribe())
}
}
} catch (e) {
yield put({ type: 'NETINFO_CHANNEL_ERROR', error: e.message })
}
}
function* appStateChangeHandler(appState) {
const { connected, session } = yield select(({ chat, webrtc }) => ({
connected: chat.connected,
session: webrtc.session,
}))
if (appState.match(/inactive|background/)) {
if (connected && !session) {
yield put(chatDisconnect())
yield put(webrtcRelease())
}
} else {
yield put(chatConnectAndSubscribe())
}
}
export default [
takeLatest(INIT_QB_REQUEST, appStart),
takeLatest(CHAT_CONNECT_AND_SUBSCRIBE, connectAndSubscribe),
spawn(startAppStateListener),
spawn(startNetInfoStateListener),
]