lastn: simplify computing the effective last N value

This commit is contained in:
Saúl Ibarra Corretgé 2019-08-02 14:23:09 +02:00 committed by Saúl Ibarra Corretgé
parent 467c9d36cf
commit bf50a110c7
4 changed files with 29 additions and 169 deletions

View File

@ -1,11 +0,0 @@
// @flow
/**
* The type of (redux) action which sets the video channel's lastN (value).
*
* {
* type: SET_LASTN,
* lastN: number
* }
*/
export const SET_LASTN = 'SET_LASTN';

View File

@ -1,34 +0,0 @@
// @flow
import { SET_LASTN } from './actionTypes';
import type { Dispatch } from 'redux';
/**
* Sets the video channel's last N (value) of the current conference. A value of
* undefined shall be used to reset it to the default value.
*
* @param {(number|undefined)} lastN - The last N value to be set.
* @returns {Function}
*/
export function setLastN(lastN: ?number) {
return (dispatch: Dispatch<any>, getState: Function) => {
if (typeof lastN === 'undefined') {
const config = getState()['features/base/config'];
/* eslint-disable no-param-reassign */
lastN = config.channelLastN;
if (typeof lastN === 'undefined') {
lastN = -1;
}
/* eslint-enable no-param-reassign */
}
dispatch({
type: SET_LASTN,
lastN
});
};
}

View File

@ -1,6 +1,3 @@
// @flow
export * from './actions';
export * from './actionTypes';
import './middleware';

View File

@ -9,152 +9,60 @@ import { SET_AUDIO_ONLY } from '../audio-only';
import { CONFERENCE_JOINED } from '../conference/actionTypes';
import { MiddlewareRegistry } from '../redux';
import { setLastN } from './actions';
import { SET_LASTN } from './actionTypes';
declare var APP: Object;
const logger = getLogger('features/base/lastn');
MiddlewareRegistry.register(store => next => action => {
const result = next(action);
switch (action.type) {
case APP_STATE_CHANGED:
return _appStateChanged(store, next, action);
case CONFERENCE_JOINED:
return _conferenceJoined(store, next, action);
case SET_AUDIO_ONLY:
return _setAudioOnly(store, next, action);
case SET_FILMSTRIP_ENABLED:
return _setFilmstripEnabled(store, next, action);
case SET_LASTN:
return _setLastN(store, next, action);
_updateLastN(store);
break;
}
return next(action);
return result;
});
/**
* Adjusts the lasN value based on the app state.
* Updates the last N value in the conference based on the current state of the redux store.
*
* @param {Store} store - The redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The redux action {@code APP_STATE_CHANGED} which is
* being dispatched in the specified {@code store}.
* @param {Store} store - The redux store.
* @private
* @returns {Object} The value returned by {@code next(action)}.
* @returns {void}
*/
function _appStateChanged({ dispatch, getState }, next, action) {
const { enabled: audioOnly } = getState()['features/base/audio-only'];
function _updateLastN({ getState }) {
const state = getState();
const { conference } = state['features/base/conference'];
const { enabled: audioOnly } = state['features/base/audio-only'];
const { appState } = state['features/background'];
const { enabled: filmStripEnabled } = state['features/filmstrip'];
const config = state['features/base/config'];
if (!audioOnly) {
const { appState } = action;
const lastN = appState === 'active' ? undefined : 0;
if (!conference) {
logger.debug('There is no active conference, not updating last N');
dispatch(setLastN(lastN));
logger.log(`App state changed - updated lastN to ${String(lastN)}`);
return;
}
return next(action);
}
const defaultLastN = typeof config.channelLastN === 'undefined' ? -1 : config.channelLastN;
let lastN = defaultLastN;
/**
* Adjusts the lasN value upon joining a conference.
*
* @param {Store} store - The redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
* being dispatched in the specified {@code store}.
* @private
* @returns {Object} The value returned by {@code next(action)}.
*/
function _conferenceJoined({ dispatch, getState }, next, action) {
const { conference } = action;
const { enabled: audioOnly } = getState()['features/base/audio-only'];
audioOnly && conference.getLastN() !== 0 && dispatch(setLastN(0));
return next(action);
}
/**
* Sets the audio-only flag for the current conference. When audio-only is set,
* local video is muted and last N is set to 0 to avoid receiving remote video.
*
* @param {Store} store - The redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The redux action {@code SET_AUDIO_ONLY} which is
* being dispatched in the specified {@code store}.
* @private
* @returns {Object} The value returned by {@code next(action)}.
*/
function _setAudioOnly({ dispatch }, next, action) {
const { audioOnly } = action;
// Set lastN to 0 in case audio-only is desired; leave it as undefined,
// otherwise, and the default lastN value will be chosen automatically.
dispatch(setLastN(audioOnly ? 0 : undefined));
return next(action);
}
/**
* Notifies the feature filmstrip that the action {@link SET_FILMSTRIP_ENABLED}
* is being dispatched within a specific redux store.
*
* @param {Store} store - The redux store in which the specified action is being
* dispatched.
* @param {Dispatch} next - The redux dispatch function to dispatch the
* specified action to the specified store.
* @param {Action} action - The redux action {@code SET_FILMSTRIP_ENABLED} which
* is being dispatched in the specified store.
* @private
* @returns {Object} The value returned by {@code next(action)}.
*/
function _setFilmstripEnabled({ dispatch, getState }, next, action) {
// FIXME This action is not currently dispatched on web.
if (typeof APP === 'undefined') {
const { enabled } = action;
const { enabled: audioOnly } = getState()['features/base/audio-only'];
audioOnly || dispatch(setLastN(enabled ? undefined : 1));
if (audioOnly || appState !== 'active') {
lastN = 0;
} else if (!filmStripEnabled) {
lastN = 1;
}
return next(action);
}
logger.info(`Setting last N to: ${lastN}`);
/**
* Sets the last N (value) of the video channel in the conference.
*
* @param {Store} store - The redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The redux action {@code SET_LASTN} which is being
* dispatched in the specified {@code store}.
* @private
* @returns {Object} The value returned by {@code next(action)}.
*/
function _setLastN({ getState }, next, action) {
const { conference } = getState()['features/base/conference'];
if (conference) {
try {
conference.setLastN(action.lastN);
} catch (err) {
logger.error(`Failed to set lastN: ${err}`);
}
try {
conference.setLastN(lastN);
} catch (err) {
logger.error(`Failed to set lastN: ${err}`);
}
return next(action);
}