Geen omschrijving

index.ts 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. */
  9. import {useState, useEffect} from 'react';
  10. import DeprecatedUtils from './internal/deprecatedUtils';
  11. import DeprecatedState from './internal/deprecatedState';
  12. import * as DeprecatedTypes from './internal/deprecatedTypes';
  13. import State from './internal/state';
  14. import * as Types from './internal/types';
  15. // Call the setup methods of the two state modules right away
  16. State.setup();
  17. DeprecatedState.setup();
  18. const _isConnectedListeners = new Map<
  19. DeprecatedTypes.IsConnectedHandler,
  20. /// @ts-ignore Typescript des not like the trailing comma that Prettier insists upon
  21. Types.NetInfoChangeHandler
  22. >();
  23. /**
  24. * Returns a `Promise` that resolves to a `NetInfoState` object.
  25. *
  26. * @returns A Promise which contains the current connection state.
  27. */
  28. export function fetch(): Promise<Types.NetInfoState> {
  29. return State.latest();
  30. }
  31. /**
  32. * Subscribe to connection information. The callback is called with a parameter of type
  33. * [`NetInfoState`](README.md#netinfostate) whenever the connection state changes. Your listener
  34. * will be called with the latest information soon after you subscribe and then with any
  35. * subsequent changes afterwards. You should not assume that the listener is called in the same
  36. * way across devices or platforms.
  37. *
  38. * @param listener The listener which is called when the network state changes.
  39. *
  40. * @returns An ofunction which can be called to unsubscribe.
  41. */
  42. export function addEventListener(
  43. listener: Types.NetInfoChangeHandler,
  44. ): Types.NetInfoSubscription;
  45. /**
  46. * Deprecated network state listener. You should remove the event name and change your handler to
  47. * use the new state shape.
  48. *
  49. * @deprecated
  50. *
  51. * @param type The event type.
  52. * @param deprecatedHandler The listener.
  53. *
  54. * @returns An object with a remove function which can be called to unsubscribe.
  55. */
  56. export function addEventListener(
  57. type: string,
  58. deprecatedHandler: DeprecatedTypes.ChangeHandler,
  59. ): DeprecatedTypes.Subscription;
  60. // Implementation of the overloaded methods above
  61. export function addEventListener(
  62. listenerOrType: Types.NetInfoChangeHandler | string,
  63. deprecatedHandler: DeprecatedTypes.ChangeHandler | undefined = undefined,
  64. ): Types.NetInfoSubscription | DeprecatedTypes.Subscription {
  65. if (typeof listenerOrType === 'string') {
  66. DeprecatedUtils.warnOnce();
  67. if (
  68. listenerOrType === DeprecatedTypes.CHANGE_EVENT_NAME &&
  69. deprecatedHandler
  70. ) {
  71. DeprecatedState.add(deprecatedHandler);
  72. return {
  73. remove: (): void => {
  74. DeprecatedState.remove(deprecatedHandler);
  75. },
  76. };
  77. } else {
  78. return {
  79. remove: (): void => {},
  80. };
  81. }
  82. } else {
  83. const listener = listenerOrType;
  84. State.add(listener);
  85. return (): void => {
  86. State.remove(listener);
  87. };
  88. }
  89. }
  90. /**
  91. * A React Hook which updates when the connection state changes.
  92. *
  93. * @returns The connection state.
  94. */
  95. export function useNetInfo(): Types.NetInfoState {
  96. const [netInfo, setNetInfo] = useState<Types.NetInfoState>({
  97. type: Types.NetInfoStateType.unknown,
  98. isConnected: false,
  99. isInternetReachable: false,
  100. details: null,
  101. });
  102. useEffect((): (() => void) => {
  103. return addEventListener(setNetInfo);
  104. }, []);
  105. return netInfo;
  106. }
  107. /**
  108. * Deprecated method to remove the listener. You should upgrade to the new API.
  109. *
  110. * @deprecated
  111. *
  112. * @param type The event type.
  113. * @param handler The event listener.
  114. */
  115. export function removeEventListener(
  116. type: string,
  117. handler: DeprecatedTypes.ChangeHandler,
  118. ): void {
  119. DeprecatedUtils.warnOnce();
  120. if (type === DeprecatedTypes.CHANGE_EVENT_NAME) {
  121. DeprecatedState.remove(handler);
  122. }
  123. }
  124. /**
  125. * Deprecated method to get the current state. You should upgrade to the new `fetch` method and
  126. * handle the new state type.
  127. *
  128. * @deprecated
  129. */
  130. export function getConnectionInfo(): Promise<DeprecatedTypes.NetInfoData> {
  131. DeprecatedUtils.warnOnce();
  132. return DeprecatedState.latest();
  133. }
  134. /**
  135. * Deprecated method to tell if the current connection is "expensive". Only available on Android.
  136. * You should now call the `fetch` method and look at the `details.isConnectionExpensive` property.
  137. *
  138. * @deprecated
  139. */
  140. export function isConnectionExpensive(): Promise<boolean> {
  141. DeprecatedUtils.warnOnce();
  142. return State.latest().then(DeprecatedUtils.isConnectionExpensive);
  143. }
  144. export const isConnected = {
  145. /**
  146. * Deprecated method to listen for changes to the connected boolean. You should now use the
  147. * normal `addEventListener` method and look at the `isConnected` property.
  148. *
  149. * @deprecated
  150. */
  151. addEventListener: (
  152. eventName: string,
  153. handler: DeprecatedTypes.IsConnectedHandler,
  154. ): DeprecatedTypes.Subscription => {
  155. if (eventName !== DeprecatedTypes.CHANGE_EVENT_NAME) {
  156. return {remove: (): void => {}};
  157. }
  158. const listener = (state: Types.NetInfoState): void => {
  159. handler(DeprecatedUtils.isConnected(state));
  160. };
  161. _isConnectedListeners.set(handler, listener);
  162. State.add(listener);
  163. return {
  164. remove: (): void => {
  165. State.remove(listener);
  166. },
  167. };
  168. },
  169. /**
  170. * Deprecated method to stop listening for changes to the connected boolean. You should now use
  171. * the normal `addEventListener` method and look at the `isConnected` property.
  172. *
  173. * @deprecated
  174. */
  175. removeEventListener: (
  176. _eventName: string,
  177. handler: DeprecatedTypes.IsConnectedHandler,
  178. ): void => {
  179. const listener = _isConnectedListeners.get(handler);
  180. listener && State.remove(listener);
  181. _isConnectedListeners.delete(handler);
  182. },
  183. /**
  184. * Deprecated method to get the current is connected boolean. You should now use the normal
  185. * `fetch` method and look at the `isConnected` property.
  186. *
  187. * @deprecated
  188. */
  189. fetch: (): Promise<boolean> => {
  190. return State.latest().then(DeprecatedUtils.isConnected);
  191. },
  192. };
  193. export * from './internal/types';
  194. export * from './internal/deprecatedTypes';
  195. export default {
  196. fetch,
  197. addEventListener,
  198. useNetInfo,
  199. removeEventListener,
  200. getConnectionInfo,
  201. isConnectionExpensive,
  202. isConnected,
  203. };