Skip to content

Commit

Permalink
fix: Make all middleware functions sync.
Browse files Browse the repository at this point in the history
Some middleware functions are declared as async. This wraps next(action) in Promise which will delay the execution of actions and also dispatch will return the its result always as a Promise.
  • Loading branch information
hristoterezov committed Jul 25, 2024
1 parent b242900 commit 2514617
Show file tree
Hide file tree
Showing 35 changed files with 223 additions and 184 deletions.
3 changes: 2 additions & 1 deletion conference.js
Original file line number Diff line number Diff line change
Expand Up @@ -1165,11 +1165,12 @@ export default {
APP.store.dispatch(gumPending(mutedTrackTypes, IGUMPendingState.NONE));
}

this._setLocalAudioVideoStreams(tracks);
this._room = room; // FIXME do not use this

APP.store.dispatch(_conferenceWillJoin(room));

this._setLocalAudioVideoStreams(tracks);

sendLocalParticipant(APP.store, room);

this._setupListeners();
Expand Down
2 changes: 1 addition & 1 deletion react/features/base/app/middleware.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let pressureObserver: typeof window.PressureObserver;
* @param {Store} store - The redux store.
* @returns {Function}
*/
MiddlewareRegistry.register(() => (next: Function) => async (action: AnyAction) => {
MiddlewareRegistry.register(() => (next: Function) => (action: AnyAction) => {

switch (action.type) {
case APP_WILL_MOUNT: {
Expand Down
2 changes: 1 addition & 1 deletion react/features/base/audio-only/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ export function toggleAudioOnly() {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
const { enabled } = getState()['features/base/audio-only'];

return dispatch(setAudioOnly(!enabled));
dispatch(setAudioOnly(!enabled));
};
}
24 changes: 15 additions & 9 deletions react/features/base/conference/actions.any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ export function setPassword(
password?: string) {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
if (!conference) {
return;
return Promise.reject();
}
switch (method) {
case conference.join: {
Expand Down Expand Up @@ -981,7 +981,7 @@ export function setStartMutedPolicy(
video: startVideoMuted
});

return dispatch(
dispatch(
onStartMutedPolicyChanged(startAudioMuted, startVideoMuted));
};
}
Expand Down Expand Up @@ -1058,14 +1058,20 @@ export function redirect(vnode: string, focusJid: string, username: string) {
return;
}

dispatch(overwriteConfig(newConfig)) // @ts-ignore
.then(() => dispatch(disconnect(true)))
.then(() => dispatch(setIAmVisitor(Boolean(vnode))))
dispatch(overwriteConfig(newConfig));

dispatch(disconnect(true))
.then(() => {
dispatch(setIAmVisitor(Boolean(vnode)));

// we do not clear local tracks on error, so we need to manually clear them
return dispatch(destroyLocalTracks());
})
.then(() => {
dispatch(conferenceWillInit());

// we do not clear local tracks on error, so we need to manually clear them
.then(() => dispatch(destroyLocalTracks()))
.then(() => dispatch(conferenceWillInit()))
.then(() => dispatch(connect()))
return dispatch(connect());
})
.then(() => {
const media: Array<MediaType> = [];

Expand Down
7 changes: 4 additions & 3 deletions react/features/base/conference/middleware.any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,10 @@ function _conferenceFailed({ dispatch, getState }: IStore, next: Function, actio
const newConfig = restoreConferenceOptions(getState);

if (newConfig) {
dispatch(overwriteConfig(newConfig)) // @ts-ignore
.then(() => dispatch(conferenceWillLeave(conference)))
.then(() => conference.leave())
dispatch(overwriteConfig(newConfig));
dispatch(conferenceWillLeave(conference));

conference.leave()
.then(() => dispatch(disconnect()))
.then(() => dispatch(connect()))
.then(() => {
Expand Down
7 changes: 5 additions & 2 deletions react/features/base/connection/actions.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ export function connect(id?: string, password?: string) {
return getJaasJWT(state);
}
})
.then(j => j && dispatch(setJWT(j)))
.then(() => dispatch(_connectInternal(id, password)));
.then(j => {
j && dispatch(setJWT(j));

return dispatch(_connectInternal(id, password));
});
}

// used by jibri
Expand Down
9 changes: 4 additions & 5 deletions react/features/base/i18n/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import logger from './logger';
* @param {Store} store - The redux store.
* @returns {Function}
*/
MiddlewareRegistry.register(store => next => async action => {
MiddlewareRegistry.register(store => next => action => {
switch (action.type) {
case I18NEXT_INITIALIZED:
case LANGUAGE_CHANGED:
Expand All @@ -23,11 +23,10 @@ MiddlewareRegistry.register(store => next => async action => {
: store.getState()['features/dynamic-branding'];

if (language && labels && labels[language]) {
try {
await changeLanguageBundle(language, labels[language]);
} catch (err) {
changeLanguageBundle(language, labels[language])
.catch(err => {
logger.log('Error setting dynamic language bundle', err);
}
});
}
break;
}
Expand Down
4 changes: 2 additions & 2 deletions react/features/base/media/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export function setScreenshareMuted(
// eslint-disable-next-line no-bitwise
const newValue = muted ? oldValue | authority : oldValue & ~authority;

return dispatch({
dispatch({
type: SET_SCREENSHARE_MUTED,
authority,
ensureTrack,
Expand Down Expand Up @@ -180,7 +180,7 @@ export function setVideoMuted(
// eslint-disable-next-line no-bitwise
const newValue = muted ? oldValue | authority : oldValue & ~authority;

return dispatch({
dispatch({
type: SET_VIDEO_MUTED,
authority,
ensureTrack,
Expand Down
20 changes: 9 additions & 11 deletions react/features/base/tracks/actions.any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function addLocalTrack(newTrack: any) {
const isMuted = newTrack.isMuted();

logger.log(`Adding ${newTrack.getType()} track - ${isMuted ? 'muted' : 'unmuted'}`);
await dispatch(setMuted(isMuted));
dispatch(setMuted(isMuted));

return dispatch(_addTracks([ newTrack ]));
};
Expand Down Expand Up @@ -233,12 +233,11 @@ export function createLocalTracksA(options: ITrackOptions = {}) {
*/
export function destroyLocalTracks(track: any = null) {
if (track) {
return (dispatch: IStore['dispatch']) => {
dispatch(_disposeAndRemoveTracks([ track ]));
};
return (dispatch: IStore['dispatch']) => dispatch(_disposeAndRemoveTracks([ track ]));
}

return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) =>

// First wait until any getUserMedia in progress is settled and then get
// rid of all local tracks.
_cancelGUMProcesses(getState)
Expand All @@ -248,7 +247,6 @@ export function destroyLocalTracks(track: any = null) {
getState()['features/base/tracks']
.filter(t => t.local)
.map(t => t.jitsiTrack))));
};
}

/**
Expand All @@ -274,7 +272,7 @@ export function noDataFromSource(track: any) {
* @returns {Function}
*/
export function showNoDataFromSourceVideoError(jitsiTrack: any) {
return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
let notificationInfo;

const track = getTrackByJitsiTrack(getState()['features/base/tracks'], jitsiTrack);
Expand All @@ -286,7 +284,7 @@ export function showNoDataFromSourceVideoError(jitsiTrack: any) {
if (track.isReceivingData) {
notificationInfo = undefined;
} else {
const notificationAction = await dispatch(showErrorNotification({
const notificationAction = dispatch(showErrorNotification({
descriptionKey: 'dialog.cameraNotSendingData',
titleKey: 'dialog.cameraNotSendingDataTitle'
}, NOTIFICATION_TIMEOUT_TYPE.LONG));
Expand Down Expand Up @@ -359,7 +357,7 @@ function replaceStoredTracks(oldTrack: any, newTrack: any) {
sendAnalytics(createTrackMutedEvent(newTrack.getType(), 'track.replaced', isMuted));
logger.log(`Replace ${newTrack.getType()} track - ${isMuted ? 'muted' : 'unmuted'}`);

await dispatch(setMuted(isMuted));
dispatch(setMuted(isMuted));
await dispatch(_addTracks([ newTrack ]));
}
};
Expand All @@ -373,7 +371,7 @@ function replaceStoredTracks(oldTrack: any, newTrack: any) {
* @returns {Function}
*/
export function trackAdded(track: any) {
return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
track.on(
JitsiTrackEvents.TRACK_MUTE_CHANGED,
() => dispatch(trackMutedChanged(track)));
Expand All @@ -400,7 +398,7 @@ export function trackAdded(track: any) {
track.on(JitsiTrackEvents.NO_DATA_FROM_SOURCE, () => dispatch(noDataFromSource({ jitsiTrack: track })));
if (!isReceivingData) {
if (mediaType === MEDIA_TYPE.AUDIO) {
const notificationAction = await dispatch(showNotification({
const notificationAction = dispatch(showNotification({
descriptionKey: 'dialog.micNotSendingData',
titleKey: 'dialog.micNotSendingDataTitle'
}, NOTIFICATION_TIMEOUT_TYPE.LONG));
Expand Down
2 changes: 1 addition & 1 deletion react/features/base/tracks/middleware.any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function _getLocalTrack(
* @private
* @returns {void}
*/
async function _setMuted(store: IStore, { ensureTrack, muted }: {
function _setMuted(store: IStore, { ensureTrack, muted }: {
ensureTrack: boolean; muted: boolean; }, mediaType: MediaType) {
const { dispatch, getState } = store;
const localTrack = _getLocalTrack(store, mediaType, /* includePending */ true);
Expand Down
2 changes: 1 addition & 1 deletion react/features/jaas/middleware.any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getVpaasTenant, isVpaasMeeting } from './functions';
* @returns {Function}
*/

MiddlewareRegistry.register(store => next => async action => {
MiddlewareRegistry.register(store => next => action => {
switch (action.type) {
case CONFERENCE_JOINED: {
_maybeTrackVpaasConferenceJoin(store.getState());
Expand Down
2 changes: 1 addition & 1 deletion react/features/jaas/middleware.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import logger from './logger';
* @param {Store} store - The redux store.
* @returns {Function}
*/
MiddlewareRegistry.register(store => next => async action => {
MiddlewareRegistry.register(store => next => action => {
switch (action.type) {
case CONFERENCE_JOINED: {
const { conference } = action;
Expand Down
16 changes: 8 additions & 8 deletions react/features/lobby/actions.any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { IKnockingParticipant } from './types';
* @returns {Function}
*/
export function joinWithPassword(password: string) {
return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
const conference = getCurrentConference(getState);

dispatch(setPassword(conference, conference?.join, password));
Expand Down Expand Up @@ -79,7 +79,7 @@ export function participantIsKnockingOrUpdated(participant: IKnockingParticipant
* @returns {Function}
*/
export function answerKnockingParticipant(id: string, approved: boolean) {
return async (dispatch: IStore['dispatch']) => {
return (dispatch: IStore['dispatch']) => {
dispatch(setKnockingParticipantApproval(id, approved));
dispatch(hideNotification(LOBBY_NOTIFICATION_ID));
};
Expand Down Expand Up @@ -202,7 +202,7 @@ export function setPasswordJoinFailed(failed: boolean) {
* @returns {Function}
*/
export function startKnocking() {
return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
const state = getState();
const { membersOnly } = state['features/base/conference'];

Expand Down Expand Up @@ -287,7 +287,7 @@ export function hideLobbyScreen() {
* @returns {Promise<void>}
*/
export function handleLobbyChatInitialized(payload: { attendee: IParticipant; moderator: IParticipant; }) {
return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
const state = getState();
const conference = getCurrentConference(state);

Expand Down Expand Up @@ -323,7 +323,7 @@ export function handleLobbyChatInitialized(payload: { attendee: IParticipant; mo
* @returns {Promise<void>}
*/
export function onSendMessage(message: string) {
return async (dispatch: IStore['dispatch']) => {
return (dispatch: IStore['dispatch']) => {
dispatch(sendMessage(message));
};
}
Expand All @@ -349,7 +349,7 @@ export function sendLobbyChatMessage(message: Object) {
* @returns {Function}
*/
export function maybeSetLobbyChatMessageListener() {
return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
const state = getState();
const lobbyEnabled = getLobbyEnabled(state);

Expand All @@ -366,7 +366,7 @@ export function maybeSetLobbyChatMessageListener() {
* @returns {Function}
*/
export function updateLobbyParticipantOnLeave(participantId: string) {
return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
const state = getState();
const { knocking, knockingParticipants } = state['features/lobby'];
const { lobbyMessageRecipient } = state['features/chat'];
Expand Down Expand Up @@ -400,7 +400,7 @@ export function updateLobbyParticipantOnLeave(participantId: string) {
* @returns {Function}
*/
export function setLobbyMessageListener() {
return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
const state = getState();
const conference = getCurrentConference(state);
const { enableChat = true } = getLobbyConfig(state);
Expand Down
4 changes: 2 additions & 2 deletions react/features/no-audio-signal/middleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import DialInLink from './components/DialInLink';
import { NO_AUDIO_SIGNAL_SOUND_ID } from './constants';
import { NO_AUDIO_SIGNAL_SOUND_FILE } from './sounds';

MiddlewareRegistry.register(store => next => async action => {
MiddlewareRegistry.register(store => next => action => {
const result = next(action);
const { dispatch } = store;

Expand Down Expand Up @@ -107,7 +107,7 @@ async function _handleNoAudioSignalNotification({ dispatch, getState }: IStore,
} ];
}

const notification = await dispatch(showNotification({
const notification = dispatch(showNotification({
titleKey: 'toolbar.noAudioSignalTitle',
description: <DialInLink />,
descriptionKey,
Expand Down
4 changes: 2 additions & 2 deletions react/features/noise-detection/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ MiddlewareRegistry.register(store => next => action => {
}
});
conference.on(
JitsiConferenceEvents.NOISY_MIC, async () => {
const notification = await dispatch(showNotification({
JitsiConferenceEvents.NOISY_MIC, () => {
const notification = dispatch(showNotification({
titleKey: 'toolbar.noisyAudioInputTitle',
descriptionKey: 'toolbar.noisyAudioInputDesc'
}, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
Expand Down
4 changes: 2 additions & 2 deletions react/features/prejoin/actions.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export function dialOut(onSuccess: Function, onFail: Function) {
* @returns {Function}
*/
export function initPrejoin(tracks: Object[], errors: Object) {
return async function(dispatch: IStore['dispatch']) {
return function(dispatch: IStore['dispatch']) {
dispatch(setPrejoinDeviceErrors(errors));
dispatch(prejoinInitialized());

Expand All @@ -216,7 +216,7 @@ export function initPrejoin(tracks: Object[], errors: Object) {
*/
export function joinConference(options?: Object, ignoreJoiningInProgress = false,
jid?: string, password?: string) {
return async function(dispatch: IStore['dispatch'], getState: IStore['getState']) {
return function(dispatch: IStore['dispatch'], getState: IStore['getState']) {
if (!ignoreJoiningInProgress) {
const state = getState();
const { joiningInProgress } = state['features/prejoin'];
Expand Down
2 changes: 1 addition & 1 deletion react/features/prejoin/middleware.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { isPrejoinPageVisible } from './functions';
* @param {Store} store - The redux store.
* @returns {Function}
*/
MiddlewareRegistry.register(store => next => async action => {
MiddlewareRegistry.register(store => next => action => {
switch (action.type) {
case SET_AUDIO_MUTED: {
if (isPrejoinPageVisible(store.getState())) {
Expand Down
4 changes: 2 additions & 2 deletions react/features/recording/actions.any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function setLiveStreamKey(streamKey: string) {
* @returns {Function}
*/
export function showPendingRecordingNotification(streamType: string) {
return async (dispatch: IStore['dispatch']) => {
return (dispatch: IStore['dispatch']) => {
const isLiveStreaming
= streamType === JitsiMeetJS.constants.recording.mode.STREAM;
const dialogProps = isLiveStreaming ? {
Expand All @@ -148,7 +148,7 @@ export function showPendingRecordingNotification(streamType: string) {
descriptionKey: 'recording.pending',
titleKey: 'dialog.recording'
};
const notification = await dispatch(showNotification({
const notification = dispatch(showNotification({
...dialogProps
}, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));

Expand Down
Loading

0 comments on commit 2514617

Please sign in to comment.