Simplify the source code

If half the file is written in ES6, it is easier to read if the rest of
the file is in ES6 as well. If ES6 is used, then const is better than
let. If source code is shorter yet as readable as the long version, then
prefer the short version.
This commit is contained in:
Lyubomir Marinov 2017-02-04 21:54:58 -06:00
parent d6b0f8d4c5
commit 6c5468d904
1 changed files with 20 additions and 26 deletions

View File

@ -2,12 +2,13 @@ const logger = require("jitsi-meet-logger").getLogger(__filename);
/**
* Create deferred object.
*
* @returns {{promise, resolve, reject}}
*/
export function createDeferred () {
let deferred = {};
export function createDeferred() {
const deferred = {};
deferred.promise = new Promise(function (resolve, reject) {
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = resolve;
deferred.reject = reject;
});
@ -18,7 +19,7 @@ export function createDeferred () {
/**
* Reload page.
*/
export function reload () {
export function reload() {
window.location.reload();
}
@ -35,48 +36,41 @@ export function 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 = "") {
export function reportError(e, msg = "") {
logger.error(msg, e);
if(window.onerror)
window.onerror(msg, null, null,
null, e);
window.onerror && window.onerror(msg, null, null, null, e);
}
/**
* Creates a debounced function that delays invoking func until after wait
* milliseconds have elapsed since the last time the debounced
* function was invoked
* milliseconds have elapsed since the last time the debounced function was
* invoked.
*
* @param fn
* @param wait
* @param options
* @returns {function(...[*])}
*/
export function debounce(fn, wait = 0, options = {}) {
let leading = options.leading || false;
let trailing = true;
let isCalled = false;
if (typeof options.trailing !== 'undefined') {
trailing = options.trailing;
}
const leading = options.leading || false;
const trailing
= (typeof options.trailing === 'undefined') || options.trailing;
let called = false;
return (...args) => {
if(!isCalled) {
if (leading) {
fn(...args);
}
if (!called) {
leading && fn(...args);
setTimeout(() => {
isCalled = false;
if (trailing) {
fn(...args);
}
called = false;
trailing && fn(...args);
}, wait);
isCalled = true;
called = true;
}
};
}