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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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-ignore
  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. // @ts-ignore
  27. isOnline: netInfoState.isInternetReachable,
  28. // @ts-ignore
  29. details: netInfoState.details,
  30. networkType: netInfoState.type
  31. };
  32. }
  33. /**
  34. * Checks for support.
  35. *
  36. * @returns {boolean}
  37. */
  38. static isSupported() {
  39. return Boolean(NetInfo);
  40. }
  41. /**
  42. * Starts the service.
  43. *
  44. * @returns {void}
  45. */
  46. start() {
  47. this._subscription = NetInfo.addEventListener(netInfoState => {
  48. // @ts-ignore
  49. this.emit(ONLINE_STATE_CHANGED_EVENT, NetworkInfoService._convertNetInfoState(netInfoState));
  50. });
  51. }
  52. /**
  53. * Stops the service.
  54. *
  55. * @returns {void}
  56. */
  57. stop() {
  58. if (this._subscription) {
  59. this._subscription();
  60. // @ts-ignore
  61. this._subscription = undefined;
  62. }
  63. }
  64. }