瀏覽代碼

Convert a function to an asynchronous redux action creator

j8
Lyubo Marinov 8 年之前
父節點
當前提交
dcc6ce025f

+ 3
- 3
react/features/base/media/middleware.js 查看文件

93
  * @private
93
  * @private
94
  * @returns {void}
94
  * @returns {void}
95
  */
95
  */
96
-function _syncTrackMutedState(store, track) {
97
-    const state = store.getState()['features/base/media'];
96
+function _syncTrackMutedState({ dispatch, getState }, track) {
97
+    const state = getState()['features/base/media'];
98
     const muted = state[track.mediaType].muted;
98
     const muted = state[track.mediaType].muted;
99
 
99
 
100
     // XXX If muted state of track when it was added is different from our media
100
     // XXX If muted state of track when it was added is different from our media
104
     // fired before track gets to state.
104
     // fired before track gets to state.
105
     if (track.muted !== muted) {
105
     if (track.muted !== muted) {
106
         track.muted = muted;
106
         track.muted = muted;
107
-        setTrackMuted(track.jitsiTrack, muted, store);
107
+        dispatch(setTrackMuted(track.jitsiTrack, muted));
108
     }
108
     }
109
 }
109
 }

+ 33
- 0
react/features/base/tracks/actions.js 查看文件

344
     };
344
     };
345
 }
345
 }
346
 
346
 
347
+/**
348
+ * Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of
349
+ * the specified <tt>track</tt> is already in accord with the specified
350
+ * <tt>muted</tt> value, then does nothing. In case the actual muting/unmuting
351
+ * fails, a rollback action will be dispatched to undo the muting/unmuting.
352
+ *
353
+ * @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
354
+ * unmute.
355
+ * @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
356
+ * <tt>true</tt>; otherwise, <tt>false</tt>.
357
+ * @returns {Function}
358
+ */
359
+export function setTrackMuted(track, muted) {
360
+    return dispatch => {
361
+        if (track.isMuted() === muted) {
362
+            return Promise.resolve();
363
+        }
364
+
365
+        const f = muted ? 'mute' : 'unmute';
366
+
367
+        return track[f]().catch(error => {
368
+            console.error(`set track ${f} failed`, error);
369
+
370
+            const setMuted
371
+                = track.mediaType === MEDIA_TYPE.AUDIO
372
+                    ? setAudioMuted
373
+                    : setVideoMuted;
374
+
375
+            dispatch(setMuted(!muted));
376
+        });
377
+    };
378
+}
379
+
347
 /**
380
 /**
348
  * Returns true if the provided JitsiTrack should be rendered as a mirror.
381
  * Returns true if the provided JitsiTrack should be rendered as a mirror.
349
  *
382
  *

+ 1
- 37
react/features/base/tracks/functions.js 查看文件

1
 /* global APP */
1
 /* global APP */
2
 
2
 
3
 import JitsiMeetJS, { JitsiTrackEvents } from '../lib-jitsi-meet';
3
 import JitsiMeetJS, { JitsiTrackEvents } from '../lib-jitsi-meet';
4
-import { MEDIA_TYPE, setAudioMuted, setVideoMuted } from '../media';
4
+import { MEDIA_TYPE } from '../media';
5
 
5
 
6
 const logger = require('jitsi-meet-logger').getLogger(__filename);
6
 const logger = require('jitsi-meet-logger').getLogger(__filename);
7
 
7
 
155
 export function getTracksByMediaType(tracks, mediaType) {
155
 export function getTracksByMediaType(tracks, mediaType) {
156
     return tracks.filter(t => t.mediaType === mediaType);
156
     return tracks.filter(t => t.mediaType === mediaType);
157
 }
157
 }
158
-
159
-/**
160
- * Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of
161
- * the specified <tt>track</tt> is already in accord with the specified
162
- * <tt>muted</tt> value, then does nothing. In case mute/unmute operation fails
163
- * (JitsiLocalTrack Promise is rejected) a rollback action will be dispatched on
164
- * the given store. For example if the mute operation fails then the 'unmute'
165
- * action will be dispatched to rollback to the previous state in base/media.
166
- *
167
- * @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
168
- * unmute.
169
- * @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
170
- * <tt>true</tt>; otherwise, <tt>false</tt>.
171
- * @param {Store} store - The redux store in the context of which the function
172
- * is to execute and which will be used to dispatch the rollback action in case
173
- * mute/unmute fails.
174
-* @returns {Promise}
175
- */
176
-export function setTrackMuted(track, muted, { dispatch }) {
177
-    if (track.isMuted() === muted) {
178
-        return Promise.resolve();
179
-    }
180
-
181
-    const f = muted ? 'mute' : 'unmute';
182
-
183
-    return track[f]().catch(error => {
184
-        console.error(`set track ${f} failed`, error);
185
-
186
-        const setMuted
187
-            = track.mediaType === MEDIA_TYPE.AUDIO
188
-                ? setAudioMuted
189
-                : setVideoMuted;
190
-
191
-        dispatch(setMuted(!muted));
192
-    });
193
-}

