Skip to content

Commit eaed080

Browse files
authored
fix: ensure one reachability check in-flight at once / proper useEffect listener cleanup (#732)
* fix: track ongoing requests during refresh * update example * add a request queue
1 parent 060e5e2 commit eaed080

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

example/ConnectionInfoRefresh.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,21 @@ export default class ConnectionInfoCurrent extends React.Component<
3434
});
3535
};
3636

37+
_triggerMultipleRefreshes = (): void => {
38+
// Trigger multiple refreshes in quick succession
39+
this._refreshState();
40+
this._refreshState();
41+
this._refreshState();
42+
this._refreshState();
43+
};
44+
3745
render() {
3846
return (
3947
<View>
40-
<TouchableOpacity onPress={this._refreshState}>
41-
<Text style={{color: 'black'}}>{this.state.connectionInfo}</Text>
48+
<TouchableOpacity onPress={this._triggerMultipleRefreshes}>
49+
<Text>Tap to trigger multiple refreshes</Text>
4250
</TouchableOpacity>
51+
<Text style={{color: 'black'}}>{this.state.connectionInfo}</Text>
4352
</View>
4453
);
4554
}

src/index.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ const createState = (): State => {
2323
return new State(_configuration);
2424
};
2525

26+
// Track ongoing requests
27+
let isRequestInProgress = false;
28+
let requestQueue: ((state: Types.NetInfoState) => void)[] = [];
29+
2630
/**
2731
* Configures the library with the given configuration. Note that calling this will stop all
2832
* previously added listeners from being called again. It is best to call this right when your
@@ -74,7 +78,23 @@ export function refresh(): Promise<Types.NetInfoState> {
7478
if (!_state) {
7579
_state = createState();
7680
}
77-
return _state._fetchCurrentState();
81+
82+
// If a request is already in progress, return a promise that will resolve when the current request finishes
83+
if (isRequestInProgress) {
84+
return new Promise((resolve) => {
85+
requestQueue.push(resolve);
86+
});
87+
}
88+
89+
isRequestInProgress = true;
90+
91+
return _state._fetchCurrentState().then((result) => {
92+
requestQueue.forEach((resolve) => resolve(result));
93+
requestQueue = [];
94+
return result;
95+
}).finally(() => {
96+
isRequestInProgress = false;
97+
});
7898
}
7999

80100
/**
@@ -123,7 +143,8 @@ export function useNetInfo(
123143
});
124144

125145
useEffect((): (() => void) => {
126-
return addEventListener(setNetInfo);
146+
const unsubscribe = addEventListener(setNetInfo);
147+
return () => unsubscribe();
127148
}, []);
128149

129150
return netInfo;
@@ -165,7 +186,12 @@ export function useNetInfoInstance(
165186
}, [isPaused, configuration]);
166187

167188
const refresh = useCallback(() => {
168-
networkInfoManager && networkInfoManager._fetchCurrentState();
189+
if (networkInfoManager && !isRequestInProgress) {
190+
isRequestInProgress = true;
191+
networkInfoManager._fetchCurrentState().finally(() => {
192+
isRequestInProgress = false;
193+
});
194+
}
169195
}, [networkInfoManager]);
170196

171197
return {

0 commit comments

Comments
 (0)