This commit is contained in:
9
react/features/base/user-interaction/actionTypes.ts
Normal file
9
react/features/base/user-interaction/actionTypes.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* The type of (redux) action which signals the user has interacted with the
|
||||
* page.
|
||||
*
|
||||
* {
|
||||
* type: USER_INTERACTION_RECEIVED,
|
||||
* }
|
||||
*/
|
||||
export const USER_INTERACTION_RECEIVED = 'USER_INTERACTION_RECEIVED';
|
||||
86
react/features/base/user-interaction/middleware.ts
Normal file
86
react/features/base/user-interaction/middleware.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { IStore } from '../../app/types';
|
||||
import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app/actionTypes';
|
||||
import MiddlewareRegistry from '../redux/MiddlewareRegistry';
|
||||
|
||||
import { USER_INTERACTION_RECEIVED } from './actionTypes';
|
||||
|
||||
/**
|
||||
* Reference to any callback that has been created to be invoked on user
|
||||
* interaction.
|
||||
*
|
||||
* @type {Function|null}
|
||||
*/
|
||||
let userInteractionListener: Function | null = null;
|
||||
|
||||
/**
|
||||
* 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:
|
||||
_stopListeningForUserInteraction();
|
||||
break;
|
||||
}
|
||||
|
||||
return next(action);
|
||||
});
|
||||
|
||||
/**
|
||||
* 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}
|
||||
*/
|
||||
function _onUserInteractionReceived(dispatch: IStore['dispatch'], event: any) {
|
||||
if (event.isTrusted) {
|
||||
dispatch({
|
||||
type: USER_INTERACTION_RECEIVED
|
||||
});
|
||||
|
||||
_stopListeningForUserInteraction();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers listeners to notify redux of any user interaction with the page.
|
||||
*
|
||||
* @param {Object} store - The redux store.
|
||||
* @private
|
||||
* @returns {void}
|
||||
*/
|
||||
function _startListeningForUserInteraction({ dispatch }: { dispatch: IStore['dispatch']; }) {
|
||||
_stopListeningForUserInteraction();
|
||||
|
||||
userInteractionListener = _onUserInteractionReceived.bind(null, dispatch);
|
||||
|
||||
// @ts-ignore
|
||||
window.addEventListener('mousedown', userInteractionListener);
|
||||
|
||||
// @ts-ignore
|
||||
window.addEventListener('keydown', userInteractionListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* De-registers listeners for user interaction with the page.
|
||||
*
|
||||
* @private
|
||||
* @returns {void}
|
||||
*/
|
||||
function _stopListeningForUserInteraction() {
|
||||
// @ts-ignore
|
||||
window.removeEventListener('mousedown', userInteractionListener);
|
||||
|
||||
// @ts-ignore
|
||||
window.removeEventListener('keydown', userInteractionListener);
|
||||
|
||||
userInteractionListener = null;
|
||||
}
|
||||
29
react/features/base/user-interaction/reducer.ts
Normal file
29
react/features/base/user-interaction/reducer.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app/actionTypes';
|
||||
import ReducerRegistry from '../redux/ReducerRegistry';
|
||||
|
||||
import { USER_INTERACTION_RECEIVED } from './actionTypes';
|
||||
|
||||
export interface IUserInteractionState {
|
||||
interacted?: boolean;
|
||||
}
|
||||
|
||||
|
||||
ReducerRegistry.register<IUserInteractionState>('features/base/user-interaction',
|
||||
(state = {}, action): IUserInteractionState => {
|
||||
switch (action.type) {
|
||||
case APP_WILL_MOUNT:
|
||||
case APP_WILL_UNMOUNT:
|
||||
return {
|
||||
...state,
|
||||
interacted: false
|
||||
};
|
||||
|
||||
case USER_INTERACTION_RECEIVED:
|
||||
return {
|
||||
...state,
|
||||
interacted: true
|
||||
};
|
||||
}
|
||||
|
||||
return state;
|
||||
});
|
||||
Reference in New Issue
Block a user