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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * Creates a debounced function that delays invoking func until after wait
  17. * milliseconds have elapsed since the last time the debounced function was
  18. * invoked.
  19. *
  20. * @param fn
  21. * @param wait
  22. * @param options
  23. * @returns {function(...[*])}
  24. */
  25. export function debounce(fn, wait = 0, options = {}) {
  26. const leading = options.leading || false;
  27. const trailing
  28. = (typeof options.trailing === 'undefined') || options.trailing;
  29. let called = false;
  30. return (...args) => {
  31. if (!called) {
  32. leading && fn(...args);
  33. setTimeout(() => {
  34. called = false;
  35. trailing && fn(...args);
  36. }, wait);
  37. called = true;
  38. }
  39. };
  40. }
  41. /**
  42. * Returns the namespace for all global variables, functions, etc that we need.
  43. *
  44. * @returns {Object} the namespace.
  45. *
  46. * NOTE: After React-ifying everything this should be the only global.
  47. */
  48. export function getJitsiMeetGlobalNS() {
  49. if (!window.JitsiMeetJS) {
  50. window.JitsiMeetJS = {};
  51. }
  52. if (!window.JitsiMeetJS.app) {
  53. window.JitsiMeetJS.app = {};
  54. }
  55. return window.JitsiMeetJS.app;
  56. }
  57. /**
  58. * Reload page.
  59. */
  60. export function reload() {
  61. window.location.reload();
  62. }
  63. /**
  64. * Redirects to a specific new URL by replacing the current location (in the
  65. * history).
  66. *
  67. * @param {string} url the URL pointing to the location where the user should
  68. * be redirected to.
  69. */
  70. export function replace(url) {
  71. window.location.replace(url);
  72. }
  73. /**
  74. * Prints the error and reports it to the global error handler.
  75. *
  76. * @param e {Error} the error
  77. * @param msg {string} [optional] the message printed in addition to the error
  78. */
  79. export function reportError(e, msg = "") {
  80. logger.error(msg, e);
  81. window.onerror && window.onerror(msg, null, null, null, e);
  82. }