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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* @flow */
  2. import { AppState } from 'react-native';
  3. import type { Dispatch } from 'redux';
  4. import {
  5. _setAppStateListener,
  6. appStateChanged,
  7. setBackgroundVideoMuted
  8. } from './actions';
  9. import {
  10. _SET_APP_STATE_LISTENER,
  11. APP_STATE_CHANGED
  12. } from './actionTypes';
  13. import {
  14. APP_WILL_MOUNT,
  15. APP_WILL_UNMOUNT
  16. } from '../app';
  17. import { MiddlewareRegistry } from '../base/redux';
  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. * requred to mute or unmute the local video in case the application goes to
  22. * the backgound or comes back from it.
  23. *
  24. * @see https://facebook.github.io/react-native/docs/appstate.html
  25. * @param {Store} store - Redux store.
  26. * @returns {Function}
  27. */
  28. MiddlewareRegistry.register(store => next => action => {
  29. switch (action.type) {
  30. case _SET_APP_STATE_LISTENER: {
  31. const { appStateListener } = store.getState()['features/background'];
  32. if (appStateListener) {
  33. AppState.removeEventListener('change', appStateListener);
  34. }
  35. if (action.listener) {
  36. AppState.addEventListener('change', action.listener);
  37. }
  38. break;
  39. }
  40. case APP_STATE_CHANGED:
  41. _handleAppStateChange(store.dispatch, action.appState);
  42. break;
  43. case APP_WILL_MOUNT: {
  44. const listener
  45. = __onAppStateChanged.bind(undefined, store.dispatch);
  46. store.dispatch(_setAppStateListener(listener));
  47. break;
  48. }
  49. case APP_WILL_UNMOUNT:
  50. store.dispatch(_setAppStateListener(null));
  51. break;
  52. }
  53. return next(action);
  54. });
  55. /**
  56. * Handler for app state changes. If will fire the necessary actions for
  57. * local video to be muted when the app goes to the background, and to
  58. * unmute it when it comes back.
  59. *
  60. * @param {Dispatch} dispatch - Redux dispatch function.
  61. * @param {string} appState - Current app state.
  62. * @private
  63. * @returns {void}
  64. */
  65. function _handleAppStateChange(dispatch: Dispatch<*>, appState: string) {
  66. // XXX: we purposely don't handle the 'inactive' state.
  67. if (appState === 'background') {
  68. dispatch(setBackgroundVideoMuted(true));
  69. } else if (appState === 'active') {
  70. dispatch(setBackgroundVideoMuted(false));
  71. }
  72. }
  73. /**
  74. * Handler called by React's AppState API indicating that the application state
  75. * has changed.
  76. *
  77. * @param {Dispatch} dispatch - Redux dispatch function.
  78. * @param {string} appState - The current application execution state.
  79. * @private
  80. * @returns {void}
  81. */
  82. function __onAppStateChanged(dispatch: Dispatch<*>, appState: string) {
  83. dispatch(appStateChanged(appState));
  84. }