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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // @flow
  2. const logger = require('jitsi-meet-logger').getLogger(__filename);
  3. /**
  4. * Returns the namespace for all global variables, functions, etc that we need.
  5. *
  6. * @returns {Object} The namespace.
  7. *
  8. * NOTE: After React-ifying everything this should be the only global.
  9. */
  10. export function getJitsiMeetGlobalNS() {
  11. if (!window.JitsiMeetJS) {
  12. window.JitsiMeetJS = {};
  13. }
  14. if (!window.JitsiMeetJS.app) {
  15. window.JitsiMeetJS.app = {};
  16. }
  17. return window.JitsiMeetJS.app;
  18. }
  19. /**
  20. * Gets the description of a specific {@code Symbol}.
  21. *
  22. * @param {Symbol} symbol - The {@code Symbol} to retrieve the description of.
  23. * @private
  24. * @returns {string} The description of {@code symbol}.
  25. */
  26. export function getSymbolDescription(symbol: ?Symbol) {
  27. let description = symbol ? symbol.toString() : 'undefined';
  28. if (description.startsWith('Symbol(') && description.endsWith(')')) {
  29. description = description.slice(7, -1);
  30. }
  31. // The polyfill es6-symbol that we use does not appear to comply with the
  32. // Symbol standard and, merely, adds @@ at the beginning of the description.
  33. if (description.startsWith('@@')) {
  34. description = description.slice(2);
  35. }
  36. return description;
  37. }
  38. /**
  39. * A helper function that behaves similar to Object.assign, but only reassigns a
  40. * property in target if it's defined in source.
  41. *
  42. * @param {Object} target - The target object to assign the values into.
  43. * @param {Object} source - The source object.
  44. * @returns {Object}
  45. */
  46. export function assignIfDefined(target: Object, source: Object) {
  47. const to = Object(target);
  48. for (const nextKey in source) {
  49. if (source.hasOwnProperty(nextKey)) {
  50. const value = source[nextKey];
  51. if (typeof value !== 'undefined') {
  52. to[nextKey] = value;
  53. }
  54. }
  55. }
  56. return to;
  57. }
  58. /**
  59. * Prints the error and reports it to the global error handler.
  60. *
  61. * @param {Error} e - The error object.
  62. * @param {string} msg - A custom message to print in addition to the error.
  63. * @returns {void}
  64. */
  65. export function reportError(e: Object, msg: string = '') {
  66. logger.error(msg, e);
  67. window.onerror && window.onerror(msg, null, null, null, e);
  68. }