Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite5a152b

Browse files
Emyrkpull[bot]
authored andcommitted
chore: add function to refetch latencies to ProxyContext (#7769)
* Allow refetching of proxy latencies* Pass refetch funtion up the context stack* Add to menu bar
1 parente6bb041 commite5a152b

File tree

11 files changed

+52
-12
lines changed

11 files changed

+52
-12
lines changed

‎site/jest.setup.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jest.mock("contexts/useProxyLatency", () => ({
1919
useProxyLatency:(proxies?:RegionsResponse)=>{
2020
// Must use `useMemo` here to avoid infinite loop.
2121
// Mocking the hook with a hook.
22-
constlatencies=useMemo(()=>{
22+
constproxyLatencies=useMemo(()=>{
2323
if(!proxies){
2424
return{}asRecord<string,ProxyLatencyReport>
2525
}
@@ -35,7 +35,7 @@ jest.mock("contexts/useProxyLatency", () => ({
3535
},{}asRecord<string,ProxyLatencyReport>)
3636
},[proxies])
3737

38-
returnlatencies
38+
return{ proxyLatencies,refetch:jest.fn()}
3939
},
4040
}))
4141

‎site/src/components/AppLink/AppLink.stories.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const Template: Story<AppLinkProps> = (args) => (
2929
clearProxy:()=>{
3030
return
3131
},
32+
refetchProxyLatencies:()=>{
33+
return
34+
},
3235
}}
3336
>
3437
<AppLink{...args}/>

‎site/src/components/Navbar/NavbarView.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const proxyContextValue: ProxyContextValue = {
1919
isFetched:true,
2020
setProxy:jest.fn(),
2121
clearProxy:action("clearProxy"),
22+
refetchProxyLatencies:jest.fn(),
2223
proxyLatencies:{},
2324
}
2425

‎site/src/components/Navbar/NavbarView.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
189189
constbuttonRef=useRef<HTMLButtonElement>(null)
190190
const[isOpen,setIsOpen]=useState(false)
191191
constselectedProxy=proxyContextValue.proxy.proxy
192+
constrefreshLatencies=proxyContextValue.refetchProxyLatencies
192193
constcloseMenu=()=>setIsOpen(false)
193194
constnavigate=useNavigate()
194195

@@ -293,6 +294,9 @@ const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
293294
>
294295
Proxy settings
295296
</MenuItem>
297+
<MenuItemsx={{fontSize:14}}onClick={refreshLatencies}>
298+
Refresh Latencies
299+
</MenuItem>
296300
</Menu>
297301
</>
298302
)

‎site/src/components/Resources/AgentRow.stories.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ const TemplateFC = (
6868
clearProxy:()=>{
6969
return
7070
},
71+
refetchProxyLatencies:()=>{
72+
return
73+
},
7174
}}
7275
>
7376
<AgentRow{...args}/>

‎site/src/components/Resources/ResourceCard.stories.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Example.args = {
3333
clearProxy:()=>{
3434
return
3535
},
36+
refetchProxyLatencies:()=>{
37+
return
38+
},
3639
}}
3740
>
3841
<AgentRow
@@ -103,6 +106,9 @@ BunchOfMetadata.args = {
103106
clearProxy:()=>{
104107
return
105108
},
109+
refetchProxyLatencies:()=>{
110+
return
111+
},
106112
}}
107113
>
108114
<AgentRow

‎site/src/components/Workspace/Workspace.stories.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const Template: Story<WorkspaceProps> = (args) => (
5353
setProxy:()=>{
5454
return
5555
},
56+
refetchProxyLatencies:()=>{
57+
return
58+
},
5659
}}
5760
>
5861
<Workspace{...args}/>

‎site/src/contexts/ProxyContext.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import userEvent from "@testing-library/user-event"
2626
// here and not inside a unit test.
2727
jest.mock("contexts/useProxyLatency",()=>({
2828
useProxyLatency:()=>{
29-
returnhardCodedLatencies
29+
return{proxyLatencies:hardCodedLatencies,refetch:jest.fn()}
3030
},
3131
}))
3232

‎site/src/contexts/ProxyContext.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export interface ProxyContextValue {
4545
// then the latency has not been fetched yet. Calculations happen async for each proxy in the list.
4646
// Refer to the returned report for a given proxy for more information.
4747
proxyLatencies:Record<string,ProxyLatencyReport>
48+
// refetchProxyLatencies will trigger refreshing of the proxy latencies. By default the latencies
49+
// are loaded once.
50+
refetchProxyLatencies:()=>void
4851
// setProxy is a function that sets the user's selected proxy. This function should
4952
// only be called if the user is manually selecting a proxy. This value is stored in local
5053
// storage and will persist across reloads and tabs.
@@ -101,7 +104,8 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
101104

102105
// Every time we get a new proxiesResponse, update the latency check
103106
// to each workspace proxy.
104-
constproxyLatencies=useProxyLatency(proxiesResp)
107+
const{ proxyLatencies,refetch:refetchProxyLatencies}=
108+
useProxyLatency(proxiesResp)
105109

106110
// updateProxy is a helper function that when called will
107111
// update the proxy being used.
@@ -128,6 +132,7 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
128132
<ProxyContext.Provider
129133
value={{
130134
proxyLatencies,
135+
refetchProxyLatencies,
131136
userProxy:userSavedProxy,
132137
proxy:experimentEnabled
133138
?proxy

‎site/src/contexts/useProxyLatency.ts‎

Lines changed: 22 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
importPerformanceObserverfrom"@fastly/performance-observer-polyfill"
44
importaxiosfrom"axios"
55
import{generateRandomString}from"utils/random"
@@ -25,20 +25,31 @@ 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+
returnstate
3230
}
3331

3432
exportconstuseProxyLatency=(
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 latestFetchRequest is used to trigger a refetch of the proxy latencies.
46+
const[latestFetchRequest,setLatestFetchRequest]=useState(
47+
newDate().toISOString(),
48+
)
49+
constrefetch=()=>{
50+
setLatestFetchRequest(newDate().toISOString())
51+
}
52+
4253
// Only run latency updates when the proxies change.
4354
useEffect(()=>{
4455
if(!proxies){
@@ -148,7 +159,10 @@ export const useProxyLatency = (
148159
// via the performance observer. So we can disconnect the observer.
149160
observer.disconnect()
150161
})
151-
},[proxies])
162+
},[proxies,latestFetchRequest])
152163

153-
returnproxyLatencies
164+
return{
165+
proxyLatencies,
166+
refetch,
167+
}
154168
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp