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.

NetworkInfoService.native.ts 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import NetInfo from '@react-native-community/netinfo';
  2. import type { NetInfoState, NetInfoSubscription } from '@react-native-community/netinfo';
  3. // eslint-disable-next-line lines-around-comment
  4. // @ts-expect-error
  5. import EventEmitter from 'events';
  6. import { ONLINE_STATE_CHANGED_EVENT } from './events';
  7. import type { NetworkInfo } from './types';
  8. /**
  9. * The network info service implementation for iOS and Android. 'react-native-netinfo' seems to support windows as well,
  10. * but that has not been tested and is nto used by jitsi-meet.
  11. */
  12. export default class NetworkInfoService extends EventEmitter {
  13. /**
  14. * Stores the native subscription for future cleanup.
  15. */
  16. _subscription?: NetInfoSubscription;
  17. /**
  18. * Converts library's structure to {@link NetworkInfo} used by jitsi-meet.
  19. *
  20. * @param {NetInfoState} netInfoState - The new state given by the native library.
  21. * @private
  22. * @returns {NetworkInfo}
  23. */
  24. static _convertNetInfoState(netInfoState: NetInfoState): NetworkInfo {
  25. return {
  26. isOnline: Boolean(netInfoState.isInternetReachable),
  27. details: netInfoState.details,
  28. networkType: netInfoState.type
  29. };
  30. }
  31. /**
  32. * Checks for support.
  33. *
  34. * @returns {boolean}
  35. */
  36. static isSupported() {
  37. return Boolean(NetInfo);
  38. }
  39. /**
  40. * Starts the service.
  41. *
  42. * @returns {void}
  43. */
  44. start() {
  45. this._subscription = NetInfo.addEventListener(netInfoState => {
  46. super.emit(ONLINE_STATE_CHANGED_EVENT, NetworkInfoService._convertNetInfoState(netInfoState));
  47. });
  48. }
  49. /**
  50. * Stops the service.
  51. *
  52. * @returns {void}
  53. */
  54. stop() {
  55. if (this._subscription) {
  56. this._subscription();
  57. this._subscription = undefined;
  58. }
  59. }
  60. }