Make getPropertyValue's config easier to use

This commit is contained in:
zbettenbuk 2018-04-13 12:57:12 +02:00 committed by Lyubo Marinov
parent 0539e8f2df
commit 1513e1f3b3
2 changed files with 59 additions and 43 deletions

View File

@ -60,12 +60,13 @@ function _setRoom({ dispatch, getState }, next, action) {
// Read the config. // Read the config.
const state = getState(); const state = getState();
const hasRoom = Boolean(room);
const audioMuted = Boolean(getPropertyValue(state, 'startWithAudioMuted', { const audioMuted = Boolean(getPropertyValue(state, 'startWithAudioMuted', {
ignoreUrlParams: !room urlParams: hasRoom
})); }));
const videoMuted = Boolean(getPropertyValue(state, 'startWithVideoMuted', { const videoMuted = Boolean(getPropertyValue(state, 'startWithVideoMuted', {
ignoreUrlParams: !room urlParams: hasRoom
})); }));
sendAnalytics(createStartMutedConfigurationEvent( sendAnalytics(createStartMutedConfigurationEvent(

View File

@ -4,64 +4,79 @@ import { parseURLParams } from '../config';
import { toState } from '../redux'; import { toState } from '../redux';
/** /**
* Returns the effective value of a property by applying a precedence * Returns the effective value of a configuration/preference/setting by applying
* between values in URL, config and profile. * a precedence among the values specified by JWT, URL, profile, and config.
* *
* @param {Object|Function} stateful - The redux state object or function * @param {Object|Function} stateful - The redux state object or
* to retreive the state. * {@code getState} function.
* @param {string} propertyName - The name of the property we need. * @param {string} propertyName - The name of the
* configuration/preference/setting (property) to retrieve.
* @param {{ * @param {{
* ignoreJWT: boolean, * config: boolean,
* ignoreUrlParams: boolean, * jwt: boolean,
* ignoreProfile: boolean, * profile: boolean,
* ignoreConfig: boolean * urlParams: boolean
* }} precedence - A structure of booleans to set which property sources * }} [sources] - A set/structure of {@code boolean} flags indicating the
* should be ignored. * configuration/preference/setting sources to consider/retrieve values from.
* @returns {any} * @returns {any}
*/ */
export function getPropertyValue( export function getPropertyValue(
stateful: Object | Function, stateful: Object | Function,
propertyName: string, propertyName: string,
precedence: Object = { sources?: Object
ignoreJWT: false,
ignoreUrlParams: false,
ignoreProfile: false,
ignoreConfig: false
}
) { ) {
// Default values don't play nicely with partial objects and we want to make
// the function easy to use without exhaustively defining all flags:
sources = { // eslint-disable-line no-param-reassign
// Defaults:
config: true,
jwt: true,
profile: true,
urlParams: true,
...sources
};
// Precedence: jwt -> urlParams -> profile -> config.
const state = toState(stateful); const state = toState(stateful);
const jwt = state['features/base/jwt'];
const urlParams
= parseURLParams(state['features/base/connection'].locationURL);
const profile = state['features/base/profile'];
const config = state['features/base/config'];
const urlParamName = `config.${propertyName}`;
// Precedence: jwt -> urlParams -> profile -> config // jwt
if (sources.jwt) {
const value = state['features/base/jwt'][propertyName];
if ( if (typeof value !== 'undefined') {
!precedence.ignoreJWT return value[propertyName];
&& typeof jwt[propertyName] !== 'undefined' }
) {
return jwt[propertyName];
} }
if ( // urlParams
!precedence.ignoreUrlParams if (sources.urlParams) {
&& typeof urlParams[urlParamName] !== 'undefined' const urlParams
) { = parseURLParams(state['features/base/connection'].locationURL);
return urlParams[urlParamName]; const value = urlParams[`config.${propertyName}`];
if (typeof value !== 'undefined') {
return value;
}
} }
if ( // profile
!precedence.ignoreProfile if (sources.profile) {
&& typeof profile[propertyName] !== 'undefined' const value = state['features/base/profile'][propertyName];
) {
return profile[propertyName]; if (typeof value !== 'undefined') {
return value;
}
} }
if (!precedence.ignoreConfig) { // config
return config[propertyName]; if (sources.config) {
const value = state['features/base/config'][propertyName];
if (typeof value !== 'undefined') {
return value;
}
} }
return undefined; return undefined;