| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 | /* @flow */
import { AppState } from 'react-native';
import type { Dispatch } from 'redux';
import {
    APP_WILL_MOUNT,
    APP_WILL_UNMOUNT
} from '../../app';
import { MiddlewareRegistry } from '../../base/redux';
import {
    _setAppStateListener,
    _setBackgroundVideoMuted,
    appStateChanged
} from './actions';
import {
    _SET_APP_STATE_LISTENER,
    _SET_LASTN,
    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
 * 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}
 * @see {@link https://facebook.github.io/react-native/docs/appstate.html}
 */
MiddlewareRegistry.register(store => next => action => {
    switch (action.type) {
    case _SET_APP_STATE_LISTENER: {
        // Remove the current/old AppState listener.
        const { appStateListener } = store.getState()['features/background'];
        if (appStateListener) {
            AppState.removeEventListener('change', appStateListener);
        }
        // Add the new AppState listener.
        if (action.listener) {
            AppState.addEventListener('change', action.listener);
        }
        break;
    }
    case _SET_LASTN: {
        const { conference } = store.getState()['features/base/conference'];
        if (conference) {
            try {
                conference.setLastN(action.lastN);
            } catch (err) {
                console.warn(`Failed to set lastN: ${err}`);
            }
        }
        break;
    }
    case APP_STATE_CHANGED:
        _appStateChanged(store.dispatch, action.appState);
        break;
    case APP_WILL_MOUNT:
        store.dispatch(
                _setAppStateListener(
                        _onAppStateChange.bind(undefined, store.dispatch)));
        break;
    case APP_WILL_UNMOUNT:
        store.dispatch(_setAppStateListener(null));
        break;
    }
    return next(action);
});
/**
 * 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.
 *
 * @param {Dispatch} dispatch - Redux dispatch function.
 * @param {string} appState - The current app state.
 * @private
 * @returns {void}
 */
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;
    }
    dispatch(_setBackgroundVideoMuted(muted));
}
/**
 * Called by React Native's AppState API to notify that the application state
 * has changed. Dispatches the change within the (associated) Redux store.
 *
 * @param {Dispatch} dispatch - Redux dispatch function.
 * @param {string} appState - The current application execution state.
 * @private
 * @returns {void}
 */
function _onAppStateChange(dispatch: Dispatch<*>, appState: string) {
    dispatch(appStateChanged(appState));
}
 |