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