Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.js 3.2KB

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