Skip to content

Commit 63ee428

Browse files
committed
fix(devices) simplify configureInitialDevices
Its does not need to wait on getAvailableDevices() since it's always called before we enter here. What's more, that was delaying the execution enough to miss the device update that triggers processing the device requests queue.
1 parent b873c02 commit 63ee428

File tree

2 files changed

+56
-70
lines changed

2 files changed

+56
-70
lines changed

react/features/base/devices/actions.web.ts

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -72,71 +72,62 @@ export function addPendingDeviceRequest(request: Object) {
7272
*/
7373
export function configureInitialDevices() {
7474
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
75-
const deviceLabels = getDevicesFromURL(getState());
76-
let updateSettingsPromise;
75+
const state = getState();
76+
const deviceLabels = getDevicesFromURL(state);
7777

7878
logger.debug(`(TIME) configureInitialDevices: deviceLabels=${
7979
Boolean(deviceLabels)}, performance.now=${window.performance.now()}`);
8080

8181
if (deviceLabels) {
82-
updateSettingsPromise = dispatch(getAvailableDevices()).then(() => {
83-
const state = getState();
84-
85-
if (!areDeviceLabelsInitialized(state)) {
86-
// The labels are not available if the A/V permissions are
87-
// not yet granted.
88-
89-
Object.keys(deviceLabels).forEach(key => {
90-
dispatch(addPendingDeviceRequest({
91-
type: 'devices',
92-
name: 'setDevice',
93-
device: {
94-
kind: key.toLowerCase(),
95-
label: deviceLabels[key as keyof typeof deviceLabels]
96-
},
97-
// eslint-disable-next-line @typescript-eslint/no-empty-function
98-
responseCallback() {}
99-
}));
100-
});
101-
102-
return;
103-
}
104-
105-
const newSettings: any = {};
82+
if (!areDeviceLabelsInitialized(state)) {
83+
// The labels are not available if the A/V permissions are
84+
// not yet granted.
10685

10786
Object.keys(deviceLabels).forEach(key => {
108-
const label = deviceLabels[key as keyof typeof deviceLabels];
87+
dispatch(addPendingDeviceRequest({
88+
type: 'devices',
89+
name: 'setDevice',
90+
device: {
91+
kind: key.toLowerCase(),
92+
label: deviceLabels[key as keyof typeof deviceLabels]
93+
},
94+
// eslint-disable-next-line @typescript-eslint/no-empty-function
95+
responseCallback() {}
96+
}));
97+
});
10998

110-
// @ts-ignore
111-
const deviceId = getDeviceIdByLabel(state, label, key);
99+
return;
100+
}
112101

113-
if (deviceId) {
114-
const settingsTranslationMap = DEVICE_TYPE_TO_SETTINGS_KEYS[
115-
key as keyof typeof DEVICE_TYPE_TO_SETTINGS_KEYS];
102+
const newSettings: any = {};
116103

117-
newSettings[settingsTranslationMap.currentDeviceId] = deviceId;
118-
newSettings[settingsTranslationMap.userSelectedDeviceId] = deviceId;
119-
newSettings[settingsTranslationMap.userSelectedDeviceLabel] = label;
120-
}
121-
});
104+
Object.keys(deviceLabels).forEach(key => {
105+
const label = deviceLabels[key as keyof typeof deviceLabels];
106+
107+
// @ts-ignore
108+
const deviceId = getDeviceIdByLabel(state, label, key);
122109

123-
dispatch(updateSettings(newSettings));
110+
if (deviceId) {
111+
const settingsTranslationMap = DEVICE_TYPE_TO_SETTINGS_KEYS[
112+
key as keyof typeof DEVICE_TYPE_TO_SETTINGS_KEYS];
113+
114+
newSettings[settingsTranslationMap.currentDeviceId] = deviceId;
115+
newSettings[settingsTranslationMap.userSelectedDeviceId] = deviceId;
116+
newSettings[settingsTranslationMap.userSelectedDeviceLabel] = label;
117+
}
124118
});
125-
} else {
126-
updateSettingsPromise = Promise.resolve();
119+
120+
dispatch(updateSettings(newSettings));
127121
}
128122

129-
return updateSettingsPromise
130-
.then(() => {
131-
const userSelectedAudioOutputDeviceId = getUserSelectedOutputDeviceId(getState());
123+
const userSelectedAudioOutputDeviceId = getUserSelectedOutputDeviceId(getState());
132124

133-
logger.debug(`(TIME) configureInitialDevices -> setAudioOutputDeviceId: performance.now=${
134-
window.performance.now()}`);
125+
logger.debug(`(TIME) configureInitialDevices -> setAudioOutputDeviceId: performance.now=${
126+
window.performance.now()}`);
135127

136-
return setAudioOutputDeviceId(userSelectedAudioOutputDeviceId, dispatch)
137-
.catch(ex => logger.warn(`Failed to set audio output device.
138-
Default audio output device will be used instead ${ex}`));
139-
});
128+
return setAudioOutputDeviceId(userSelectedAudioOutputDeviceId, dispatch)
129+
.catch(ex => logger.warn(`Failed to set audio output device.
130+
Default audio output device will be used instead ${ex}`));
140131
};
141132
}
142133

