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.

AbstractConnectionIndicator.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // @flow
  2. import { Component } from 'react';
  3. import statsEmitter from '../statsEmitter';
  4. declare var interfaceConfig: Object;
  5. const defaultAutoHideTimeout = 5000;
  6. /**
  7. * The connection quality percentage that must be reached to be considered of
  8. * good quality and can result in the connection indicator being hidden.
  9. *
  10. * @type {number}
  11. */
  12. export const INDICATOR_DISPLAY_THRESHOLD = 30;
  13. /**
  14. * The type of the React {@code Component} props of {@link ConnectionIndicator}.
  15. */
  16. export type Props = {
  17. /**
  18. * How long the connection indicator should remain displayed before hiding.
  19. */
  20. _autoHideTimeout: number,
  21. /**
  22. * The ID of the participant associated with the displayed connection indication and
  23. * stats.
  24. */
  25. participantId: string
  26. };
  27. /**
  28. * The type of the React {@code Component} state of {@link ConnectionIndicator}.
  29. */
  30. export type State = {
  31. /**
  32. * Whether or not a CSS class should be applied to the root for hiding the
  33. * connection indicator. By default the indicator should start out hidden
  34. * because the current connection status is not known at mount.
  35. */
  36. showIndicator: boolean,
  37. /**
  38. * Cache of the stats received from subscribing to stats emitting. The keys
  39. * should be the name of the stat. With each stat update, updates stats are
  40. * mixed in with cached stats and a new stats object is set in state.
  41. */
  42. stats: Object
  43. };
  44. /**
  45. * Implements a React {@link Component} which displays the current connection
  46. * quality.
  47. *
  48. * @extends {Component}
  49. */
  50. class AbstractConnectionIndicator<P: Props, S: State> extends Component<P, S> {
  51. /**
  52. * The timeout for automatically hiding the indicator.
  53. */
  54. autoHideTimeout: ?TimeoutID
  55. /**
  56. * Initializes a new {@code ConnectionIndicator} instance.
  57. *
  58. * @param {P} props - The read-only properties with which the new
  59. * instance is to be initialized.
  60. */
  61. constructor(props: P) {
  62. super(props);
  63. // Bind event handlers so they are only bound once for every instance.
  64. this._onStatsUpdated = this._onStatsUpdated.bind(this);
  65. }
  66. /**
  67. * Starts listening for stat updates.
  68. *
  69. * @inheritdoc
  70. * returns {void}
  71. */
  72. componentDidMount() {
  73. statsEmitter.subscribeToClientStats(
  74. this.props.participantId, this._onStatsUpdated);
  75. }
  76. /**
  77. * Updates which user's stats are being listened to.
  78. *
  79. * @inheritdoc
  80. * returns {void}
  81. */
  82. componentDidUpdate(prevProps: Props) {
  83. if (prevProps.participantId !== this.props.participantId) {
  84. statsEmitter.unsubscribeToClientStats(
  85. prevProps.participantId, this._onStatsUpdated);
  86. statsEmitter.subscribeToClientStats(
  87. this.props.participantId, this._onStatsUpdated);
  88. }
  89. }
  90. /**
  91. * Cleans up any queued processes, which includes listening for new stats
  92. * and clearing any timeout to hide the indicator.
  93. *
  94. * @private
  95. * @returns {void}
  96. */
  97. componentWillUnmount() {
  98. statsEmitter.unsubscribeToClientStats(
  99. this.props.participantId, this._onStatsUpdated);
  100. clearTimeout(this.autoHideTimeout);
  101. }
  102. _onStatsUpdated: (Object) => void;
  103. /**
  104. * Callback invoked when new connection stats associated with the passed in
  105. * user ID are available. Will update the component's display of current
  106. * statistics.
  107. *
  108. * @param {Object} stats - Connection stats from the library.
  109. * @private
  110. * @returns {void}
  111. */
  112. _onStatsUpdated(stats = {}) {
  113. // Rely on React to batch setState actions.
  114. const { connectionQuality } = stats;
  115. const newPercentageState = typeof connectionQuality === 'undefined'
  116. ? {} : { percent: connectionQuality };
  117. const newStats = Object.assign(
  118. {},
  119. this.state.stats,
  120. stats,
  121. newPercentageState);
  122. this.setState({
  123. stats: newStats
  124. });
  125. this._updateIndicatorAutoHide(newStats.percent);
  126. }
  127. /**
  128. * Updates the internal state for automatically hiding the indicator.
  129. *
  130. * @param {number} percent - The current connection quality percentage
  131. * between the values 0 and 100.
  132. * @private
  133. * @returns {void}
  134. */
  135. _updateIndicatorAutoHide(percent) {
  136. if (percent < INDICATOR_DISPLAY_THRESHOLD) {
  137. clearTimeout(this.autoHideTimeout);
  138. this.autoHideTimeout = undefined;
  139. this.setState({
  140. showIndicator: true
  141. });
  142. } else if (this.autoHideTimeout) {
  143. // This clause is intentionally left blank because no further action
  144. // is needed if the percent is below the threshold and there is an
  145. // autoHideTimeout set.
  146. } else {
  147. this.autoHideTimeout = setTimeout(() => {
  148. this.setState({
  149. showIndicator: false
  150. });
  151. }, this.props._autoHideTimeout);
  152. }
  153. }
  154. }
  155. /**
  156. * Maps (parts of) the Redux state to the associated props for the
  157. * {@code ConnectorIndicator} component.
  158. *
  159. * @param {Object} state - The Redux state.
  160. * @private
  161. * @returns {Props}
  162. */
  163. export function mapStateToProps(state: Object) {
  164. return {
  165. _autoHideTimeout: state['features/base/config'].connectionIndicators.autoHideTimeout ?? defaultAutoHideTimeout
  166. };
  167. }
  168. export default AbstractConnectionIndicator;