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.

middleware.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* @flow */
  2. import Logger from 'jitsi-meet-logger';
  3. import { APP_WILL_MOUNT } from '../../app';
  4. import JitsiMeetJS, { LIB_WILL_INIT } from '../lib-jitsi-meet';
  5. import { MiddlewareRegistry } from '../redux';
  6. import { SET_LOGGING_CONFIG } from './actionTypes';
  7. declare var APP: Object;
  8. /**
  9. * The Redux middleware of the feature base/logging.
  10. *
  11. * @param {Store} store - The Redux store.
  12. * @returns {Function}
  13. * @private
  14. */
  15. MiddlewareRegistry.register(store => next => action => {
  16. switch (action.type) {
  17. case APP_WILL_MOUNT:
  18. return _appWillMount(store, next, action);
  19. case LIB_WILL_INIT:
  20. return _libWillInit(store, next, action);
  21. case SET_LOGGING_CONFIG:
  22. return _setLoggingConfig(store, next, action);
  23. }
  24. return next(action);
  25. });
  26. /**
  27. * Notifies the feature base/logging that the action {@link APP_WILL_MOUNT} is
  28. * being dispatched within a specific Redux {@code store}.
  29. *
  30. * @param {Store} store - The Redux store in which the specified {@code action}
  31. * is being dispatched.
  32. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  33. * specified {@code action} to the specified {@code store}.
  34. * @param {Action} action - The Redux action {@code APP_WILL_MOUNT} which is
  35. * being dispatched in the specified {@code store}.
  36. * @private
  37. * @returns {Object} The new state that is the result of the reduction of the
  38. * specified {@code action}.
  39. */
  40. function _appWillMount({ getState }, next, action) {
  41. const { config } = getState()['features/base/logging'];
  42. _setLogLevels(Logger, config);
  43. // FIXME Until the logic of conference.js is rewritten into the React
  44. // app we, JitsiMeetJS.init is to not be used for the React app.
  45. // Consequently, LIB_WILL_INIT will not be dispatched. In the meantime, do
  46. // the following:
  47. typeof APP === 'undefined' || _setLogLevels(JitsiMeetJS, config);
  48. return next(action);
  49. }
  50. /**
  51. * Notifies the feature base/logging that the action {@link LIB_WILL_INIT} is
  52. * being dispatched within a specific Redux {@code store}.
  53. *
  54. * @param {Store} store - The Redux store in which the specified {@code action}
  55. * is being dispatched.
  56. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  57. * specified {@code action} to the specified {@code store}.
  58. * @param {Action} action - The Redux action {@code LIB_WILL_INIT} which is
  59. * being dispatched in the specified {@code store}.
  60. * @private
  61. * @returns {Object} The new state that is the result of the reduction of the
  62. * specified {@code action}.
  63. */
  64. function _libWillInit({ getState }, next, action) {
  65. _setLogLevels(JitsiMeetJS, getState()['features/base/logging'].config);
  66. return next(action);
  67. }
  68. /**
  69. * Notifies the feature base/logging that the action {@link SET_LOGGING_CONFIG}
  70. * is being dispatched within a specific Redux {@code store}.
  71. *
  72. * @param {Store} store - The Redux store in which the specified {@code action}
  73. * is being dispatched.
  74. * @param {Dispatch} next - The Redux {@code dispatch} function to dispatch the
  75. * specified {@code action} to the specified {@code store}.
  76. * @param {Action} action - The Redux action {@code SET_LOGGING_CONFIG} which is
  77. * being dispatched in the specified {@code store}.
  78. * @private
  79. * @returns {Object} The new state that is the result of the reduction of the
  80. * specified {@code action}.
  81. */
  82. function _setLoggingConfig({ getState }, next, action) {
  83. const oldValue = getState()['features/base/logging'].config;
  84. const result = next(action);
  85. const newValue = getState()['features/base/logging'].config;
  86. if (oldValue !== newValue) {
  87. _setLogLevels(Logger, newValue);
  88. _setLogLevels(JitsiMeetJS, newValue);
  89. }
  90. return result;
  91. }
  92. /**
  93. * Sets the log levels of {@link Logger} or {@link JitsiMeetJS} in accord with
  94. * a specific configuration.
  95. *
  96. * @param {Object} logger - The object on which the log levels are to be set.
  97. * @param {Object} config - The configuration specifying the log levels to be
  98. * set on {@code Logger} or {@code JitsiMeetJS}.
  99. * @private
  100. * @returns {void}
  101. */
  102. function _setLogLevels(logger, config) {
  103. // XXX The loggers of the library lib-jitsi-meet and the application
  104. // jitsi-meet are separate, so the log levels have to be set in both.
  105. // First, set the default log level.
  106. logger.setLogLevel(config.defaultLogLevel);
  107. // Second, set the log level of each logger explictly overriden by config.
  108. Object.keys(config).forEach(
  109. id =>
  110. id === 'defaultLogLevel' || logger.setLogLevelById(config[id], id));
  111. }