Skip to content

Commit 771d41f

Browse files
committed
Allow refetching of proxy latencies
1 parent 555ea64 commit 771d41f

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

site/src/contexts/ProxyContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
101101

102102
// Every time we get a new proxiesResponse, update the latency check
103103
// to each workspace proxy.
104-
const proxyLatencies = useProxyLatency(proxiesResp)
104+
const { proxyLatencies } = useProxyLatency(proxiesResp)
105105

106106
// updateProxy is a helper function that when called will
107107
// update the proxy being used.

site/src/contexts/useProxyLatency.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Region, RegionsResponse } from "api/typesGenerated"
2-
import { useEffect, useReducer } from "react"
2+
import { useEffect, useReducer, useState } from "react"
33
import PerformanceObserver from "@fastly/performance-observer-polyfill"
44
import axios from "axios"
55
import { generateRandomString } from "utils/random"
@@ -25,20 +25,29 @@ const proxyLatenciesReducer = (
2525
action: ProxyLatencyAction,
2626
): Record<string, ProxyLatencyReport> => {
2727
// Just overwrite any existing latency.
28-
return {
29-
...state,
30-
[action.proxyID]: action.report,
31-
}
28+
state[action.proxyID] = action.report
29+
return state
3230
}
3331

3432
export const useProxyLatency = (
3533
proxies?: RegionsResponse,
36-
): Record<string, ProxyLatencyReport> => {
34+
): {
35+
// Refetch can be called to refetch the proxy latencies.
36+
// Until the new values are loaded, the old values will still be used.
37+
refetch: () => void
38+
proxyLatencies: Record<string, ProxyLatencyReport>
39+
} => {
3740
const [proxyLatencies, dispatchProxyLatencies] = useReducer(
3841
proxyLatenciesReducer,
3942
{},
4043
)
4144

45+
// This fetchNumber is used to trigger a refetch of the proxy latencies.
46+
const [fetchNumber, setFetchNumber] = useState(0)
47+
const refetch = (): void => {
48+
setFetchNumber(fetchNumber + 1)
49+
}
50+
4251
// Only run latency updates when the proxies change.
4352
useEffect(() => {
4453
if (!proxies) {
@@ -148,7 +157,10 @@ export const useProxyLatency = (
148157
// via the performance observer. So we can disconnect the observer.
149158
observer.disconnect()
150159
})
151-
}, [proxies])
160+
}, [proxies, fetchNumber])
152161

153-
return proxyLatencies
162+
return {
163+
proxyLatencies,
164+
refetch,
165+
}
154166
}

0 commit comments

Comments
 (0)