2017-04-23 20:14:02 +00:00
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
import { SET_CONFIG } from '../config';
|
2017-04-23 20:17:31 +00:00
|
|
|
import { setLoggingConfig } from '../logging';
|
2016-10-05 14:36:59 +00:00
|
|
|
import { PARTICIPANT_LEFT } from '../participants';
|
|
|
|
import { MiddlewareRegistry } from '../redux';
|
|
|
|
|
2017-02-28 03:22:32 +00:00
|
|
|
import { disposeLib, initLib, setWebRTCReady } from './actions';
|
2017-04-23 20:14:02 +00:00
|
|
|
import { LIB_DID_INIT, LIB_INIT_ERROR } from './actionTypes';
|
2017-02-28 03:22:32 +00:00
|
|
|
import { WEBRTC_NOT_READY, WEBRTC_NOT_SUPPORTED } from './constants';
|
2016-10-05 14:36:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware that captures PARTICIPANT_LEFT action for a local participant
|
|
|
|
* (which signalizes that we finally left the app) and disposes lib-jitsi-meet.
|
|
|
|
* Also captures SET_CONFIG action and disposes previous instance (if any) of
|
|
|
|
* lib-jitsi-meet, and initializes a new one with new config.
|
|
|
|
*
|
|
|
|
* @param {Store} store - Redux store.
|
2017-02-27 22:45:53 +00:00
|
|
|
* @private
|
2017-04-22 22:57:08 +00:00
|
|
|
* @returns {Function}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
2017-02-28 03:22:32 +00:00
|
|
|
case LIB_DID_INIT:
|
|
|
|
store.dispatch(setWebRTCReady(true));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LIB_INIT_ERROR:
|
|
|
|
return _libInitError(store, next, action);
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
case PARTICIPANT_LEFT:
|
2016-10-25 16:43:15 +00:00
|
|
|
action.participant.local && store.dispatch(disposeLib());
|
2016-10-05 14:36:59 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-16 19:51:01 +00:00
|
|
|
case SET_CONFIG:
|
|
|
|
return _setConfig(store, next, action);
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
2017-02-16 19:51:01 +00:00
|
|
|
|
2017-02-28 03:22:32 +00:00
|
|
|
/**
|
|
|
|
* Notifies the feature base/lib-jitsi-meet that the action LIB_INIT_ERROR is
|
|
|
|
* being dispatched within a specific Redux store.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The Redux store in which the specified action is being
|
|
|
|
* dispatched.
|
|
|
|
* @param {Dispatch} next - The Redux dispatch function to dispatch the
|
|
|
|
* specified action to the specified store.
|
|
|
|
* @param {Action} action - The Redux action LIB_INIT_ERROR which is being
|
|
|
|
* dispatched in the specified store.
|
2017-04-22 22:57:08 +00:00
|
|
|
* @private
|
2017-02-28 03:22:32 +00:00
|
|
|
* @returns {Object} The new state that is the result of the reduction of the
|
|
|
|
* specified action.
|
|
|
|
*/
|
|
|
|
function _libInitError(store, next, action) {
|
|
|
|
const nextState = next(action);
|
|
|
|
|
2017-04-22 22:57:08 +00:00
|
|
|
const { error } = action;
|
2017-02-28 03:22:32 +00:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
let webRTCReady;
|
|
|
|
|
|
|
|
switch (error.name) {
|
|
|
|
case WEBRTC_NOT_READY:
|
|
|
|
webRTCReady = error.webRTCReadyPromise;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WEBRTC_NOT_SUPPORTED:
|
|
|
|
webRTCReady = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
typeof webRTCReady === 'undefined'
|
|
|
|
|| store.dispatch(setWebRTCReady(webRTCReady));
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextState;
|
|
|
|
}
|
|
|
|
|
2017-02-16 19:51:01 +00:00
|
|
|
/**
|
|
|
|
* Notifies the feature base/lib-jitsi-meet that the action SET_CONFIG is being
|
|
|
|
* dispatched within a specific Redux store.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The Redux store in which the specified action is being
|
|
|
|
* dispatched.
|
|
|
|
* @param {Dispatch} next - The Redux dispatch function to dispatch the
|
|
|
|
* specified action to the specified store.
|
|
|
|
* @param {Action} action - The Redux action SET_CONFIG which is being
|
|
|
|
* dispatched in the specified store.
|
2017-04-22 22:57:08 +00:00
|
|
|
* @private
|
2017-02-16 19:51:01 +00:00
|
|
|
* @returns {Object} The new state that is the result of the reduction of the
|
|
|
|
* specified action.
|
|
|
|
*/
|
2017-04-22 22:57:08 +00:00
|
|
|
function _setConfig({ dispatch, getState }, next, action) {
|
2017-02-16 19:51:01 +00:00
|
|
|
const { initialized } = getState()['features/base/lib-jitsi-meet'];
|
|
|
|
|
|
|
|
// XXX Since the config is changing, the library lib-jitsi-meet must be
|
2017-04-22 22:57:08 +00:00
|
|
|
// initialized again with the new config. Consequently, it may need to be
|
2017-02-16 19:51:01 +00:00
|
|
|
// disposed of first.
|
|
|
|
// TODO Currently, disposeLib actually does not dispose of lib-jitsi-meet
|
|
|
|
// because lib-jitsi-meet does not implement such functionality.
|
2017-05-17 21:44:28 +00:00
|
|
|
if (initialized) {
|
|
|
|
dispatch(disposeLib());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let the new config into the Redux store (because initLib will read it
|
|
|
|
// from there).
|
|
|
|
const result = next(action);
|
|
|
|
|
|
|
|
// FIXME Obviously, the following is bad design. However, I'm currently
|
|
|
|
// introducing the features base/config and base/logging and I'm trying to
|
|
|
|
// minimize the scope of the changes while I'm attempting to preserve
|
|
|
|
// compatibility with the existing partially React-ified Web source code and
|
|
|
|
// what was already executing on React Native. Additionally, I do not care
|
|
|
|
// to load logging_config.js on React Native.
|
|
|
|
dispatch(setLoggingConfig(window.loggingConfig));
|
|
|
|
|
|
|
|
dispatch(initLib());
|
|
|
|
|
|
|
|
return result;
|
2017-02-16 19:51:01 +00:00
|
|
|
}
|