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.7KB

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