You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const logger = require("jitsi-meet-logger").getLogger(__filename);
  2. /**
  3. * Create deferred object.
  4. * @returns {{promise, resolve, reject}}
  5. */
  6. export function createDeferred () {
  7. let deferred = {};
  8. deferred.promise = new Promise(function (resolve, reject) {
  9. deferred.resolve = resolve;
  10. deferred.reject = reject;
  11. });
  12. return deferred;
  13. }
  14. /**
  15. * Reload page.
  16. */
  17. export function reload () {
  18. window.location.reload();
  19. }
  20. /**
  21. * Redirects to new URL.
  22. * @param {string} url the URL pointing to the location where the user should
  23. * be redirected to.
  24. */
  25. export function redirect (url) {
  26. window.location.replace(url);
  27. }
  28. /**
  29. * Prints the error and reports it to the global error handler.
  30. * @param e {Error} the error
  31. * @param msg {string} [optional] the message printed in addition to the error
  32. */
  33. export function reportError (e, msg = "") {
  34. logger.error(msg, e);
  35. if(window.onerror)
  36. window.onerror(msg, null, null,
  37. null, e);
  38. }
  39. /**
  40. * Creates a debounced function that delays invoking func until after wait
  41. * milliseconds have elapsed since the last time the debounced
  42. * function was invoked
  43. * @param fn
  44. * @param wait
  45. * @param options
  46. * @returns {function(...[*])}
  47. */
  48. export function debounce(fn, wait = 0, options = {}) {
  49. let leading = options.leading || false;
  50. let trailing = true;
  51. let isCalled = false;
  52. if (typeof options.trailing !== 'undefined') {
  53. trailing = options.trailing;
  54. }
  55. return (...args) => {
  56. if(!isCalled) {
  57. if (leading) {
  58. fn(...args);
  59. }
  60. setTimeout(() => {
  61. isCalled = false;
  62. if (trailing) {
  63. fn(...args);
  64. }
  65. }, wait);
  66. isCalled = true;
  67. }
  68. };
  69. }