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 2.9KB

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