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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. break;
  23. }
  24. case APP_WILL_UNMOUNT:
  25. _setAppStateListener(store, undefined);
  26. break;
  27. }
  28. return next(action);
  29. });
  30. /**
  31. * Called by React Native's AppState API to notify that the application state
  32. * has changed. Dispatches the change within the (associated) redux store.
  33. *
  34. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  35. * @param {string} appState - The current application execution state.
  36. * @private
  37. * @returns {void}
  38. */
  39. function _onAppStateChange(dispatch: IStore['dispatch'], appState: string) {
  40. dispatch(appStateChanged(appState));
  41. logger.debug(`appState changed to: ${appState}`);
  42. }
  43. /**
  44. * Notifies the feature filmstrip that the action
  45. * {@link _SET_IMMERSIVE_LISTENER} is being dispatched within a specific redux
  46. * store.
  47. *
  48. * @param {Store} store - The redux store in which the specified action is being
  49. * dispatched.
  50. * @param {any} listener - Listener for app state status.
  51. * @private
  52. * @returns {Object} The value returned by {@code next(action)}.
  53. */
  54. function _setAppStateListener({ dispatch, getState }: IStore, listener: any) {
  55. const { subscription } = getState()['features/mobile/background'];
  56. subscription?.remove();
  57. dispatch(_setAppStateSubscription(listener ? AppState.addEventListener('change', listener) : undefined));
  58. }