Skip to content

Commit 8310f8b

Browse files
committed
Use a singleton polling manager
1 parent 0fa7202 commit 8310f8b

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

src/hooks/useApi.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ type useApiHookReturnValue<T> = {
88
data?: T;
99
};
1010

11-
const pollingManager = new PollingManager();
12-
1311
function useApi<T>(
1412
apiCall: apiCallParam<T>,
15-
pollingTime?: number,
13+
pollingTime?: number
1614
): useApiHookReturnValue<T> {
1715
const [isLoading, setIsLoading] = useState<boolean>(true);
1816
const [data, setData] = useState<T>();
@@ -37,12 +35,12 @@ function useApi<T>(
3735

3836
if (pollingTime) {
3937
// Use polling manager for rate-limited polling
40-
pollingManager.addTask(
38+
PollingManager.addTask(
4139
taskId,
4240
stableApiCall,
4341
onSuccess,
4442
onError,
45-
pollingTime,
43+
pollingTime
4644
);
4745
} else {
4846
// For non-polling requests, execute immediately
@@ -58,7 +56,7 @@ function useApi<T>(
5856

5957
return () => {
6058
if (taskIdRef.current) {
61-
pollingManager.removeTask(taskIdRef.current);
59+
PollingManager.removeTask(taskIdRef.current);
6260
}
6361
};
6462
}, [stableApiCall, pollingTime]);

src/utils/pollingManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,4 @@ class PollingManager {
164164
}
165165
}
166166

167-
export default PollingManager;
167+
export default new PollingManager();

0 commit comments

Comments
 (0)