You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.js 3.7KB

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