2017-02-16 19:57:39 +00:00
|
|
|
import type { Dispatch } from 'redux';
|
|
|
|
|
2017-02-28 03:21:50 +00:00
|
|
|
import JitsiMeetJS from './_';
|
2016-10-05 14:36:59 +00:00
|
|
|
import {
|
2017-02-27 22:45:53 +00:00
|
|
|
LIB_DID_DISPOSE,
|
|
|
|
LIB_DID_INIT,
|
2016-10-05 14:36:59 +00:00
|
|
|
LIB_INIT_ERROR,
|
2017-02-27 22:45:53 +00:00
|
|
|
LIB_WILL_DISPOSE,
|
|
|
|
LIB_WILL_INIT,
|
2017-02-28 03:22:32 +00:00
|
|
|
SET_CONFIG,
|
|
|
|
SET_WEBRTC_READY
|
2016-10-05 14:36:59 +00:00
|
|
|
} from './actionTypes';
|
|
|
|
|
2017-02-16 19:57:39 +00:00
|
|
|
declare var APP: Object;
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
2017-02-27 22:45:53 +00:00
|
|
|
* Disposes (of) lib-jitsi-meet.
|
2016-10-05 14:36:59 +00:00
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function disposeLib() {
|
2017-02-16 19:57:39 +00:00
|
|
|
return (dispatch: Dispatch<*>) => {
|
2017-02-27 22:45:53 +00:00
|
|
|
dispatch({ type: LIB_WILL_DISPOSE });
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2017-02-27 22:45:53 +00:00
|
|
|
// XXX We're wrapping it with Promise because:
|
|
|
|
// a) to be better aligned with initLib() method which is async;
|
|
|
|
// b) as currently there is no implementation for it in lib-jitsi-meet
|
|
|
|
// and there is a big chance it will be async.
|
|
|
|
// TODO Currently, lib-jitsi-meet doesn't have the functionality to
|
|
|
|
// dispose itself.
|
|
|
|
return (
|
|
|
|
Promise.resolve()
|
|
|
|
.then(() => dispatch({ type: LIB_DID_DISPOSE })));
|
2016-10-05 14:36:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-27 22:45:53 +00:00
|
|
|
* Initializes lib-jitsi-meet (i.e. {@link invokes JitsiMeetJS.init()}) with the
|
|
|
|
* current config(uration).
|
2016-10-05 14:36:59 +00:00
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function initLib() {
|
2017-02-16 19:57:39 +00:00
|
|
|
return (dispatch: Dispatch<*>, getState: Function) => {
|
2017-02-24 19:08:49 +00:00
|
|
|
const { config } = getState()['features/base/lib-jitsi-meet'];
|
2016-10-05 14:36:59 +00:00
|
|
|
|
|
|
|
if (!config) {
|
2017-02-24 19:08:49 +00:00
|
|
|
throw new Error('Cannot init lib-jitsi-meet without config');
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 21:55:31 +00:00
|
|
|
// XXX Temporarily until conference.js is moved to the React app we
|
|
|
|
// shouldn't use JitsiMeetJS from the React app.
|
|
|
|
if (typeof APP !== 'undefined') {
|
2017-01-10 19:06:18 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2017-02-27 22:45:53 +00:00
|
|
|
dispatch({ type: LIB_WILL_INIT });
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2017-02-27 22:45:53 +00:00
|
|
|
return (
|
|
|
|
JitsiMeetJS.init(config)
|
|
|
|
.then(() => dispatch({ type: LIB_DID_INIT }))
|
|
|
|
.catch(error => {
|
|
|
|
dispatch(libInitError(error));
|
|
|
|
|
|
|
|
// TODO Handle LIB_INIT_ERROR error somewhere instead.
|
|
|
|
console.error('lib-jitsi-meet failed to init:', error);
|
|
|
|
throw error;
|
|
|
|
}));
|
2016-10-05 14:36:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-24 19:08:49 +00:00
|
|
|
/**
|
|
|
|
* Notifies about a specific error raised by {@link JitsiMeetJS.init()}.
|
|
|
|
*
|
|
|
|
* @param {Error} error - The Error raised by JitsiMeetJS.init().
|
|
|
|
* @returns {{
|
|
|
|
* type: LIB_INIT_ERROR,
|
|
|
|
* error: Error
|
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
export function libInitError(error: Error) {
|
|
|
|
return {
|
|
|
|
type: LIB_INIT_ERROR,
|
|
|
|
error
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Sets config.
|
|
|
|
*
|
2017-02-24 19:08:49 +00:00
|
|
|
* @param {Object} config - The config(uration) object in the format accepted by
|
|
|
|
* the JitsiMeetJS.init() method.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @returns {{
|
2017-02-16 19:57:39 +00:00
|
|
|
* type: SET_CONFIG,
|
|
|
|
* config: Object
|
|
|
|
* }}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2017-02-16 19:57:39 +00:00
|
|
|
export function setConfig(config: Object) {
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: SET_CONFIG,
|
|
|
|
config
|
|
|
|
};
|
|
|
|
}
|
2017-02-28 03:22:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the indicator which determines whether WebRTC is ready. In execution
|
|
|
|
* environments in which WebRTC is supported via a known plugin such
|
|
|
|
* as Temasys WebRTC may start not ready and then become ready. Of course, there
|
|
|
|
* are execution enviroments such as old Mozilla Firefox versions or
|
|
|
|
* certains Microsoft Edge versions in which WebRTC is not supported at all.
|
|
|
|
*
|
|
|
|
* @param {boolean|Promise} webRTCReady - The indicator which determines
|
|
|
|
* whether WebRTC is ready. If a Promise is specified, its resolution will be
|
|
|
|
* awaited.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function setWebRTCReady(webRTCReady: boolean | Promise<*>) {
|
|
|
|
return (dispatch: Dispatch<*>, getState: Function) => {
|
|
|
|
if (getState()['features/base/lib-jitsi-meet'].webRTCReady
|
|
|
|
!== webRTCReady) {
|
|
|
|
dispatch({
|
|
|
|
type: SET_WEBRTC_READY,
|
|
|
|
webRTCReady
|
|
|
|
});
|
|
|
|
|
|
|
|
// If the specified webRTCReady is a thenable (i.e. a Promise), then
|
|
|
|
// await its resolution.
|
|
|
|
switch (typeof webRTCReady) {
|
|
|
|
case 'function':
|
|
|
|
case 'object': {
|
|
|
|
const { then } = webRTCReady;
|
|
|
|
|
|
|
|
if (typeof then === 'function') {
|
|
|
|
const onFulfilled = value => {
|
|
|
|
// Is the app still interested in the specified
|
|
|
|
// webRTCReady?
|
|
|
|
if (getState()['features/base/lib-jitsi-meet']
|
|
|
|
.webRTCReady
|
|
|
|
=== webRTCReady) {
|
|
|
|
dispatch(setWebRTCReady(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
then.call(
|
|
|
|
webRTCReady,
|
|
|
|
/* onFulfilled */ () => onFulfilled(true),
|
|
|
|
/* onRejected*/ () => onFulfilled(false));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|