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