Remove duplication

This commit is contained in:
Lyubo Marinov 2017-09-01 16:25:48 -05:00
parent ec9c05e401
commit 6545a7a1bb
8 changed files with 69 additions and 57 deletions

View File

@ -93,7 +93,7 @@ export class AbstractApp extends Component {
* @inheritdoc * @inheritdoc
*/ */
componentWillMount() { componentWillMount() {
const dispatch = this._getStore().dispatch; const { dispatch } = this._getStore();
dispatch(appWillMount(this)); dispatch(appWillMount(this));
@ -163,7 +163,7 @@ export class AbstractApp extends Component {
* @inheritdoc * @inheritdoc
*/ */
componentWillUnmount() { componentWillUnmount() {
const dispatch = this._getStore().dispatch; const { dispatch } = this._getStore();
dispatch(localParticipantLeft()); dispatch(localParticipantLeft());

View File

@ -2,6 +2,7 @@
import { isRoomValid } from '../base/conference'; import { isRoomValid } from '../base/conference';
import { RouteRegistry } from '../base/react'; import { RouteRegistry } from '../base/react';
import { toState } from '../base/redux';
import { Conference } from '../conference'; import { Conference } from '../conference';
import { Entryway } from '../welcome'; import { Entryway } from '../welcome';
@ -14,12 +15,8 @@ import { Entryway } from '../welcome';
* @returns {Route} * @returns {Route}
*/ */
export function _getRouteToRender(stateOrGetState: Object | Function) { export function _getRouteToRender(stateOrGetState: Object | Function) {
const state const { room } = toState(stateOrGetState)['features/base/conference'];
= typeof stateOrGetState === 'function' const component = isRoomValid(room) ? Conference : WelcomePage;
? stateOrGetState()
: stateOrGetState;
const { room } = state['features/base/conference'];
const component = isRoomValid(room) ? Conference : Entryway;
return RouteRegistry.getRouteByComponent(component); return RouteRegistry.getRouteByComponent(component);
} }

View File

@ -1,6 +1,7 @@
/* @flow */ /* @flow */
import { Platform } from '../base/react'; import { Platform } from '../base/react';
import { toState } from '../base/redux';
import { import {
NoMobileApp, NoMobileApp,
PluginRequiredBrowser, PluginRequiredBrowser,
@ -22,7 +23,7 @@ declare var loggingConfig: Object;
* render. * render.
* *
* @private * @private
* @param {Object} state - Object containing current Redux state. * @param {Object} state - Object containing current redux state.
* @returns {ReactElement|void} * @returns {ReactElement|void}
* @type {Function[]} * @type {Function[]}
*/ */
@ -34,7 +35,7 @@ const _INTERCEPT_COMPONENT_RULES = [
* app even if the browser supports the app (e.g. Google Chrome with * app even if the browser supports the app (e.g. Google Chrome with
* WebRTC support on Android). * WebRTC support on Android).
* *
* @param {Object} state - Redux state of the app. * @param {Object} state - The redux state of the app.
* @returns {UnsupportedMobileBrowser|void} If the rule is satisfied then * @returns {UnsupportedMobileBrowser|void} If the rule is satisfied then
* we should intercept existing component by UnsupportedMobileBrowser. * we should intercept existing component by UnsupportedMobileBrowser.
*/ */
@ -73,11 +74,11 @@ const _INTERCEPT_COMPONENT_RULES = [
]; ];
/** /**
* Determines which route is to be rendered in order to depict a specific Redux * Determines which route is to be rendered in order to depict a specific redux
* store. * store.
* *
* @param {(Object|Function)} stateOrGetState - Redux state or Regux getState() * @param {(Object|Function)} stateOrGetState - The redux state or
* method. * {@link getState} function.
* @returns {Route} * @returns {Route}
*/ */
export function _getRouteToRender(stateOrGetState: Object | Function) { export function _getRouteToRender(stateOrGetState: Object | Function) {
@ -93,21 +94,18 @@ export function _getRouteToRender(stateOrGetState: Object | Function) {
/** /**
* Intercepts route components based on a {@link _INTERCEPT_COMPONENT_RULES}. * Intercepts route components based on a {@link _INTERCEPT_COMPONENT_RULES}.
* *
* @param {Object|Function} stateOrGetState - Either Redux state object or * @param {Object|Function} stateOrGetState - The redux state or
* getState() function. * {@link getState} function.
* @param {ReactElement} component - Current route component to render. * @param {ReactElement} component - Current route component to render.
* @private * @private
* @returns {ReactElement} If any of the pre-defined rules is satisfied, returns * @returns {ReactElement} If any of the pre-defined rules is satisfied, returns
* intercepted component. * intercepted component.
*/ */
function _interceptComponent( function _interceptComponent(
stateOrGetState: Object, stateOrGetState: Object | Function,
component: ReactElement<*>) { component: ReactElement<*>) {
let result; let result;
const state const state = toState(stateOrGetState);
= typeof stateOrGetState === 'function'
? stateOrGetState()
: stateOrGetState;
for (const rule of _INTERCEPT_COMPONENT_RULES) { for (const rule of _INTERCEPT_COMPONENT_RULES) {
result = rule(state); result = rule(state);

View File

@ -1,5 +1,7 @@
/* @flow */ /* @flow */
import { toState } from '../redux';
/** /**
* Retrieves a simplified version of the conference/location URL stripped of URL * Retrieves a simplified version of the conference/location URL stripped of URL
* params (i.e. query/search and hash) which should be used for sending invites. * params (i.e. query/search and hash) which should be used for sending invites.
@ -9,21 +11,13 @@
* @returns {string|undefined} * @returns {string|undefined}
*/ */
export function getInviteURL(stateOrGetState: Function | Object): ?string { export function getInviteURL(stateOrGetState: Function | Object): ?string {
const state const state = toState(stateOrGetState);
= typeof stateOrGetState === 'function'
? stateOrGetState()
: stateOrGetState;
const locationURL const locationURL
= state instanceof URL = state instanceof URL
? state ? state
: state['features/base/connection'].locationURL; : state['features/base/connection'].locationURL;
let inviteURL;
if (locationURL) { return locationURL ? getURLWithoutParams(locationURL).href : undefined;
inviteURL = getURLWithoutParams(locationURL).href;
}
return inviteURL;
} }
/** /**

View File

@ -219,7 +219,7 @@ function _visitNode(node, callback) {
// then it doesn't sound like what expected. // then it doesn't sound like what expected.
&& nodePrototype !== Object.getPrototypeOf({})) { && nodePrototype !== Object.getPrototypeOf({})) {
// Override console.log. // Override console.log.
const console = global.console; const { console } = global;
if (console) { if (console) {
const loggerLevels = require('jitsi-meet-logger').levels; const loggerLevels = require('jitsi-meet-logger').levels;

View File

@ -1,3 +1,7 @@
/* @flow */
import { toState } from '../redux';
import { DEFAULT_AVATAR_RELATIVE_PATH } from './constants'; import { DEFAULT_AVATAR_RELATIVE_PATH } from './constants';
declare var config: Object; declare var config: Object;
@ -13,20 +17,22 @@ declare var MD5: Object;
* @param {string} [participant.avatarURL] - Participant's avatar URL. * @param {string} [participant.avatarURL] - Participant's avatar URL.
* @param {string} [participant.email] - Participant's e-mail address. * @param {string} [participant.email] - Participant's e-mail address.
* @param {string} [participant.id] - Participant's ID. * @param {string} [participant.id] - Participant's ID.
* @public
* @returns {string} The URL of the image for the avatar of the specified * @returns {string} The URL of the image for the avatar of the specified
* participant. * participant.
*
* @public
*/ */
export function getAvatarURL(participant) { export function getAvatarURL({ avatarID, avatarURL, email, id }: {
avatarID: string,
avatarURL: string,
email: string,
id: string
}) {
// If disableThirdPartyRequests disables third-party avatar services, we are // If disableThirdPartyRequests disables third-party avatar services, we are
// restricted to a stock image of ours. // restricted to a stock image of ours.
if (typeof config === 'object' && config.disableThirdPartyRequests) { if (typeof config === 'object' && config.disableThirdPartyRequests) {
return DEFAULT_AVATAR_RELATIVE_PATH; return DEFAULT_AVATAR_RELATIVE_PATH;
} }
const { avatarID, avatarURL, email, id } = participant;
// If an avatarURL is specified, then obviously there's nothing to generate. // If an avatarURL is specified, then obviously there's nothing to generate.
if (avatarURL) { if (avatarURL) {
return avatarURL; return avatarURL;
@ -77,7 +83,7 @@ export function getAvatarURL(participant) {
* features/base/participants state. * features/base/participants state.
* @returns {(Participant|undefined)} * @returns {(Participant|undefined)}
*/ */
export function getLocalParticipant(stateOrGetState) { export function getLocalParticipant(stateOrGetState: Object | Function) {
const participants = _getParticipants(stateOrGetState); const participants = _getParticipants(stateOrGetState);
return participants.find(p => p.local); return participants.find(p => p.local);
@ -94,7 +100,9 @@ export function getLocalParticipant(stateOrGetState) {
* @private * @private
* @returns {(Participant|undefined)} * @returns {(Participant|undefined)}
*/ */
export function getParticipantById(stateOrGetState, id) { export function getParticipantById(
stateOrGetState: Object | Function,
id: string) {
const participants = _getParticipants(stateOrGetState); const participants = _getParticipants(stateOrGetState);
return participants.find(p => p.id === id); return participants.find(p => p.id === id);
@ -110,7 +118,7 @@ export function getParticipantById(stateOrGetState, id) {
* features/base/participants state. * features/base/participants state.
* @returns {number} * @returns {number}
*/ */
export function getParticipantCount(stateOrGetState) { export function getParticipantCount(stateOrGetState: Object | Function) {
const participants = _getParticipants(stateOrGetState); const participants = _getParticipants(stateOrGetState);
const realParticipants = participants.filter(p => !p.isBot); const realParticipants = participants.filter(p => !p.isBot);
@ -126,7 +134,7 @@ export function getParticipantCount(stateOrGetState) {
* features/base/participants state. * features/base/participants state.
* @returns {(Participant|undefined)} * @returns {(Participant|undefined)}
*/ */
export function getPinnedParticipant(stateOrGetState) { export function getPinnedParticipant(stateOrGetState: Object | Function) {
const participants = _getParticipants(stateOrGetState); const participants = _getParticipants(stateOrGetState);
return participants.find(p => p.pinned); return participants.find(p => p.pinned);
@ -143,14 +151,8 @@ export function getPinnedParticipant(stateOrGetState) {
* @returns {Participant[]} * @returns {Participant[]}
*/ */
function _getParticipants(stateOrGetState) { function _getParticipants(stateOrGetState) {
if (Array.isArray(stateOrGetState)) { return (
return stateOrGetState; Array.isArray(stateOrGetState)
} ? stateOrGetState
: toState(stateOrGetState)['features/base/participants'] || []);
const state
= typeof stateOrGetState === 'function'
? stateOrGetState()
: stateOrGetState;
return state['features/base/participants'] || [];
} }

View File

@ -1,3 +1,5 @@
/* @flow */
import _ from 'lodash'; import _ from 'lodash';
/** /**
@ -13,7 +15,7 @@ import _ from 'lodash';
* from the specified target by setting the specified properties to the * from the specified target by setting the specified properties to the
* specified values. * specified values.
*/ */
export function assign(target, source) { export function assign(target: Object, source: Object) {
let t = target; let t = target;
for (const property in source) { // eslint-disable-line guard-for-in for (const property in source) { // eslint-disable-line guard-for-in
@ -32,7 +34,7 @@ export function assign(target, source) {
* @returns {boolean} True if {@code a} equals {@code b} (according to deep * @returns {boolean} True if {@code a} equals {@code b} (according to deep
* comparison); false, otherwise. * comparison); false, otherwise.
*/ */
export function equals(a, b) { export function equals(a: any, b: any) {
return _.isEqual(a, b); return _.isEqual(a, b);
} }
@ -52,7 +54,7 @@ export function equals(a, b) {
* constructed from the specified <tt>state</tt> by setting the specified * constructed from the specified <tt>state</tt> by setting the specified
* <tt>property</tt> to the specified <tt>value</tt>. * <tt>property</tt> to the specified <tt>value</tt>.
*/ */
export function set(state, property, value) { export function set(state: Object, property: string, value: any) {
return _set(state, property, value, /* copyOnWrite */ true); return _set(state, property, value, /* copyOnWrite */ true);
} }
@ -77,7 +79,11 @@ export function set(state, property, value) {
* <tt>state</tt> by setting the specified <tt>property</tt> to the specified * <tt>state</tt> by setting the specified <tt>property</tt> to the specified
* <tt>value</tt>. * <tt>value</tt>.
*/ */
function _set(state, property, value, copyOnWrite) { function _set(
state: Object,
property: string,
value: any,
copyOnWrite: boolean) {
// Delete state properties that are to be set to undefined. (It is a matter // Delete state properties that are to be set to undefined. (It is a matter
// of personal preference, mostly.) // of personal preference, mostly.)
if (typeof value === 'undefined' if (typeof value === 'undefined'
@ -104,3 +110,20 @@ function _set(state, property, value, copyOnWrite) {
} }
/* eslint-enable max-params */ /* eslint-enable max-params */
/**
* If the specified <tt>stateOrGetState</tt> is a function, it is presumed to be
* the redux {@link getState} function, it is invoked, and its return value is
* returned; otherwise, <tt>stateOrGetState</tt> is presumed to be the redux
* state and is returned.
*
* @param {Object|Function} stateOrGetState - The redux state or
* {@link getState} function.
* @returns {Object} The redux state.
*/
export function toState(stateOrGetState: Object | Function) {
return (
typeof stateOrGetState === 'function'
? stateOrGetState()
: stateOrGetState);
}

View File

@ -90,9 +90,7 @@ class Root extends Component {
* @inheritdoc * @inheritdoc
*/ */
componentWillReceiveProps({ url }) { componentWillReceiveProps({ url }) {
if (!equals(this.props.url, url)) { equals(this.props.url, url) || this.setState({ url: url || null });
this.setState({ url: url || null });
}
} }
/** /**