jiti-meet/react/features/mobile/background/middleware.js

110 lines
2.9 KiB
JavaScript
Raw Normal View History

/* @flow */
import { AppState } from 'react-native';
import type { Dispatch } from 'redux';
2017-02-10 16:13:39 +00:00
import {
APP_WILL_MOUNT,
APP_WILL_UNMOUNT
} from '../../app';
import { MiddlewareRegistry } from '../../base/redux';
2017-02-10 16:13:39 +00:00
import {
_setAppStateListener,
2017-02-10 16:13:39 +00:00
_setBackgroundVideoMuted,
appStateChanged
} from './actions';
import {
_SET_APP_STATE_LISTENER,
APP_STATE_CHANGED
} from './actionTypes';
/**
* 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.
*
* @param {Store} store - Redux store.
* @returns {Function}
2017-02-10 16:13:39 +00:00
* @see {@link https://facebook.github.io/react-native/docs/appstate.html}
*/
MiddlewareRegistry.register(store => next => action => {
switch (action.type) {
case _SET_APP_STATE_LISTENER: {
2017-02-10 16:13:39 +00:00
// Remove the current/old AppState listener.
2017-02-10 06:47:55 +00:00
const { appStateListener } = store.getState()['features/background'];
2017-02-10 06:47:55 +00:00
if (appStateListener) {
AppState.removeEventListener('change', appStateListener);
}
2017-02-10 16:13:39 +00:00
// Add the new AppState listener.
if (action.listener) {
AppState.addEventListener('change', action.listener);
}
break;
}
2017-02-10 16:13:39 +00:00
case APP_STATE_CHANGED:
2017-02-10 16:13:39 +00:00
_appStateChanged(store.dispatch, action.appState);
break;
2017-02-10 16:13:39 +00:00
case APP_WILL_MOUNT:
store.dispatch(
_setAppStateListener(
_onAppStateChange.bind(undefined, store.dispatch)));
break;
2017-02-10 16:13:39 +00:00
case APP_WILL_UNMOUNT:
store.dispatch(_setAppStateListener(null));
break;
}
return next(action);
});
/**
2017-02-10 16:13:39 +00:00
* Handles app state changes. Dispatches the necessary Redux actions for the
* local video to be muted when the app goes to the background, and to be
* unmuted when the app comes back.
*
2017-02-10 16:13:39 +00:00
* @param {Dispatch} dispatch - Redux dispatch function.
* @param {string} appState - The current app state.
* @private
* @returns {void}
*/
2017-02-10 16:13:39 +00:00
function _appStateChanged(dispatch: Dispatch<*>, appState: string) {
let muted;
switch (appState) {
case 'active':
muted = false;
break;
case 'background':
muted = true;
break;
case 'inactive':
default:
// XXX: We purposely don't handle the 'inactive' app state.
return;
}
2017-02-10 16:13:39 +00:00
dispatch(_setBackgroundVideoMuted(muted));
}
/**
2017-02-10 16:13:39 +00:00
* Called by React Native's AppState API to notify that the application state
* has changed. Dispatches the change within the (associated) Redux store.
*
2017-02-10 16:13:39 +00:00
* @param {Dispatch} dispatch - Redux dispatch function.
* @param {string} appState - The current application execution state.
* @private
* @returns {void}
*/
2017-02-10 16:13:39 +00:00
function _onAppStateChange(dispatch: Dispatch<*>, appState: string) {
dispatch(appStateChanged(appState));
}