您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

helpers.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 a specific new URL by replacing the current location (in the
  22. * history).
  23. *
  24. * @param {string} url the URL pointing to the location where the user should
  25. * be redirected to.
  26. */
  27. export function replace(url) {
  28. window.location.replace(url);
  29. }
  30. /**
  31. * Prints the error and reports it to the global error handler.
  32. * @param e {Error} the error
  33. * @param msg {string} [optional] the message printed in addition to the error
  34. */
  35. export function reportError (e, msg = "") {
  36. logger.error(msg, e);
  37. if(window.onerror)
  38. window.onerror(msg, null, null,
  39. 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
  44. * function was invoked
  45. * @param fn
  46. * @param wait
  47. * @param options
  48. * @returns {function(...[*])}
  49. */
  50. export function debounce(fn, wait = 0, options = {}) {
  51. let leading = options.leading || false;
  52. let trailing = true;
  53. let isCalled = false;
  54. if (typeof options.trailing !== 'undefined') {
  55. trailing = options.trailing;
  56. }
  57. return (...args) => {
  58. if(!isCalled) {
  59. if (leading) {
  60. fn(...args);
  61. }
  62. setTimeout(() => {
  63. isCalled = false;
  64. if (trailing) {
  65. fn(...args);
  66. }
  67. }, wait);
  68. isCalled = true;
  69. }
  70. };
  71. }