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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { Component } from 'react';
  2. import { IReduxState } from '../../app/types';
  3. import { getVirtualScreenshareParticipantOwnerId } from '../../base/participants/functions';
  4. import statsEmitter from '../statsEmitter';
  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 interface IProps {
  17. /**
  18. * How long the connection indicator should remain displayed before hiding.
  19. */
  20. _autoHideTimeout: number;
  21. /**
  22. * Whether or not the statistics are for screen share.
  23. */
  24. _isVirtualScreenshareParticipant: boolean;
  25. /**
  26. * Custom icon style.
  27. */
  28. iconStyle?: Object;
  29. /**
  30. * The ID of the participant associated with the displayed connection indication and
  31. * stats.
  32. */
  33. participantId: string;
  34. }
  35. /**
  36. * The type of the React {@code Component} state of {@link ConnectionIndicator}.
  37. */
  38. export interface IState {
  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: {
  51. percent?: number;
  52. };
  53. }
  54. /**
  55. * Implements a React {@link Component} which displays the current connection
  56. * quality.
  57. *
  58. * @augments {Component}
  59. */
  60. class AbstractConnectionIndicator<P extends IProps, S extends IState> extends Component<P, S> {
  61. /**
  62. * The timeout for automatically hiding the indicator.
  63. */
  64. autoHideTimeout: number | undefined;
  65. /**
  66. * Initializes a new {@code ConnectionIndicator} instance.
  67. *
  68. * @param {P} props - The read-only properties with which the new
  69. * instance is to be initialized.
  70. */
  71. constructor(props: P) {
  72. super(props);
  73. // Bind event handlers so they are only bound once for every instance.
  74. this._onStatsUpdated = this._onStatsUpdated.bind(this);
  75. }
  76. /**
  77. * Starts listening for stat updates.
  78. *
  79. * @inheritdoc
  80. * returns {void}
  81. */
  82. componentDidMount() {
  83. statsEmitter.subscribeToClientStats(this._getRealParticipantId(this.props), this._onStatsUpdated);
  84. }
  85. /**
  86. * Updates which user's stats are being listened to.
  87. *
  88. * @inheritdoc
  89. * returns {void}
  90. */
  91. componentDidUpdate(prevProps: IProps) {
  92. const prevParticipantId = this._getRealParticipantId(prevProps);
  93. const participantId = this._getRealParticipantId(this.props);
  94. if (prevParticipantId !== participantId) {
  95. statsEmitter.unsubscribeToClientStats(prevParticipantId, this._onStatsUpdated);
  96. statsEmitter.subscribeToClientStats(participantId, this._onStatsUpdated);
  97. }
  98. }
  99. /**
  100. * Cleans up any queued processes, which includes listening for new stats
  101. * and clearing any timeout to hide the indicator.
  102. *
  103. * @private
  104. * @returns {void}
  105. */
  106. componentWillUnmount() {
  107. statsEmitter.unsubscribeToClientStats(this._getRealParticipantId(this.props), this._onStatsUpdated);
  108. clearTimeout(this.autoHideTimeout ?? 0);
  109. }
  110. /**
  111. * Gets the "real" participant ID. FOr a virtual screenshare participant, that is its "owner".
  112. *
  113. * @param {Props} props - The props where to extract the data from.
  114. * @returns {string | undefined } The resolved participant ID.
  115. */
  116. _getRealParticipantId(props: IProps) {
  117. if (props._isVirtualScreenshareParticipant) {
  118. return getVirtualScreenshareParticipantOwnerId(props.participantId);
  119. }
  120. return props.participantId;
  121. }
  122. /**
  123. * Callback invoked when new connection stats associated with the passed in
  124. * user ID are available. Will update the component's display of current
  125. * statistics.
  126. *
  127. * @param {Object} stats - Connection stats from the library.
  128. * @private
  129. * @returns {void}
  130. */
  131. _onStatsUpdated(stats = { connectionQuality: undefined }) {
  132. // Rely on React to batch setState actions.
  133. const { connectionQuality } = stats;
  134. const newPercentageState = typeof connectionQuality === 'undefined'
  135. ? {} : { percent: connectionQuality };
  136. const newStats = Object.assign(
  137. {},
  138. this.state.stats,
  139. stats,
  140. newPercentageState);
  141. this.setState({
  142. stats: newStats
  143. });
  144. this._updateIndicatorAutoHide(newStats.percent ?? 0);
  145. }
  146. /**
  147. * Updates the internal state for automatically hiding the indicator.
  148. *
  149. * @param {number} percent - The current connection quality percentage
  150. * between the values 0 and 100.
  151. * @private
  152. * @returns {void}
  153. */
  154. _updateIndicatorAutoHide(percent: number) {
  155. if (percent < INDICATOR_DISPLAY_THRESHOLD) {
  156. clearTimeout(this.autoHideTimeout ?? 0);
  157. this.autoHideTimeout = undefined;
  158. this.setState({
  159. showIndicator: true
  160. });
  161. } else if (this.autoHideTimeout) {
  162. // This clause is intentionally left blank because no further action
  163. // is needed if the percent is below the threshold and there is an
  164. // autoHideTimeout set.
  165. } else {
  166. this.autoHideTimeout = window.setTimeout(() => {
  167. this.setState({
  168. showIndicator: false
  169. });
  170. }, this.props._autoHideTimeout);
  171. }
  172. }
  173. }
  174. /**
  175. * Maps (parts of) the Redux state to the associated props for the
  176. * {@code ConnectorIndicator} component.
  177. *
  178. * @param {Object} state - The Redux state.
  179. * @private
  180. * @returns {IProps}
  181. */
  182. export function mapStateToProps(state: IReduxState) {
  183. return {
  184. _autoHideTimeout: state['features/base/config'].connectionIndicators?.autoHideTimeout ?? defaultAutoHideTimeout
  185. };
  186. }
  187. export default AbstractConnectionIndicator;