2022-07-29 13:18:14 +00:00
|
|
|
import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app/actionTypes';
|
|
|
|
import MiddlewareRegistry from '../redux/MiddlewareRegistry';
|
2019-07-10 11:02:27 +00:00
|
|
|
|
2019-07-13 13:59:58 +00:00
|
|
|
import { USER_INTERACTION_RECEIVED } from './actionTypes';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to any callback that has been created to be invoked on user
|
|
|
|
* interaction.
|
|
|
|
*
|
|
|
|
* @type {Function|null}
|
|
|
|
*/
|
2022-07-29 13:18:14 +00:00
|
|
|
let userInteractionListener: Function|null = null;
|
2019-07-10 11:02:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements the entry point of the middleware of the feature base/user-interaction.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
|
|
|
case APP_WILL_MOUNT:
|
|
|
|
_startListeningForUserInteraction(store);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case APP_WILL_UNMOUNT:
|
2019-07-13 13:59:58 +00:00
|
|
|
_stopListeningForUserInteraction();
|
2019-07-10 11:02:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
2019-07-13 13:59:58 +00:00
|
|
|
/**
|
|
|
|
* Callback invoked when the user interacts with the page.
|
|
|
|
*
|
|
|
|
* @param {Function} dispatch - The redux dispatch function.
|
|
|
|
* @param {Object} event - The DOM event for a user interacting with the page.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-07-29 13:18:14 +00:00
|
|
|
function _onUserInteractionReceived(dispatch: Function, event: any) {
|
2019-07-13 13:59:58 +00:00
|
|
|
if (event.isTrusted) {
|
|
|
|
dispatch({
|
|
|
|
type: USER_INTERACTION_RECEIVED
|
|
|
|
});
|
|
|
|
|
|
|
|
_stopListeningForUserInteraction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 11:02:27 +00:00
|
|
|
/**
|
|
|
|
* Registers listeners to notify redux of any user interaction with the page.
|
|
|
|
*
|
|
|
|
* @param {Object} store - The redux store.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-07-29 13:18:14 +00:00
|
|
|
function _startListeningForUserInteraction({ dispatch }: {dispatch: Function}) {
|
2019-07-13 13:59:58 +00:00
|
|
|
_stopListeningForUserInteraction();
|
2019-07-10 11:02:27 +00:00
|
|
|
|
2019-07-13 13:59:58 +00:00
|
|
|
userInteractionListener = _onUserInteractionReceived.bind(null, dispatch);
|
2019-07-10 11:02:27 +00:00
|
|
|
|
2022-07-29 13:18:14 +00:00
|
|
|
// @ts-ignore
|
2019-07-10 11:02:27 +00:00
|
|
|
window.addEventListener('mousedown', userInteractionListener);
|
2022-07-29 13:18:14 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
2019-07-10 11:02:27 +00:00
|
|
|
window.addEventListener('keydown', userInteractionListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-13 13:59:58 +00:00
|
|
|
* De-registers listeners for user interaction with the page.
|
2019-07-10 11:02:27 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-07-13 13:59:58 +00:00
|
|
|
function _stopListeningForUserInteraction() {
|
2022-07-29 13:18:14 +00:00
|
|
|
// @ts-ignore
|
2019-07-13 13:59:58 +00:00
|
|
|
window.removeEventListener('mousedown', userInteractionListener);
|
2022-07-29 13:18:14 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
2019-07-13 13:59:58 +00:00
|
|
|
window.removeEventListener('keydown', userInteractionListener);
|
2019-07-10 11:02:27 +00:00
|
|
|
|
2019-07-13 13:59:58 +00:00
|
|
|
userInteractionListener = null;
|
2019-07-10 11:02:27 +00:00
|
|
|
}
|