+ 12
- 13
react/features/base/tracks/middleware.js 查看文件

13
 } from '../media';
13
 } from '../media';
14
 import { MiddlewareRegistry } from '../redux';
14
 import { MiddlewareRegistry } from '../redux';
15
 
15
 
16
+import { setTrackMuted } from './actions';
16
 import { TRACK_ADDED, TRACK_REMOVED, TRACK_UPDATED } from './actionTypes';
17
 import { TRACK_ADDED, TRACK_REMOVED, TRACK_UPDATED } from './actionTypes';
17
-import { getLocalTrack, setTrackMuted } from './functions';
18
+import { getLocalTrack } from './functions';
18
 
19
 
19
 declare var APP: Object;
20
 declare var APP: Object;
20
 
21
 
23
  * respectively, creates/destroys local media tracks. Also listens to
24
  * respectively, creates/destroys local media tracks. Also listens to
24
  * media-related actions and performs corresponding operations with tracks.
25
  * media-related actions and performs corresponding operations with tracks.
25
  *
26
  *
26
- * @param {Store} store - Redux store.
27
+ * @param {Store} store - The redux store.
27
  * @returns {Function}
28
  * @returns {Function}
28
  */
29
  */
29
 MiddlewareRegistry.register(store => next => action => {
30
 MiddlewareRegistry.register(store => next => action => {
132
 
133
 
133
 /**
134
 /**
134
  * Gets the local track associated with a specific <tt>MEDIA_TYPE</tt> in a
135
  * Gets the local track associated with a specific <tt>MEDIA_TYPE</tt> in a
135
- * specific Redux store.
136
+ * specific redux store.
136
  *
137
  *
137
- * @param {Store} store - The Redux store from which the local track associated
138
+ * @param {Store} store - The redux store from which the local track associated
138
  * with the specified <tt>mediaType</tt> is to be retrieved.
139
  * with the specified <tt>mediaType</tt> is to be retrieved.
139
  * @param {MEDIA_TYPE} mediaType - The <tt>MEDIA_TYPE</tt> of the local track to
140
  * @param {MEDIA_TYPE} mediaType - The <tt>MEDIA_TYPE</tt> of the local track to
140
  * be retrieved from the specified <tt>store</tt>.
141
  * be retrieved from the specified <tt>store</tt>.
149
 /**
150
 /**
150
  * Mutes or unmutes a local track with a specific media type.
151
  * Mutes or unmutes a local track with a specific media type.
151
  *
152
  *
152
- * @param {Store} store - The Redux store in which the specified action is
153
+ * @param {Store} store - The redux store in which the specified action is
153
  * dispatched.
154
  * dispatched.
154
- * @param {Action} action - The Redux action dispatched in the specified store.
155
+ * @param {Action} action - The redux action dispatched in the specified store.
155
  * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
156
  * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
156
  * which is being muted or unmuted.
157
  * which is being muted or unmuted.
157
  * @private
158
  * @private
158
  * @returns {void}
159
  * @returns {void}
159
  */
160
  */
160
-function _setMuted(store, action, mediaType: MEDIA_TYPE) {
161
+function _setMuted(store, { muted }, mediaType: MEDIA_TYPE) {
161
     const localTrack = _getLocalTrack(store, mediaType);
162
     const localTrack = _getLocalTrack(store, mediaType);
162
 
163
 
163
-    if (localTrack) {
164
-        setTrackMuted(localTrack.jitsiTrack, action.muted, store);
165
-    }
164
+    localTrack && store.dispatch(setTrackMuted(localTrack.jitsiTrack, muted));
166
 }
165
 }
167
 
166
 
168
 /**
167
 /**
170
  * muted states of the local tracks of features/base/tracks with the muted
169
  * muted states of the local tracks of features/base/tracks with the muted
171
  * states of features/base/media.
170
  * states of features/base/media.
172
  *
171
  *
173
- * @param {Store} store - The Redux store in which the specified <tt>action</tt>
172
+ * @param {Store} store - The redux store in which the specified <tt>action</tt>
174
  * is being dispatched.
173
  * is being dispatched.
175
- * @param {Dispatch} next - The Redux dispatch function to dispatch the
174
+ * @param {Dispatch} next - The redux dispatch function to dispatch the
176
  * specified <tt>action</tt> to the specified <tt>store</tt>.
175
  * specified <tt>action</tt> to the specified <tt>store</tt>.
177
- * @param {Action} action - The Redux action <tt>TRACK_UPDATED</tt> which is
176
+ * @param {Action} action - The redux action <tt>TRACK_UPDATED</tt> which is
178
  * being dispatched in the specified <tt>store</tt>.
177
  * being dispatched in the specified <tt>store</tt>.
179
  * @private
178
  * @private
180
  * @returns {Object} The new state that is the result of the reduction of the
179
  * @returns {Object} The new state that is the result of the reduction of the

Loading…
取消
儲存