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 6.3KB

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