UNPKG

3.29 kBJavaScriptView Raw
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 */
9import { useState, useEffect } from 'react';
10import { Platform } from 'react-native';
11import DEFAULT_CONFIGURATION from './internal/defaultConfiguration';
12import NativeInterface from './internal/nativeInterface';
13import State from './internal/state';
14import * as Types from './internal/types'; // Stores the currently used configuration
15
16let _configuration = DEFAULT_CONFIGURATION; // Stores the singleton reference to the state manager
17
18let _state = null;
19
20const createState = () => {
21 return new State(_configuration);
22};
23/**
24 * Configures the library with the given configuration. Note that calling this will stop all
25 * previously added listeners from being called again. It is best to call this right when your
26 * application is started to avoid issues.
27 *
28 * @param configuration The new configuration to set.
29 */
30
31
32export function configure(configuration) {
33 _configuration = { ...DEFAULT_CONFIGURATION,
34 ...configuration
35 };
36
37 if (_state) {
38 _state.tearDown();
39
40 _state = createState();
41 }
42
43 if (Platform.OS === 'ios') {
44 NativeInterface.configure(configuration);
45 }
46}
47/**
48 * Returns a `Promise` that resolves to a `NetInfoState` object.
49 *
50 * @param [requestedInterface] interface from which to obtain the information
51 *
52 * @returns A Promise which contains the current connection state.
53 */
54
55export function fetch(requestedInterface) {
56 if (!_state) {
57 _state = createState();
58 }
59
60 return _state.latest(requestedInterface);
61}
62/**
63 * Force-refreshes the internal state of the NetInfo library.
64 *
65 * @returns A Promise which contains the updated connection state.
66 */
67
68export function refresh() {
69 if (!_state) {
70 _state = createState();
71 }
72
73 return _state._fetchCurrentState();
74}
75/**
76 * Subscribe to connection information. The callback is called with a parameter of type
77 * [`NetInfoState`](README.md#netinfostate) whenever the connection state changes. Your listener
78 * will be called with the latest information soon after you subscribe and then with any
79 * subsequent changes afterwards. You should not assume that the listener is called in the same
80 * way across devices or platforms.
81 *
82 * @param listener The listener which is called when the network state changes.
83 *
84 * @returns A function which can be called to unsubscribe.
85 */
86
87export function addEventListener(listener) {
88 if (!_state) {
89 _state = createState();
90 }
91
92 _state.add(listener);
93
94 return () => {
95 _state && _state.remove(listener);
96 };
97}
98/**
99 * A React Hook which updates when the connection state changes.
100 *
101 * @returns The connection state.
102 */
103
104export function useNetInfo(configuration) {
105 if (configuration) {
106 configure(configuration);
107 }
108
109 const [netInfo, setNetInfo] = useState({
110 type: Types.NetInfoStateType.unknown,
111 isConnected: null,
112 isInternetReachable: null,
113 details: null
114 });
115 useEffect(() => {
116 return addEventListener(setNetInfo);
117 }, []);
118 return netInfo;
119}
120export * from './internal/types';
121export default {
122 configure,
123 fetch,
124 refresh,
125 addEventListener,
126 useNetInfo
127};
128//# sourceMappingURL=index.js.map
\No newline at end of file