2018-02-14 18:28:22 +00:00
|
|
|
// @flow
|
2017-02-08 11:37:52 +00:00
|
|
|
|
|
|
|
import { AppState } from 'react-native';
|
|
|
|
import type { Dispatch } from 'redux';
|
|
|
|
|
2018-07-11 09:42:43 +00:00
|
|
|
import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../base/app';
|
2017-03-27 09:39:10 +00:00
|
|
|
import { MiddlewareRegistry } from '../../base/redux';
|
2017-02-10 16:13:39 +00:00
|
|
|
|
2020-05-20 10:57:03 +00:00
|
|
|
import { _SET_APP_STATE_LISTENER } from './actionTypes';
|
2017-02-08 11:37:52 +00:00
|
|
|
import {
|
2018-02-14 18:28:22 +00:00
|
|
|
_setAppStateListener as _setAppStateListenerA,
|
2017-02-10 16:13:39 +00:00
|
|
|
appStateChanged
|
2017-02-08 11:37:52 +00:00
|
|
|
} from './actions';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware that captures App lifetime actions and subscribes to application
|
|
|
|
* state changes. When the application state changes it will fire the action
|
2017-02-10 16:13:39 +00:00
|
|
|
* required to mute or unmute the local video in case the application goes to
|
|
|
|
* the background or comes back from it.
|
2017-02-08 11:37:52 +00:00
|
|
|
*
|
2018-02-14 18:28:22 +00:00
|
|
|
* @param {Store} store - The redux store.
|
2017-02-08 11:37:52 +00:00
|
|
|
* @returns {Function}
|
2017-02-10 16:13:39 +00:00
|
|
|
* @see {@link https://facebook.github.io/react-native/docs/appstate.html}
|
2017-02-08 11:37:52 +00:00
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
2018-02-14 18:28:22 +00:00
|
|
|
case _SET_APP_STATE_LISTENER:
|
|
|
|
return _setAppStateListenerF(store, next, action);
|
2017-02-10 16:13:39 +00:00
|
|
|
|
2018-02-14 18:28:22 +00:00
|
|
|
case APP_WILL_MOUNT: {
|
|
|
|
const { dispatch } = store;
|
|
|
|
|
2019-07-31 12:47:52 +00:00
|
|
|
dispatch(_setAppStateListenerA(_onAppStateChange.bind(undefined, dispatch)));
|
2017-02-08 11:37:52 +00:00
|
|
|
break;
|
2018-02-14 18:28:22 +00:00
|
|
|
}
|
2017-02-10 16:13:39 +00:00
|
|
|
|
2017-02-08 11:37:52 +00:00
|
|
|
case APP_WILL_UNMOUNT:
|
2018-02-14 18:28:22 +00:00
|
|
|
store.dispatch(_setAppStateListenerA(undefined));
|
2017-02-08 11:37:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2017-02-10 16:13:39 +00:00
|
|
|
* Called by React Native's AppState API to notify that the application state
|
2018-02-14 18:28:22 +00:00
|
|
|
* has changed. Dispatches the change within the (associated) redux store.
|
2017-02-08 11:37:52 +00:00
|
|
|
*
|
2018-02-14 18:28:22 +00:00
|
|
|
* @param {Dispatch} dispatch - The redux {@code dispatch} function.
|
2017-02-10 16:13:39 +00:00
|
|
|
* @param {string} appState - The current application execution state.
|
2017-02-08 11:37:52 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-03-19 15:42:25 +00:00
|
|
|
function _onAppStateChange(dispatch: Dispatch<any>, appState: string) {
|
2017-02-08 11:37:52 +00:00
|
|
|
dispatch(appStateChanged(appState));
|
|
|
|
}
|
2018-02-14 18:28:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies the feature filmstrip that the action
|
|
|
|
* {@link _SET_IMMERSIVE_LISTENER} is being dispatched within a specific redux
|
|
|
|
* store.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store in which the specified action is being
|
|
|
|
* dispatched.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
|
|
|
* specified action to the specified store.
|
|
|
|
* @param {Action} action - The redux action {@code _SET_IMMERSIVE_LISTENER}
|
|
|
|
* which is being dispatched in the specified store.
|
|
|
|
* @private
|
|
|
|
* @returns {Object} The value returned by {@code next(action)}.
|
|
|
|
*/
|
|
|
|
function _setAppStateListenerF({ getState }, next, action) {
|
|
|
|
// Remove the old AppState listener and add the new one.
|
|
|
|
const { appStateListener: oldListener } = getState()['features/background'];
|
|
|
|
const result = next(action);
|
|
|
|
const { appStateListener: newListener } = getState()['features/background'];
|
|
|
|
|
|
|
|
if (oldListener !== newListener) {
|
|
|
|
oldListener && AppState.removeEventListener('change', oldListener);
|
|
|
|
newListener && AppState.addEventListener('change', newListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|