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.web.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // @flow
  2. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../base/app';
  3. import { MiddlewareRegistry } from '../../base/redux';
  4. import { clientResized } from './actions';
  5. /**
  6. * Dimensions change handler.
  7. */
  8. let handler;
  9. /**
  10. * Middleware that handles window dimension changes.
  11. *
  12. * @param {Store} store - The redux store.
  13. * @returns {Function}
  14. */
  15. MiddlewareRegistry.register(store => next => action => {
  16. const result = next(action);
  17. switch (action.type) {
  18. case APP_WILL_UNMOUNT: {
  19. _appWillUnmount();
  20. break;
  21. }
  22. case APP_WILL_MOUNT:
  23. _appWillMount(store);
  24. break;
  25. }
  26. return result;
  27. });
  28. /**
  29. * Notifies this feature that the action {@link APP_WILL_MOUNT} is being
  30. * dispatched within a specific redux {@code store}.
  31. *
  32. * @param {Store} store - The redux store in which the specified {@code action}
  33. * is being dispatched.
  34. * @private
  35. * @returns {void}
  36. */
  37. function _appWillMount(store) {
  38. handler = () => {
  39. const {
  40. innerHeight,
  41. innerWidth
  42. } = window;
  43. store.dispatch(clientResized(innerWidth, innerHeight));
  44. };
  45. window.addEventListener('resize', handler);
  46. }
  47. /**
  48. * Notifies this feature that the action {@link APP_WILL_UNMOUNT} is being
  49. * dispatched within a specific redux {@code store}.
  50. *
  51. * @private
  52. * @returns {void}
  53. */
  54. function _appWillUnmount() {
  55. window.removeEventListener('resize', handler);
  56. handler = undefined;
  57. }