react/features/base/devices/middleware.web.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import { AnyAction } from 'redux';
2-
31
import { IStore } from '../../app/types';
4-
import { processExternalDeviceRequest } from '../../device-selection/functions';
2+
import { processExternalDeviceRequest } from '../../device-selection/functions.web';
53
import { showNotification, showWarningNotification } from '../../notifications/actions';
64
import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications/constants';
7-
import { replaceAudioTrackById, replaceVideoTrackById, setDeviceStatusWarning } from '../../prejoin/actions';
8-
import { isPrejoinPageVisible } from '../../prejoin/functions';
5+
import { replaceAudioTrackById, replaceVideoTrackById, setDeviceStatusWarning } from '../../prejoin/actions.web';
6+
import { isPrejoinPageVisible } from '../../prejoin/functions.web';
97
import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app/actionTypes';
108
import { isMobileBrowser } from '../environment/utils';
119
import JitsiMeetJS, { JitsiMediaDevicesEvents, JitsiTrackErrors } from '../lib-jitsi-meet';
1210
import { MEDIA_TYPE } from '../media/constants';
1311
import MiddlewareRegistry from '../redux/MiddlewareRegistry';
1412
import { updateSettings } from '../settings/actions';
15-
import { getLocalTrack } from '../tracks/functions';
13+
import { getLocalTrack } from '../tracks/functions.web';
1614

1715
import {
1816
CHECK_AND_NOTIFY_FOR_NEW_DEVICE,
@@ -27,13 +25,13 @@ import {
2725
removePendingDeviceRequests,
2826
setAudioInputDevice,
2927
setVideoInputDevice
30-
} from './actions';
28+
} from './actions.web';
3129
import {
3230
areDeviceLabelsInitialized,
3331
formatDeviceLabel,
3432
logDevices,
3533
setAudioOutputDeviceId
36-
} from './functions';
34+
} from './functions.web';
3735
import logger from './logger';
3836

3937
const JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP = {
@@ -176,12 +174,16 @@ MiddlewareRegistry.register(store => next => action => {
176174
}
177175
break;
178176
}
179-
case UPDATE_DEVICE_LIST:
177+
case UPDATE_DEVICE_LIST: {
178+
const result = next(action);
179+
180180
logDevices(action.devices, 'Device list updated');
181181
if (areDeviceLabelsInitialized(store.getState())) {
182-
return _processPendingRequests(store, next, action);
182+
_processPendingRequests(store);
183183
}
184-
break;
184+
185+
return result;
186+
}
185187
case CHECK_AND_NOTIFY_FOR_NEW_DEVICE:
186188
_checkAndNotifyForNewDevice(store, action.newDevices, action.oldDevices);
187189
break;
@@ -196,20 +198,15 @@ MiddlewareRegistry.register(store => next => action => {
196198
*
197199
* @param {Store} store - The redux store in which the specified {@code action}
198200
* is being dispatched.
199-
* @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
200-
* specified {@code action} to the specified {@code store}.
201-
* @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
202-
* being dispatched in the specified {@code store}.
203201
* @private
204-
* @returns {Object} The value returned by {@code next(action)}.
202+
* @returns {void}
205203
*/
206-
function _processPendingRequests({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
207-
const result = next(action);
204+
function _processPendingRequests({ dispatch, getState }: IStore) {
208205
const state = getState();
209206
const { pendingRequests } = state['features/base/devices'];
210207

211208
if (!pendingRequests || pendingRequests.length === 0) {
212-
return result;
209+
return;
213210
}
214211

215212
pendingRequests.forEach((request: any) => {
@@ -220,8 +217,6 @@ function _processPendingRequests({ dispatch, getState }: IStore, next: Function,
220217
request.responseCallback);
221218
});
222219
dispatch(removePendingDeviceRequests());
223-
224-
return result;
225220
}
226221

227222
/**

0 commit comments

Comments
 (0)