Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.native.ts 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { AppState } from 'react-native';
  2. import { IStore } from '../../app/types';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../base/app/actionTypes';
  4. import MiddlewareRegistry from '../../base/redux/MiddlewareRegistry';
  5. import { _setAppStateSubscription, appStateChanged } from './actions';
  6. import logger from './logger';
  7. /**
  8. * Middleware that captures App lifetime actions and subscribes to application
  9. * state changes. When the application state changes it will fire the action
  10. * required to mute or unmute the local video in case the application goes to
  11. * the background or comes back from it.
  12. *
  13. * @param {Store} store - The redux store.
  14. * @returns {Function}
  15. * @see {@link https://facebook.github.io/react-native/docs/appstate.html}
  16. */
  17. MiddlewareRegistry.register(store => next => action => {
  18. switch (action.type) {
  19. case APP_WILL_MOUNT: {
  20. const { dispatch } = store;
  21. _setAppStateListener(store, _onAppStateChange.bind(undefined, dispatch));
  22. // Because there is no change taking place when the app mounts,
  23. // we need to force registering the appState status.
  24. const appStateInterval = setInterval(() => {
  25. const { currentState } = AppState;
  26. if (currentState !== 'unknown') {
  27. clearInterval(appStateInterval);
  28. _onAppStateChange(dispatch, currentState);
  29. }
  30. }, 100);
  31. break;
  32. }
  33. case APP_WILL_UNMOUNT:
  34. _setAppStateListener(store, undefined);
  35. break;
  36. }
  37. return next(action);
  38. });
  39. /**
  40. * Called by React Native's AppState API to notify that the application state
  41. * has changed. Dispatches the change within the (associated) redux store.
  42. *
  43. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  44. * @param {string} appState - The current application execution state.
  45. * @private
  46. * @returns {void}
  47. */
  48. function _onAppStateChange(dispatch: IStore['dispatch'], appState: string) {
  49. dispatch(appStateChanged(appState));
  50. logger.info(`appState changed to: ${appState}`);
  51. }
  52. /**
  53. * Notifies the feature filmstrip that the action
  54. * {@link _SET_IMMERSIVE_LISTENER} is being dispatched within a specific redux
  55. * store.
  56. *
  57. * @param {Store} store - The redux store in which the specified action is being
  58. * dispatched.
  59. * @param {any} listener - Listener for app state status.
  60. * @private
  61. * @returns {Object} The value returned by {@code next(action)}.
  62. */
  63. function _setAppStateListener({ dispatch, getState }: IStore, listener: any) {
  64. const { subscription } = getState()['features/mobile/background'];
  65. subscription?.remove();
  66. dispatch(_setAppStateSubscription(listener ? AppState.addEventListener('change', listener) : undefined));
  67. }