Browse Source

Remove duplication

master
Lyubo Marinov 7 years ago
parent
commit
6545a7a1bb

+ 2
- 2
react/features/app/components/AbstractApp.js View File

@@ -93,7 +93,7 @@ export class AbstractApp extends Component {
93 93
      * @inheritdoc
94 94
      */
95 95
     componentWillMount() {
96
-        const dispatch = this._getStore().dispatch;
96
+        const { dispatch } = this._getStore();
97 97
 
98 98
         dispatch(appWillMount(this));
99 99
 
@@ -163,7 +163,7 @@ export class AbstractApp extends Component {
163 163
      * @inheritdoc
164 164
      */
165 165
     componentWillUnmount() {
166
-        const dispatch = this._getStore().dispatch;
166
+        const { dispatch } = this._getStore();
167 167
 
168 168
         dispatch(localParticipantLeft());
169 169
 

+ 3
- 6
react/features/app/functions.native.js View File

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

+ 10
- 12
react/features/app/functions.web.js View File

@@ -1,6 +1,7 @@
1 1
 /* @flow */
2 2
 
3 3
 import { Platform } from '../base/react';
4
+import { toState } from '../base/redux';
4 5
 import {
5 6
     NoMobileApp,
6 7
     PluginRequiredBrowser,
@@ -22,7 +23,7 @@ declare var loggingConfig: Object;
22 23
  * render.
23 24
  *
24 25
  * @private
25
- * @param {Object} state - Object containing current Redux state.
26
+ * @param {Object} state - Object containing current redux state.
26 27
  * @returns {ReactElement|void}
27 28
  * @type {Function[]}
28 29
  */
@@ -34,7 +35,7 @@ const _INTERCEPT_COMPONENT_RULES = [
34 35
      * app even if the browser supports the app (e.g. Google Chrome with
35 36
      * WebRTC support on Android).
36 37
      *
37
-     * @param {Object} state - Redux state of the app.
38
+     * @param {Object} state - The redux state of the app.
38 39
      * @returns {UnsupportedMobileBrowser|void} If the rule is satisfied then
39 40
      * we should intercept existing component by UnsupportedMobileBrowser.
40 41
      */
@@ -73,11 +74,11 @@ const _INTERCEPT_COMPONENT_RULES = [
73 74
 ];
74 75
 
75 76
 /**
76
- * Determines which route is to be rendered in order to depict a specific Redux
77
+ * Determines which route is to be rendered in order to depict a specific redux
77 78
  * store.
78 79
  *
79
- * @param {(Object|Function)} stateOrGetState - Redux state or Regux getState()
80
- * method.
80
+ * @param {(Object|Function)} stateOrGetState - The redux state or
81
+ * {@link getState} function.
81 82
  * @returns {Route}
82 83
  */
83 84
 export function _getRouteToRender(stateOrGetState: Object | Function) {
@@ -93,21 +94,18 @@ export function _getRouteToRender(stateOrGetState: Object | Function) {
93 94
 /**
94 95
  * Intercepts route components based on a {@link _INTERCEPT_COMPONENT_RULES}.
95 96
  *
96
- * @param {Object|Function} stateOrGetState - Either Redux state object or
97
- * getState() function.
97
+ * @param {Object|Function} stateOrGetState - The redux state or
98
+ * {@link getState} function.
98 99
  * @param {ReactElement} component - Current route component to render.
99 100
  * @private
100 101
  * @returns {ReactElement} If any of the pre-defined rules is satisfied, returns
101 102
  * intercepted component.
102 103
  */
103 104
 function _interceptComponent(
104
-        stateOrGetState: Object,
105
+        stateOrGetState: Object | Function,
105 106
         component: ReactElement<*>) {
106 107
     let result;
107
-    const state
108
-        = typeof stateOrGetState === 'function'
109
-            ? stateOrGetState()
110
-            : stateOrGetState;
108
+    const state = toState(stateOrGetState);
111 109
 
112 110
     for (const rule of _INTERCEPT_COMPONENT_RULES) {
113 111
         result = rule(state);

+ 4
- 10
react/features/base/connection/functions.js View File

@@ -1,5 +1,7 @@
1 1
 /* @flow */
2 2
 
3
+import { toState } from '../redux';
4
+
3 5
 /**
4 6
  * Retrieves a simplified version of the conference/location URL stripped of URL
5 7
  * params (i.e. query/search and hash) which should be used for sending invites.
@@ -9,21 +11,13 @@
9 11
  * @returns {string|undefined}
10 12
  */
11 13
 export function getInviteURL(stateOrGetState: Function | Object): ?string {
12
-    const state
13
-        = typeof stateOrGetState === 'function'
14
-            ? stateOrGetState()
15
-            : stateOrGetState;
14
+    const state = toState(stateOrGetState);
16 15
     const locationURL
17 16
         = state instanceof URL
18 17
             ? state
19 18
             : state['features/base/connection'].locationURL;
20
-    let inviteURL;
21
-
22
-    if (locationURL) {
23
-        inviteURL = getURLWithoutParams(locationURL).href;
24
-    }
25 19
 
26
-    return inviteURL;
20
+    return locationURL ? getURLWithoutParams(locationURL).href : undefined;
27 21
 }
28 22
 
29 23
 /**

+ 1
- 1
react/features/base/lib-jitsi-meet/native/polyfills-browser.js View File

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

+ 21
- 19
react/features/base/participants/functions.js View File

@@ -1,3 +1,7 @@
1
+/* @flow */
2
+
3
+import { toState } from '../redux';
4
+
1 5
 import { DEFAULT_AVATAR_RELATIVE_PATH } from './constants';
2 6
 
3 7
 declare var config: Object;
@@ -13,20 +17,22 @@ declare var MD5: Object;
13 17
  * @param {string} [participant.avatarURL] - Participant's avatar URL.
14 18
  * @param {string} [participant.email] - Participant's e-mail address.
15 19
  * @param {string} [participant.id] - Participant's ID.
20
+ * @public
16 21
  * @returns {string} The URL of the image for the avatar of the specified
17 22
  * participant.
18
- *
19
- * @public
20 23
  */
21
-export function getAvatarURL(participant) {
24
+export function getAvatarURL({ avatarID, avatarURL, email, id }: {
25
+        avatarID: string,
26
+        avatarURL: string,
27
+        email: string,
28
+        id: string
29
+}) {
22 30
     // If disableThirdPartyRequests disables third-party avatar services, we are
23 31
     // restricted to a stock image of ours.
24 32
     if (typeof config === 'object' && config.disableThirdPartyRequests) {
25 33
         return DEFAULT_AVATAR_RELATIVE_PATH;
26 34
     }
27 35
 
28
-    const { avatarID, avatarURL, email, id } = participant;
29
-
30 36
     // If an avatarURL is specified, then obviously there's nothing to generate.
31 37
     if (avatarURL) {
32 38
         return avatarURL;
@@ -77,7 +83,7 @@ export function getAvatarURL(participant) {
77 83
  * features/base/participants state.
78 84
  * @returns {(Participant|undefined)}
79 85
  */
80
-export function getLocalParticipant(stateOrGetState) {
86
+export function getLocalParticipant(stateOrGetState: Object | Function) {
81 87
     const participants = _getParticipants(stateOrGetState);
82 88
 
83 89
     return participants.find(p => p.local);
@@ -94,7 +100,9 @@ export function getLocalParticipant(stateOrGetState) {
94 100
  * @private
95 101
  * @returns {(Participant|undefined)}
96 102
  */
97
-export function getParticipantById(stateOrGetState, id) {
103
+export function getParticipantById(
104
+        stateOrGetState: Object | Function,
105
+        id: string) {
98 106
     const participants = _getParticipants(stateOrGetState);
99 107
 
100 108
     return participants.find(p => p.id === id);
@@ -110,7 +118,7 @@ export function getParticipantById(stateOrGetState, id) {
110 118
  * features/base/participants state.
111 119
  * @returns {number}
112 120
  */
113
-export function getParticipantCount(stateOrGetState) {
121
+export function getParticipantCount(stateOrGetState: Object | Function) {
114 122
     const participants = _getParticipants(stateOrGetState);
115 123
     const realParticipants = participants.filter(p => !p.isBot);
116 124
 
@@ -126,7 +134,7 @@ export function getParticipantCount(stateOrGetState) {
126 134
  * features/base/participants state.
127 135
  * @returns {(Participant|undefined)}
128 136
  */
129
-export function getPinnedParticipant(stateOrGetState) {
137
+export function getPinnedParticipant(stateOrGetState: Object | Function) {
130 138
     const participants = _getParticipants(stateOrGetState);
131 139
 
132 140
     return participants.find(p => p.pinned);
@@ -143,14 +151,8 @@ export function getPinnedParticipant(stateOrGetState) {
143 151
  * @returns {Participant[]}
144 152
  */
145 153
 function _getParticipants(stateOrGetState) {
146
-    if (Array.isArray(stateOrGetState)) {
147
-        return stateOrGetState;
148
-    }
149
-
150
-    const state
151
-        = typeof stateOrGetState === 'function'
152
-            ? stateOrGetState()
153
-            : stateOrGetState;
154
-
155
-    return state['features/base/participants'] || [];
154
+    return (
155
+        Array.isArray(stateOrGetState)
156
+            ? stateOrGetState
157
+            : toState(stateOrGetState)['features/base/participants'] || []);
156 158
 }

+ 27
- 4
react/features/base/redux/functions.js View File

@@ -1,3 +1,5 @@
1
+/* @flow */
2
+
1 3
 import _ from 'lodash';
2 4
 
3 5
 /**
@@ -13,7 +15,7 @@ import _ from 'lodash';
13 15
  * from the specified target by setting the specified properties to the
14 16
  * specified values.
15 17
  */
16
-export function assign(target, source) {
18
+export function assign(target: Object, source: Object) {
17 19
     let t = target;
18 20
 
19 21
     for (const property in source) { // eslint-disable-line guard-for-in
@@ -32,7 +34,7 @@ export function assign(target, source) {
32 34
  * @returns {boolean} True if {@code a} equals {@code b} (according to deep
33 35
  * comparison); false, otherwise.
34 36
  */
35
-export function equals(a, b) {
37
+export function equals(a: any, b: any) {
36 38
     return _.isEqual(a, b);
37 39
 }
38 40
 
@@ -52,7 +54,7 @@ export function equals(a, b) {
52 54
  * constructed from the specified <tt>state</tt> by setting the specified
53 55
  * <tt>property</tt> to the specified <tt>value</tt>.
54 56
  */
55
-export function set(state, property, value) {
57
+export function set(state: Object, property: string, value: any) {
56 58
     return _set(state, property, value, /* copyOnWrite */ true);
57 59
 }
58 60
 
@@ -77,7 +79,11 @@ export function set(state, property, value) {
77 79
  * <tt>state</tt> by setting the specified <tt>property</tt> to the specified
78 80
  * <tt>value</tt>.
79 81
  */
80
-function _set(state, property, value, copyOnWrite) {
82
+function _set(
83
+        state: Object,
84
+        property: string,
85
+        value: any,
86
+        copyOnWrite: boolean) {
81 87
     // Delete state properties that are to be set to undefined. (It is a matter
82 88
     // of personal preference, mostly.)
83 89
     if (typeof value === 'undefined'
@@ -104,3 +110,20 @@ function _set(state, property, value, copyOnWrite) {
104 110
 }
105 111
 
106 112
 /* eslint-enable max-params */
113
+
114
+/**
115
+ * If the specified <tt>stateOrGetState</tt> is a function, it is presumed to be
116
+ * the redux {@link getState} function, it is invoked, and its return value is
117
+ * returned; otherwise, <tt>stateOrGetState</tt> is presumed to be the redux
118
+ * state and is returned.
119
+ *
120
+ * @param {Object|Function} stateOrGetState - The redux state or
121
+ * {@link getState} function.
122
+ * @returns {Object} The redux state.
123
+ */
124
+export function toState(stateOrGetState: Object | Function) {
125
+    return (
126
+        typeof stateOrGetState === 'function'
127
+            ? stateOrGetState()
128
+            : stateOrGetState);
129
+}

+ 1
- 3
react/index.native.js View File

@@ -90,9 +90,7 @@ class Root extends Component {
90 90
      * @inheritdoc
91 91
      */
92 92
     componentWillReceiveProps({ url }) {
93
-        if (!equals(this.props.url, url)) {
94
-            this.setState({ url: url || null });
95
-        }
93
+        equals(this.props.url, url) || this.setState({ url: url || null });
96 94
     }
97 95
 
98 96
     /**

Loading…
Cancel
Save