Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app/actionTypes';
  2. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  3. import { USER_INTERACTION_RECEIVED } from './actionTypes';
  4. /**
  5. * Reference to any callback that has been created to be invoked on user
  6. * interaction.
  7. *
  8. * @type {Function|null}
  9. */
  10. let userInteractionListener: Function | null = null;
  11. /**
  12. * Implements the entry point of the middleware of the feature base/user-interaction.
  13. *
  14. * @param {Store} store - The redux store.
  15. * @returns {Function}
  16. */
  17. MiddlewareRegistry.register(store => next => action => {
  18. switch (action.type) {
  19. case APP_WILL_MOUNT:
  20. _startListeningForUserInteraction(store);
  21. break;
  22. case APP_WILL_UNMOUNT:
  23. _stopListeningForUserInteraction();
  24. break;
  25. }
  26. return next(action);
  27. });
  28. /**
  29. * Callback invoked when the user interacts with the page.
  30. *
  31. * @param {Function} dispatch - The redux dispatch function.
  32. * @param {Object} event - The DOM event for a user interacting with the page.
  33. * @private
  34. * @returns {void}
  35. */
  36. function _onUserInteractionReceived(dispatch: Function, event: any) {
  37. if (event.isTrusted) {
  38. dispatch({
  39. type: USER_INTERACTION_RECEIVED
  40. });
  41. _stopListeningForUserInteraction();
  42. }
  43. }
  44. /**
  45. * Registers listeners to notify redux of any user interaction with the page.
  46. *
  47. * @param {Object} store - The redux store.
  48. * @private
  49. * @returns {void}
  50. */
  51. function _startListeningForUserInteraction({ dispatch }: { dispatch: Function; }) {
  52. _stopListeningForUserInteraction();
  53. userInteractionListener = _onUserInteractionReceived.bind(null, dispatch);
  54. // @ts-ignore
  55. window.addEventListener('mousedown', userInteractionListener);
  56. // @ts-ignore
  57. window.addEventListener('keydown', userInteractionListener);
  58. }
  59. /**
  60. * De-registers listeners for user interaction with the page.
  61. *
  62. * @private
  63. * @returns {void}
  64. */
  65. function _stopListeningForUserInteraction() {
  66. // @ts-ignore
  67. window.removeEventListener('mousedown', userInteractionListener);
  68. // @ts-ignore
  69. window.removeEventListener('keydown', userInteractionListener);
  70. userInteractionListener = null;
  71. }