Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

middleware.web.ts 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { IStore } from '../../app/types';
  2. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app/actionTypes';
  3. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  4. import { clientResized } from './actions';
  5. /**
  6. * Dimensions change handler.
  7. */
  8. let handler: undefined | ((this: Window, ev: UIEvent) => any);
  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: IStore) {
  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. handler && window.removeEventListener('resize', handler);
  56. handler = undefined;
  57. }