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

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