|
@@ -3,56 +3,59 @@
|
3
|
3
|
import { AppState } from 'react-native';
|
4
|
4
|
import type { Dispatch } from 'redux';
|
5
|
5
|
|
|
6
|
+import {
|
|
7
|
+ APP_WILL_MOUNT,
|
|
8
|
+ APP_WILL_UNMOUNT
|
|
9
|
+} from '../app';
|
|
10
|
+import { MiddlewareRegistry } from '../base/redux';
|
|
11
|
+
|
6
|
12
|
import {
|
7
|
13
|
_setAppStateListener,
|
8
|
|
- appStateChanged,
|
9
|
|
- setBackgroundVideoMuted
|
|
14
|
+ _setBackgroundVideoMuted,
|
|
15
|
+ appStateChanged
|
10
|
16
|
} from './actions';
|
11
|
17
|
import {
|
12
|
18
|
_SET_APP_STATE_LISTENER,
|
13
|
19
|
APP_STATE_CHANGED
|
14
|
20
|
} from './actionTypes';
|
15
|
21
|
|
16
|
|
-import {
|
17
|
|
- APP_WILL_MOUNT,
|
18
|
|
- APP_WILL_UNMOUNT
|
19
|
|
-} from '../app';
|
20
|
|
-import { MiddlewareRegistry } from '../base/redux';
|
21
|
|
-
|
22
|
|
-
|
23
|
22
|
/**
|
24
|
23
|
* Middleware that captures App lifetime actions and subscribes to application
|
25
|
24
|
* state changes. When the application state changes it will fire the action
|
26
|
|
- * requred to mute or unmute the local video in case the application goes to
|
27
|
|
- * the backgound or comes back from it.
|
|
25
|
+ * required to mute or unmute the local video in case the application goes to
|
|
26
|
+ * the background or comes back from it.
|
28
|
27
|
*
|
29
|
|
- * @see https://facebook.github.io/react-native/docs/appstate.html
|
30
|
28
|
* @param {Store} store - Redux store.
|
31
|
29
|
* @returns {Function}
|
|
30
|
+ * @see {@link https://facebook.github.io/react-native/docs/appstate.html}
|
32
|
31
|
*/
|
33
|
32
|
MiddlewareRegistry.register(store => next => action => {
|
34
|
33
|
switch (action.type) {
|
35
|
34
|
case _SET_APP_STATE_LISTENER: {
|
|
35
|
+ // Remove the current/old AppState listener.
|
36
|
36
|
const { appStateListener } = store.getState()['features/background'];
|
37
|
37
|
|
38
|
38
|
if (appStateListener) {
|
39
|
39
|
AppState.removeEventListener('change', appStateListener);
|
40
|
40
|
}
|
|
41
|
+
|
|
42
|
+ // Add the new AppState listener.
|
41
|
43
|
if (action.listener) {
|
42
|
44
|
AppState.addEventListener('change', action.listener);
|
43
|
45
|
}
|
44
|
46
|
break;
|
45
|
47
|
}
|
|
48
|
+
|
46
|
49
|
case APP_STATE_CHANGED:
|
47
|
|
- _handleAppStateChange(store.dispatch, action.appState);
|
|
50
|
+ _appStateChanged(store.dispatch, action.appState);
|
48
|
51
|
break;
|
49
|
|
- case APP_WILL_MOUNT: {
|
50
|
|
- const listener
|
51
|
|
- = __onAppStateChanged.bind(undefined, store.dispatch);
|
52
|
52
|
|
53
|
|
- store.dispatch(_setAppStateListener(listener));
|
|
53
|
+ case APP_WILL_MOUNT:
|
|
54
|
+ store.dispatch(
|
|
55
|
+ _setAppStateListener(
|
|
56
|
+ _onAppStateChange.bind(undefined, store.dispatch)));
|
54
|
57
|
break;
|
55
|
|
- }
|
|
58
|
+
|
56
|
59
|
case APP_WILL_UNMOUNT:
|
57
|
60
|
store.dispatch(_setAppStateListener(null));
|
58
|
61
|
break;
|
|
@@ -61,36 +64,46 @@ MiddlewareRegistry.register(store => next => action => {
|
61
|
64
|
return next(action);
|
62
|
65
|
});
|
63
|
66
|
|
64
|
|
-
|
65
|
67
|
/**
|
66
|
|
- * Handler for app state changes. If will fire the necessary actions for
|
67
|
|
- * local video to be muted when the app goes to the background, and to
|
68
|
|
- * unmute it when it comes back.
|
|
68
|
+ * Handles app state changes. Dispatches the necessary Redux actions for the
|
|
69
|
+ * local video to be muted when the app goes to the background, and to be
|
|
70
|
+ * unmuted when the app comes back.
|
69
|
71
|
*
|
70
|
|
- * @param {Dispatch} dispatch - Redux dispatch function.
|
71
|
|
- * @param {string} appState - Current app state.
|
|
72
|
+ * @param {Dispatch} dispatch - Redux dispatch function.
|
|
73
|
+ * @param {string} appState - The current app state.
|
72
|
74
|
* @private
|
73
|
75
|
* @returns {void}
|
74
|
76
|
*/
|
75
|
|
-function _handleAppStateChange(dispatch: Dispatch<*>, appState: string) {
|
76
|
|
- // XXX: we purposely don't handle the 'inactive' state.
|
77
|
|
- if (appState === 'background') {
|
78
|
|
- dispatch(setBackgroundVideoMuted(true));
|
79
|
|
- } else if (appState === 'active') {
|
80
|
|
- dispatch(setBackgroundVideoMuted(false));
|
|
77
|
+function _appStateChanged(dispatch: Dispatch<*>, appState: string) {
|
|
78
|
+ let muted;
|
|
79
|
+
|
|
80
|
+ switch (appState) {
|
|
81
|
+ case 'active':
|
|
82
|
+ muted = false;
|
|
83
|
+ break;
|
|
84
|
+
|
|
85
|
+ case 'background':
|
|
86
|
+ muted = true;
|
|
87
|
+ break;
|
|
88
|
+
|
|
89
|
+ case 'inactive':
|
|
90
|
+ default:
|
|
91
|
+ // XXX: We purposely don't handle the 'inactive' app state.
|
|
92
|
+ return;
|
81
|
93
|
}
|
82
|
|
-}
|
83
|
94
|
|
|
95
|
+ dispatch(_setBackgroundVideoMuted(muted));
|
|
96
|
+}
|
84
|
97
|
|
85
|
98
|
/**
|
86
|
|
- * Handler called by React's AppState API indicating that the application state
|
87
|
|
- * has changed.
|
|
99
|
+ * Called by React Native's AppState API to notify that the application state
|
|
100
|
+ * has changed. Dispatches the change within the (associated) Redux store.
|
88
|
101
|
*
|
89
|
|
- * @param {Dispatch} dispatch - Redux dispatch function.
|
90
|
|
- * @param {string} appState - The current application execution state.
|
|
102
|
+ * @param {Dispatch} dispatch - Redux dispatch function.
|
|
103
|
+ * @param {string} appState - The current application execution state.
|
91
|
104
|
* @private
|
92
|
105
|
* @returns {void}
|
93
|
106
|
*/
|
94
|
|
-function __onAppStateChanged(dispatch: Dispatch<*>, appState: string) {
|
|
107
|
+function _onAppStateChange(dispatch: Dispatch<*>, appState: string) {
|
95
|
108
|
dispatch(appStateChanged(appState));
|
96
|
109
|
}
|