2022-10-20 09:11:27 +00:00
|
|
|
import { IReduxState } from '../app/types';
|
2022-09-29 11:45:34 +00:00
|
|
|
import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
|
2022-03-11 13:00:49 +00:00
|
|
|
|
|
|
|
import { ADD_GIF_FOR_PARTICIPANT, HIDE_GIF_FOR_PARTICIPANT, SHOW_GIF_FOR_PARTICIPANT } from './actionTypes';
|
|
|
|
import { removeGif } from './actions';
|
|
|
|
import { GIF_DEFAULT_TIMEOUT } from './constants';
|
2022-10-21 11:09:15 +00:00
|
|
|
import { getGifForParticipant } from './function.any';
|
2022-03-11 13:00:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware which intercepts Gifs actions to handle changes to the
|
|
|
|
* visibility timeout of the Gifs.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
const { dispatch, getState } = store;
|
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
case ADD_GIF_FOR_PARTICIPANT: {
|
|
|
|
const id = action.participantId;
|
|
|
|
const { giphy } = state['features/base/config'];
|
|
|
|
|
|
|
|
_clearGifTimeout(state, id);
|
|
|
|
const timeoutID = setTimeout(() => dispatch(removeGif(id)), giphy?.tileTime || GIF_DEFAULT_TIMEOUT);
|
|
|
|
|
|
|
|
action.timeoutID = timeoutID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SHOW_GIF_FOR_PARTICIPANT: {
|
|
|
|
const id = action.participantId;
|
|
|
|
|
|
|
|
_clearGifTimeout(state, id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HIDE_GIF_FOR_PARTICIPANT: {
|
|
|
|
const { giphy } = state['features/base/config'];
|
|
|
|
const id = action.participantId;
|
|
|
|
const timeoutID = setTimeout(() => dispatch(removeGif(id)), giphy?.tileTime || GIF_DEFAULT_TIMEOUT);
|
|
|
|
|
|
|
|
action.timeoutID = timeoutID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears GIF timeout.
|
|
|
|
*
|
2022-10-20 09:11:27 +00:00
|
|
|
* @param {IReduxState} state - Redux state.
|
2022-03-11 13:00:49 +00:00
|
|
|
* @param {string} id - Id of the participant for whom to clear the timeout.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
function _clearGifTimeout(state: IReduxState, id: string) {
|
2022-03-11 13:00:49 +00:00
|
|
|
const gif = getGifForParticipant(state, id);
|
|
|
|
|
2022-10-21 11:09:15 +00:00
|
|
|
clearTimeout(gif?.timeoutID ?? -1);
|
2022-03-11 13:00:49 +00:00
|
|
|
}
|