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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 namespace for all global variables, functions, etc that we need.
  18. *
  19. * @returns {Object} The namespace.
  20. *
  21. * NOTE: After React-ifying everything this should be the only global.
  22. */
  23. export function getJitsiMeetGlobalNS() {
  24. if (!window.JitsiMeetJS) {
  25. window.JitsiMeetJS = {};
  26. }
  27. if (!window.JitsiMeetJS.app) {
  28. window.JitsiMeetJS.app = {};
  29. }
  30. return window.JitsiMeetJS.app;
  31. }
  32. /**
  33. * A helper function that behaves similar to Object.assign, but only reassigns a
  34. * property in target if it's defined in source.
  35. *
  36. * @param {Object} target - The target object to assign the values into.
  37. * @param {Object} source - The source object.
  38. * @returns {Object}
  39. */
  40. export function assignIfDefined(target: Object, source: Object) {
  41. const to = Object(target);
  42. for (const nextKey in source) {
  43. if (source.hasOwnProperty(nextKey)) {
  44. const value = source[nextKey];
  45. if (typeof value !== 'undefined') {
  46. to[nextKey] = value;
  47. }
  48. }
  49. }
  50. return to;
  51. }
  52. /**
  53. * Prints the error and reports it to the global error handler.
  54. *
  55. * @param {Error} e - The error object.
  56. * @param {string} msg - A custom message to print in addition to the error.
  57. * @returns {void}
  58. */
  59. export function reportError(e: Object, msg: string = '') {
  60. logger.error(msg, e);
  61. window.onerror && window.onerror(msg, null, null, null, e);
  62. }