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.js 1.7KB

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