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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // @flow
  2. const logger = require('jitsi-meet-logger').getLogger(__filename);
  3. /**
  4. * Creates a deferred object.
  5. *
  6. * @returns {{promise, resolve, reject}}
  7. */
  8. export function createDeferred(): Object {
  9. const deferred = {};
  10. deferred.promise = new Promise((resolve, reject) => {
  11. deferred.resolve = resolve;
  12. deferred.reject = reject;
  13. });
  14. return deferred;
  15. }
  16. /**
  17. * Returns the base URL of the app.
  18. *
  19. * @param {Object} w - Window object to use instead of the built in one.
  20. * @returns {string}
  21. */
  22. export function getBaseUrl(w: Object = window) {
  23. const doc = w.document;
  24. const base = doc.querySelector('base');
  25. if (base && base.href) {
  26. return base.href;
  27. }
  28. const { protocol, host } = w.location;
  29. return `${protocol}//${host}`;
  30. }
  31. /**
  32. * Returns the namespace for all global variables, functions, etc that we need.
  33. *
  34. * @returns {Object} The namespace.
  35. *
  36. * NOTE: After React-ifying everything this should be the only global.
  37. */
  38. export function getJitsiMeetGlobalNS() {
  39. if (!window.JitsiMeetJS) {
  40. window.JitsiMeetJS = {};
  41. }
  42. if (!window.JitsiMeetJS.app) {
  43. window.JitsiMeetJS.app = {};
  44. }
  45. return window.JitsiMeetJS.app;
  46. }
  47. /**
  48. * A helper function that behaves similar to Object.assign, but only reassigns a
  49. * property in target if it's defined in source.
  50. *
  51. * @param {Object} target - The target object to assign the values into.
  52. * @param {Object} source - The source object.
  53. * @returns {Object}
  54. */
  55. export function assignIfDefined(target: Object, source: Object) {
  56. const to = Object(target);
  57. for (const nextKey in source) {
  58. if (source.hasOwnProperty(nextKey)) {
  59. const value = source[nextKey];
  60. if (typeof value !== 'undefined') {
  61. to[nextKey] = value;
  62. }
  63. }
  64. }
  65. return to;
  66. }
  67. /**
  68. * Prints the error and reports it to the global error handler.
  69. *
  70. * @param {Error} e - The error object.
  71. * @param {string} msg - A custom message to print in addition to the error.
  72. * @returns {void}
  73. */
  74. export function reportError(e: Object, msg: string = '') {
  75. logger.error(msg, e);
  76. window.onerror && window.onerror(msg, null, null, null, e);
  77. }