Skip to content

feat: tolerate disconnects in agent metadata frontend #6939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions site/src/components/Resources/AgentMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,34 @@ export const AgentMetadata: FC<{
setMetadata(storybookMetadata)
return
}
const source = watchAgentMetadata(agent.id)

source.onerror = (e) => {
console.error("received error in watch stream", e)
}
source.addEventListener("data", (e) => {
const data = JSON.parse(e.data)
setMetadata(data)
})
return () => {
source.close()
let timeout: NodeJS.Timeout | undefined = undefined

const connect = (): (() => void) => {
const source = watchAgentMetadata(agent.id)

source.onerror = (e) => {
console.error("received error in watch stream", e)
setMetadata(undefined)
source.close()

timeout = setTimeout(() => {
connect()
}, 3000)
}

source.addEventListener("data", (e) => {
const data = JSON.parse(e.data)
setMetadata(data)
})
return () => {
if (timeout !== undefined) {
clearTimeout(timeout)
}
source.close()
}
}
return connect()
}, [agent.id, watchAgentMetadata, storybookMetadata])

if (metadata === undefined) {
Expand Down