Convert a function to an asynchronous redux action creator

This commit is contained in:
Lyubo Marinov 2017-07-31 11:27:34 -05:00
parent 40c9f583fa
commit dcc6ce025f
4 changed files with 49 additions and 53 deletions

View File

@ -93,8 +93,8 @@ function _setRoom({ dispatch, getState }, next, action) {
* @private
* @returns {void}
*/
function _syncTrackMutedState(store, track) {
const state = store.getState()['features/base/media'];
function _syncTrackMutedState({ dispatch, getState }, track) {
const state = getState()['features/base/media'];
const muted = state[track.mediaType].muted;
// XXX If muted state of track when it was added is different from our media
@ -104,6 +104,6 @@ function _syncTrackMutedState(store, track) {
// fired before track gets to state.
if (track.muted !== muted) {
track.muted = muted;
setTrackMuted(track.jitsiTrack, muted, store);
dispatch(setTrackMuted(track.jitsiTrack, muted));
}
}

View File

@ -344,6 +344,39 @@ function _getLocalTracksToChange(currentTracks, newTracks) {
};
}
/**
* Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of
* the specified <tt>track</tt> is already in accord with the specified
* <tt>muted</tt> value, then does nothing. In case the actual muting/unmuting
* fails, a rollback action will be dispatched to undo the muting/unmuting.
*
* @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
* unmute.
* @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
* <tt>true</tt>; otherwise, <tt>false</tt>.
* @returns {Function}
*/
export function setTrackMuted(track, muted) {
return dispatch => {
if (track.isMuted() === muted) {
return Promise.resolve();
}
const f = muted ? 'mute' : 'unmute';
return track[f]().catch(error => {
console.error(`set track ${f} failed`, error);
const setMuted
= track.mediaType === MEDIA_TYPE.AUDIO
? setAudioMuted
: setVideoMuted;
dispatch(setMuted(!muted));
});
};
}
/**
* Returns true if the provided JitsiTrack should be rendered as a mirror.
*

View File

@ -1,7 +1,7 @@
/* global APP */
import JitsiMeetJS, { JitsiTrackEvents } from '../lib-jitsi-meet';
import { MEDIA_TYPE, setAudioMuted, setVideoMuted } from '../media';
import { MEDIA_TYPE } from '../media';
const logger = require('jitsi-meet-logger').getLogger(__filename);
@ -155,39 +155,3 @@ export function getTrackByJitsiTrack(tracks, jitsiTrack) {
export function getTracksByMediaType(tracks, mediaType) {
return tracks.filter(t => t.mediaType === mediaType);
}
/**
* Mutes or unmutes a specific <tt>JitsiLocalTrack</tt>. If the muted state of
* the specified <tt>track</tt> is already in accord with the specified
* <tt>muted</tt> value, then does nothing. In case mute/unmute operation fails
* (JitsiLocalTrack Promise is rejected) a rollback action will be dispatched on
* the given store. For example if the mute operation fails then the 'unmute'
* action will be dispatched to rollback to the previous state in base/media.
*
* @param {JitsiLocalTrack} track - The <tt>JitsiLocalTrack</tt> to mute or
* unmute.
* @param {boolean} muted - If the specified <tt>track</tt> is to be muted, then
* <tt>true</tt>; otherwise, <tt>false</tt>.
* @param {Store} store - The redux store in the context of which the function
* is to execute and which will be used to dispatch the rollback action in case
* mute/unmute fails.
* @returns {Promise}
*/
export function setTrackMuted(track, muted, { dispatch }) {
if (track.isMuted() === muted) {
return Promise.resolve();
}
const f = muted ? 'mute' : 'unmute';
return track[f]().catch(error => {
console.error(`set track ${f} failed`, error);
const setMuted
= track.mediaType === MEDIA_TYPE.AUDIO
? setAudioMuted
: setVideoMuted;
dispatch(setMuted(!muted));
});
}

View File

@ -13,8 +13,9 @@ import {
} from '../media';
import { MiddlewareRegistry } from '../redux';
import { setTrackMuted } from './actions';
import { TRACK_ADDED, TRACK_REMOVED, TRACK_UPDATED } from './actionTypes';
import { getLocalTrack, setTrackMuted } from './functions';
import { getLocalTrack } from './functions';
declare var APP: Object;
@ -23,7 +24,7 @@ declare var APP: Object;
* respectively, creates/destroys local media tracks. Also listens to
* media-related actions and performs corresponding operations with tracks.
*
* @param {Store} store - Redux store.
* @param {Store} store - The redux store.
* @returns {Function}
*/
MiddlewareRegistry.register(store => next => action => {
@ -132,9 +133,9 @@ MiddlewareRegistry.register(store => next => action => {
/**
* Gets the local track associated with a specific <tt>MEDIA_TYPE</tt> in a
* specific Redux store.
* specific redux store.
*
* @param {Store} store - The Redux store from which the local track associated
* @param {Store} store - The redux store from which the local track associated
* with the specified <tt>mediaType</tt> is to be retrieved.
* @param {MEDIA_TYPE} mediaType - The <tt>MEDIA_TYPE</tt> of the local track to
* be retrieved from the specified <tt>store</tt>.
@ -149,20 +150,18 @@ function _getLocalTrack(store, mediaType: MEDIA_TYPE) {
/**
* Mutes or unmutes a local track with a specific media type.
*
* @param {Store} store - The Redux store in which the specified action is
* @param {Store} store - The redux store in which the specified action is
* dispatched.
* @param {Action} action - The Redux action dispatched in the specified store.
* @param {Action} action - The redux action dispatched in the specified store.
* @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
* which is being muted or unmuted.
* @private
* @returns {void}
*/
function _setMuted(store, action, mediaType: MEDIA_TYPE) {
function _setMuted(store, { muted }, mediaType: MEDIA_TYPE) {
const localTrack = _getLocalTrack(store, mediaType);
if (localTrack) {
setTrackMuted(localTrack.jitsiTrack, action.muted, store);
}
localTrack && store.dispatch(setTrackMuted(localTrack.jitsiTrack, muted));
}
/**
@ -170,11 +169,11 @@ function _setMuted(store, action, mediaType: MEDIA_TYPE) {
* muted states of the local tracks of features/base/tracks with the muted
* states of features/base/media.
*
* @param {Store} store - The Redux store in which the specified <tt>action</tt>
* @param {Store} store - The redux store in which the specified <tt>action</tt>
* is being dispatched.
* @param {Dispatch} next - The Redux dispatch function to dispatch the
* @param {Dispatch} next - The redux dispatch function to dispatch the
* specified <tt>action</tt> to the specified <tt>store</tt>.
* @param {Action} action - The Redux action <tt>TRACK_UPDATED</tt> which is
* @param {Action} action - The redux action <tt>TRACK_UPDATED</tt> which is
* being dispatched in the specified <tt>store</tt>.
* @private
* @returns {Object} The new state that is the result of the reduction of the