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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /**
  7. * Middleware that captures App lifetime actions and subscribes to application
  8. * state changes. When the application state changes it will fire the action
  9. * required to mute or unmute the local video in case the application goes to
  10. * the background or comes back from it.
  11. *
  12. * @param {Store} store - The redux store.
  13. * @returns {Function}
  14. * @see {@link https://facebook.github.io/react-native/docs/appstate.html}
  15. */
  16. MiddlewareRegistry.register(store => next => action => {
  17. switch (action.type) {
  18. case APP_WILL_MOUNT: {
  19. const { dispatch } = store;
  20. _setAppStateListener(store, _onAppStateChange.bind(undefined, dispatch));
  21. break;
  22. }
  23. case APP_WILL_UNMOUNT:
  24. _setAppStateListener(store, undefined);
  25. break;
  26. }
  27. return next(action);
  28. });
  29. /**
  30. * Called by React Native's AppState API to notify that the application state
  31. * has changed. Dispatches the change within the (associated) redux store.
  32. *
  33. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  34. * @param {string} appState - The current application execution state.
  35. * @private
  36. * @returns {void}
  37. */
  38. function _onAppStateChange(dispatch: IStore['dispatch'], appState: string) {
  39. dispatch(appStateChanged(appState));
  40. }
  41. /**
  42. * Notifies the feature filmstrip that the action
  43. * {@link _SET_IMMERSIVE_LISTENER} is being dispatched within a specific redux
  44. * store.
  45. *
  46. * @param {Store} store - The redux store in which the specified action is being
  47. * dispatched.
  48. * @param {any} listener - Listener for app state status.
  49. * @private
  50. * @returns {Object} The value returned by {@code next(action)}.
  51. */
  52. function _setAppStateListener({ dispatch, getState }: IStore, listener: any) {
  53. const { subscription } = getState()['features/background'];
  54. subscription?.remove();
  55. dispatch(_setAppStateSubscription(listener ? AppState.addEventListener('change', listener) : undefined));
  56. }