UNPKG

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