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.

NetworkInfo.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { getLogger } from '@jitsi/logger';
  2. import Listenable from '../util/Listenable';
  3. export const NETWORK_INFO_EVENT = 'NETWORK_INFO_CHANGED';
  4. const logger = getLogger(__filename);
  5. /**
  6. * Module provides information about the current status of the internet
  7. * connection. Lib-jitsi-meet doesn't have any logic for detecting internet
  8. * online/offline, but rather it relies on the information supplied by the app
  9. * that uses it. By default the online state is assumed and the lib acts as if
  10. * it was connected. See {@link JitsiMeetJS.setNetworkInfo}.
  11. */
  12. export class NetworkInfo extends Listenable {
  13. /**
  14. * Creates new {@link NetworkInfo} instance.
  15. */
  16. constructor() {
  17. super();
  18. this._current = {
  19. isOnline: true
  20. };
  21. }
  22. /**
  23. * Updates the network info state.
  24. *
  25. * @param {object} state - The network info state.
  26. * @param {boolean} state.isOnline - {@code true} if the internet connectivity is online or {@code false}
  27. * otherwise.
  28. */
  29. updateNetworkInfo({ isOnline }) {
  30. logger.debug('updateNetworkInfo', { isOnline });
  31. this._current = {
  32. isOnline: isOnline === true
  33. };
  34. this.eventEmitter.emit(NETWORK_INFO_EVENT, this._current);
  35. }
  36. /**
  37. * Returns the online/offline internet status. By default the value is {@code true} and changes only if
  38. * the lib's user wires the state through {@link JitsiMeetJS.setNetworkInfo} like the jitsi-meet does. Because of
  39. * that any logic should still assume that the internet may be offline and should handle the failure gracefully.
  40. * It's only a good hint in the other way around: to pause internet operations until it comes back online.
  41. * @returns {boolean}
  42. */
  43. isOnline() {
  44. return this._current.isOnline === true;
  45. }
  46. }
  47. const networkInfo = new NetworkInfo();
  48. export default networkInfo;