feat(base/connection): throw error and add isInviteURLReady

This commit is contained in:
paweldomas 2019-04-23 13:37:19 -05:00 committed by Saúl Ibarra Corretgé
parent 768cff48a4
commit e4af5ddbe9
1 changed files with 29 additions and 5 deletions

View File

@ -3,23 +3,47 @@
import { toState } from '../redux';
/**
* Retrieves a simplified version of the conference/location URL stripped of URL
* params (i.e. Query/search and hash) which should be used for sending invites.
* Retrieves a simplified version of the conference/location URL stripped of URL params (i.e. Query/search and hash)
* which should be used for sending invites.
* NOTE that the method will throw an error if called too early. That is before the conference is joined or before
* the process of joining one has started. This limitation does not apply to the case when called with the URL object
* instance. Use {@link isInviteURLReady} to check if it's safe to call the method already.
*
* @param {Function|Object} stateOrGetState - The redux state or redux's
* {@code getState} function.
* @param {Function|Object} stateOrGetState - The redux state or redux's {@code getState} function or the URL object
* to be stripped.
* @returns {string}
*/
export function getInviteURL(stateOrGetState: Function | Object): string {
const state = toState(stateOrGetState);
const locationURL
let locationURL
= state instanceof URL
? state
: state['features/base/connection'].locationURL;
// If there's no locationURL on the base/connection feature try the base/config where it's set earlier.
if (!locationURL) {
locationURL = state['features/base/config'].locationURL;
}
if (!locationURL) {
throw new Error('Can not get invite URL - the app is not ready');
}
return getURLWithoutParams(locationURL).href;
}
/**
* Checks whether or not is safe to call the {@link getInviteURL} method already.
*
* @param {Function|Object} stateOrGetState - The redux state or redux's {@code getState} function.
* @returns {boolean}
*/
export function isInviteURLReady(stateOrGetState: Function | Object): boolean {
const state = toState(stateOrGetState);
return Boolean(state['features/base/connection'].locationURL || state['features/base/config'].locationURL);
}
/**
* Gets a {@link URL} without hash and query/search params from a specific
* {@code URL}.