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.native.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 { _SET_APP_STATE_LISTENER } from './actionTypes';
  7. import {
  8. _setAppStateListener as _setAppStateListenerA,
  9. appStateChanged
  10. } from './actions';
  11. /**
  12. * Middleware that captures App lifetime actions and subscribes to application
  13. * state changes. When the application state changes it will fire the action
  14. * required to mute or unmute the local video in case the application goes to
  15. * the background or comes back from it.
  16. *
  17. * @param {Store} store - The redux store.
  18. * @returns {Function}
  19. * @see {@link https://facebook.github.io/react-native/docs/appstate.html}
  20. */
  21. MiddlewareRegistry.register(store => next => action => {
  22. switch (action.type) {
  23. case _SET_APP_STATE_LISTENER:
  24. return _setAppStateListenerF(store, next, action);
  25. case APP_WILL_MOUNT: {
  26. const { dispatch } = store;
  27. dispatch(_setAppStateListenerA(_onAppStateChange.bind(undefined, dispatch)));
  28. break;
  29. }
  30. case APP_WILL_UNMOUNT:
  31. store.dispatch(_setAppStateListenerA(undefined));
  32. break;
  33. }
  34. return next(action);
  35. });
  36. /**
  37. * Called by React Native's AppState API to notify that the application state
  38. * has changed. Dispatches the change within the (associated) redux store.
  39. *
  40. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  41. * @param {string} appState - The current application execution state.
  42. * @private
  43. * @returns {void}
  44. */
  45. function _onAppStateChange(dispatch: Dispatch<any>, appState: string) {
  46. dispatch(appStateChanged(appState));
  47. }
  48. /**
  49. * Notifies the feature filmstrip that the action
  50. * {@link _SET_IMMERSIVE_LISTENER} is being dispatched within a specific redux
  51. * store.
  52. *
  53. * @param {Store} store - The redux store in which the specified action is being
  54. * dispatched.
  55. * @param {Dispatch} next - The redux dispatch function to dispatch the
  56. * specified action to the specified store.
  57. * @param {Action} action - The redux action {@code _SET_IMMERSIVE_LISTENER}
  58. * which is being dispatched in the specified store.
  59. * @private
  60. * @returns {Object} The value returned by {@code next(action)}.
  61. */
  62. function _setAppStateListenerF({ getState }, next, action) {
  63. // Remove the old AppState listener and add the new one.
  64. const { appStateListener: oldListener } = getState()['features/background'];
  65. const result = next(action);
  66. const { appStateListener: newListener } = getState()['features/background'];
  67. if (oldListener !== newListener) {
  68. oldListener && AppState.removeEventListener('change', oldListener);
  69. newListener && AppState.addEventListener('change', newListener);
  70. }
  71. return result;
  72. }