jiti-meet/react/features/toolbox/middleware.js

103 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-04-01 05:52:40 +00:00
/* @flow */
import {
SET_AUDIO_AVAILABLE,
SET_AUDIO_MUTED,
SET_VIDEO_AVAILABLE,
SET_VIDEO_MUTED } from '../base/media';
2017-04-01 05:52:40 +00:00
import { MiddlewareRegistry } from '../base/redux';
2017-07-16 08:44:07 +00:00
import { setToolbarButton } from './actions';
import { CLEAR_TOOLBOX_TIMEOUT, SET_TOOLBOX_TIMEOUT } from './actionTypes';
2017-04-01 05:52:40 +00:00
/**
* Middleware which intercepts Toolbox actions to handle changes to the
* visibility timeout of the Toolbox.
*
2017-07-16 08:44:07 +00:00
* @param {Store} store - The redux store.
2017-04-01 05:52:40 +00:00
* @returns {Function}
*/
MiddlewareRegistry.register(store => next => action => {
switch (action.type) {
case CLEAR_TOOLBOX_TIMEOUT: {
const { timeoutID } = store.getState()['features/toolbox'];
clearTimeout(timeoutID);
break;
}
case SET_TOOLBOX_TIMEOUT: {
const { timeoutID } = store.getState()['features/toolbox'];
const { handler, timeoutMS } = action;
clearTimeout(timeoutID);
const newTimeoutId = setTimeout(handler, timeoutMS);
action.timeoutID = newTimeoutId;
break;
}
case SET_AUDIO_AVAILABLE:
case SET_AUDIO_MUTED: {
return _setAudioAvailableOrMuted(store, next, action);
}
case SET_VIDEO_AVAILABLE:
2017-07-16 08:44:07 +00:00
case SET_VIDEO_MUTED:
return _setVideoAvailableOrMuted(store, next, action);
2017-04-01 05:52:40 +00:00
}
return next(action);
});
2017-07-16 08:44:07 +00:00
/**
* Adjusts the state of toolbar's microphone button.
*
* @param {Store} store - The Redux store instance.
* @param {Function} next - The redux function to continue dispatching the
* specified {@code action} in the specified {@code store}.
* @param {Object} action - Either SET_AUDIO_AVAILABLE or SET_AUDIO_MUTED.
*
* @returns {*}
*/
function _setAudioAvailableOrMuted({ dispatch, getState }, next, action) {
const result = next(action);
const { available, muted } = getState()['features/base/media'].audio;
const i18nKey = available ? 'mute' : 'micDisabled';
dispatch(setToolbarButton('microphone', {
enabled: available,
i18n: `[content]toolbar.${i18nKey}`,
toggled: available ? muted : true
}));
return result;
}
2017-07-16 08:44:07 +00:00
/**
* Adjusts the state of toolbar's camera button.
*
* @param {Store} store - The redux store.
* @param {Function} next - The redux function to continue dispatching the
* specified {@code action} in the specified {@code store}.
* @param {Object} action - Either {@link SET_VIDEO_AVAILABLE} or
* {@link SET_VIDEO_MUTED}.
* @returns {Object} The new state that is the result of the reduction of the
* specified {@code action}.
*/
function _setVideoAvailableOrMuted({ dispatch, getState }, next, action) {
const result = next(action);
const { available, muted } = getState()['features/base/media'].video;
const i18nKey = available ? 'videomute' : 'cameraDisabled';
dispatch(setToolbarButton('camera', {
enabled: available,
i18n: `[content]toolbar.${i18nKey}`,
toggled: available ? muted : true
}));
return result;
}