2016-11-11 15:00:54 +00:00
|
|
|
const logger = require("jitsi-meet-logger").getLogger(__filename);
|
|
|
|
|
2016-01-22 15:08:58 +00:00
|
|
|
/**
|
|
|
|
* Create deferred object.
|
2017-02-05 03:54:58 +00:00
|
|
|
*
|
2016-01-22 15:08:58 +00:00
|
|
|
* @returns {{promise, resolve, reject}}
|
|
|
|
*/
|
2017-02-05 03:54:58 +00:00
|
|
|
export function createDeferred() {
|
|
|
|
const deferred = {};
|
2016-01-22 15:08:58 +00:00
|
|
|
|
2017-02-05 03:54:58 +00:00
|
|
|
deferred.promise = new Promise((resolve, reject) => {
|
2016-01-22 15:08:58 +00:00
|
|
|
deferred.resolve = resolve;
|
|
|
|
deferred.reject = reject;
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred;
|
|
|
|
}
|
2016-02-05 15:04:48 +00:00
|
|
|
|
2016-10-25 11:55:43 +00:00
|
|
|
/**
|
|
|
|
* Creates a debounced function that delays invoking func until after wait
|
2017-02-05 03:54:58 +00:00
|
|
|
* milliseconds have elapsed since the last time the debounced function was
|
|
|
|
* invoked.
|
|
|
|
*
|
2016-10-25 11:55:43 +00:00
|
|
|
* @param fn
|
|
|
|
* @param wait
|
|
|
|
* @param options
|
|
|
|
* @returns {function(...[*])}
|
|
|
|
*/
|
|
|
|
export function debounce(fn, wait = 0, options = {}) {
|
2017-02-05 03:54:58 +00:00
|
|
|
const leading = options.leading || false;
|
|
|
|
const trailing
|
|
|
|
= (typeof options.trailing === 'undefined') || options.trailing;
|
|
|
|
let called = false;
|
2016-10-25 11:55:43 +00:00
|
|
|
|
|
|
|
return (...args) => {
|
2017-02-05 03:54:58 +00:00
|
|
|
if (!called) {
|
|
|
|
leading && fn(...args);
|
2016-10-25 11:55:43 +00:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2017-02-05 03:54:58 +00:00
|
|
|
called = false;
|
|
|
|
trailing && fn(...args);
|
2016-10-25 11:55:43 +00:00
|
|
|
}, wait);
|
|
|
|
|
2017-02-05 03:54:58 +00:00
|
|
|
called = true;
|
2016-10-25 11:55:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-04-17 15:52:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the namespace for all global variables, functions, etc that we need.
|
|
|
|
*
|
|
|
|
* @returns {Object} the namespace.
|
|
|
|
*
|
2017-05-02 22:39:36 +00:00
|
|
|
* NOTE: After React-ifying everything this should be the only global.
|
2017-04-17 15:52:31 +00:00
|
|
|
*/
|
|
|
|
export function getJitsiMeetGlobalNS() {
|
2017-05-02 22:39:36 +00:00
|
|
|
if (!window.JitsiMeetJS) {
|
|
|
|
window.JitsiMeetJS = {};
|
2017-04-17 15:52:31 +00:00
|
|
|
}
|
2017-05-02 22:39:36 +00:00
|
|
|
if (!window.JitsiMeetJS.app) {
|
|
|
|
window.JitsiMeetJS.app = {};
|
2017-04-28 20:24:20 +00:00
|
|
|
}
|
|
|
|
return window.JitsiMeetJS.app;
|
2017-04-17 15:52:31 +00:00
|
|
|
}
|
2017-05-02 22:39:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reload page.
|
|
|
|
*/
|
|
|
|
export function reload() {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirects to a specific new URL by replacing the current location (in the
|
|
|
|
* history).
|
|
|
|
*
|
|
|
|
* @param {string} url the URL pointing to the location where the user should
|
|
|
|
* be redirected to.
|
|
|
|
*/
|
|
|
|
export function replace(url) {
|
|
|
|
window.location.replace(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints the error and reports it to the global error handler.
|
|
|
|
*
|
|
|
|
* @param e {Error} the error
|
|
|
|
* @param msg {string} [optional] the message printed in addition to the error
|
|
|
|
*/
|
|
|
|
export function reportError(e, msg = "") {
|
|
|
|
logger.error(msg, e);
|
|
|
|
window.onerror && window.onerror(msg, null, null, null, e);
|
|
|
|
}
|