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

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