jiti-meet/react/features/base/profile/middleware.js

65 lines
1.7 KiB
JavaScript
Raw Normal View History

// @flow
2018-02-02 14:50:16 +00:00
import { setAudioOnly } from '../conference';
import { getLocalParticipant, participantUpdated } from '../participants';
import { getProfile } from '../profile';
import { MiddlewareRegistry, toState } from '../redux';
import { PROFILE_UPDATED } from './actionTypes';
/**
* A middleWare to update the local participant when the profile is updated.
*
* @param {Store} store - The redux store.
* @returns {Function}
*/
MiddlewareRegistry.register(store => next => action => {
const result = next(action);
switch (action.type) {
case PROFILE_UPDATED:
_updateLocalParticipant(store);
2018-02-02 14:50:16 +00:00
_maybeUpdateStartAudioOnly(store, action);
}
return result;
});
2018-02-02 14:50:16 +00:00
/**
* Updates startAudioOnly flag if it's updated in the profile.
*
* @private
* @param {Store} store - The redux store.
* @param {Object} action - The redux action.
* @returns {void}
*/
function _maybeUpdateStartAudioOnly(store, action) {
const { profile } = action;
if (typeof profile.startAudioOnly === 'boolean') {
store.dispatch(setAudioOnly(profile.startAudioOnly));
}
}
/**
* Updates the local participant according to profile changes.
*
* @param {Store} store - The redux store.
* @returns {void}
*/
function _updateLocalParticipant(store) {
const state = toState(store);
const localParticipant = getLocalParticipant(state);
const profile = getProfile(state);
store.dispatch(participantUpdated({
// Identify that the participant to update i.e. the local participant:
id: localParticipant && localParticipant.id,
local: true,
// Specify the updates to be applied to the identified participant:
email: profile.email,
name: profile.displayName
}));
}