您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.native.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // @flow
  2. import { Dimensions } from 'react-native';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../base/app';
  4. import { MiddlewareRegistry } from '../../base/redux';
  5. import { setAspectRatio, setReducedUI } from './actions';
  6. /**
  7. * Dimensions change handler.
  8. */
  9. let handler;
  10. /**
  11. * Middleware that handles widnow dimension changes and updates the aspect ratio and
  12. * reduced UI modes accordingly.
  13. *
  14. * @param {Store} store - The redux store.
  15. * @returns {Function}
  16. */
  17. MiddlewareRegistry.register(store => next => action => {
  18. const result = next(action);
  19. switch (action.type) {
  20. case APP_WILL_UNMOUNT: {
  21. _appWillUnmount();
  22. break;
  23. }
  24. case APP_WILL_MOUNT:
  25. _appWillMount(store);
  26. break;
  27. }
  28. return result;
  29. });
  30. /**
  31. * Notifies this feature that the action {@link APP_WILL_MOUNT} is being
  32. * dispatched within a specific redux {@code store}.
  33. *
  34. * @param {Store} store - The redux store in which the specified {@code action}
  35. * is being dispatched.
  36. * @private
  37. * @returns {void}
  38. */
  39. function _appWillMount(store) {
  40. handler = dim => {
  41. _onDimensionsChange(dim, store);
  42. };
  43. Dimensions.addEventListener('change', handler);
  44. }
  45. /**
  46. * Notifies this feature that the action {@link APP_WILL_UNMOUNT} is being
  47. * dispatched within a specific redux {@code store}.
  48. *
  49. * @private
  50. * @returns {void}
  51. */
  52. function _appWillUnmount() {
  53. Dimensions.removeEventListener('change', handler);
  54. handler = undefined;
  55. }
  56. /**
  57. * Handles window dimension changes.
  58. *
  59. * @param {Object} dimensions - The new dimensions.
  60. * @param {Store} store - The redux store.
  61. * @private
  62. * @returns {void}
  63. */
  64. function _onDimensionsChange(dimensions, store) {
  65. const { width, height } = dimensions.window;
  66. const { dispatch } = store;
  67. dispatch(setAspectRatio(width, height));
  68. dispatch(setReducedUI(width, height));
  69. }