-
Notifications
You must be signed in to change notification settings - Fork 6.6k
fix(web): disconnect offline servers from threads #11671
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { EnvironmentId } from "@t3tools/contracts"; | ||
| import { act, useLayoutEffect } from "react"; | ||
| import { create, type ReactTestRenderer } from "react-test-renderer"; | ||
| import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import { useEnvironmentDisconnectDelay } from "./useEnvironmentDisconnectDelay"; | ||
|
|
||
| const environmentId = EnvironmentId.make("remote"); | ||
| let renderer: ReactTestRenderer; | ||
| let elapsed = false; | ||
|
|
||
| function Probe({ unavailableId }: { unavailableId: EnvironmentId | null }) { | ||
| const value = useEnvironmentDisconnectDelay(unavailableId); | ||
| useLayoutEffect(() => { | ||
| elapsed = value; | ||
| }); | ||
| return null; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); | ||
| act(() => { | ||
| renderer = create(<Probe unavailableId={environmentId} />); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| act(() => renderer.unmount()); | ||
| vi.useRealTimers(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it("waits 20 seconds without restarting on renders for the same environment", () => { | ||
| act(() => vi.advanceTimersByTime(10_000)); | ||
| expect(elapsed).toBe(false); | ||
| act(() => renderer.update(<Probe unavailableId={environmentId} />)); | ||
| act(() => vi.advanceTimersByTime(9_999)); | ||
| expect(elapsed).toBe(false); | ||
| act(() => vi.advanceTimersByTime(1)); | ||
| expect(elapsed).toBe(true); | ||
| }); | ||
|
|
||
| it("cancels a brief outage and starts a fresh delay on the next outage", () => { | ||
| act(() => vi.advanceTimersByTime(10_000)); | ||
| act(() => renderer.update(<Probe unavailableId={null} />)); | ||
| act(() => vi.advanceTimersByTime(20_000)); | ||
| expect(elapsed).toBe(false); | ||
| act(() => renderer.update(<Probe unavailableId={environmentId} />)); | ||
| act(() => vi.advanceTimersByTime(19_999)); | ||
| expect(elapsed).toBe(false); | ||
| act(() => vi.advanceTimersByTime(1)); | ||
| expect(elapsed).toBe(true); | ||
| act(() => renderer.update(<Probe unavailableId={null} />)); | ||
| expect(elapsed).toBe(false); | ||
| }); | ||
|
|
||
| it("does not carry elapsed time to another environment", () => { | ||
| act(() => vi.advanceTimersByTime(20_000)); | ||
| expect(elapsed).toBe(true); | ||
| act(() => renderer.update(<Probe unavailableId={EnvironmentId.make("another-remote")} />)); | ||
| expect(elapsed).toBe(false); | ||
| act(() => vi.advanceTimersByTime(20_000)); | ||
| expect(elapsed).toBe(true); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import type { EnvironmentId } from "@t3tools/contracts"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| /** Wait through brief outages before offering to switch off the active environment. */ | ||
| export function useEnvironmentDisconnectDelay(unavailableEnvironmentId: EnvironmentId | null) { | ||
| const [delay, setDelay] = useState({ environmentId: unavailableEnvironmentId, elapsed: false }); | ||
| if (delay.environmentId !== unavailableEnvironmentId) { | ||
| setDelay({ environmentId: unavailableEnvironmentId, elapsed: false }); | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| if (unavailableEnvironmentId === null) return; | ||
| const timeout = setTimeout( | ||
| () => setDelay({ environmentId: unavailableEnvironmentId, elapsed: true }), | ||
| 20_000, | ||
| ); | ||
| return () => clearTimeout(timeout); | ||
| }, [unavailableEnvironmentId]); | ||
|
|
||
| return ( | ||
| unavailableEnvironmentId !== null && | ||
| delay.environmentId === unavailableEnvironmentId && | ||
| delay.elapsed | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Scope the pending disconnect operation to its environment and route.
ChatViewcan receive a new thread or environment route whilesetEnvironmentEnabledis pending. On success, the existing callback always navigates to/. This can remove the user from the newly selected thread. The shareddisconnectingEnvironmentstate can also disable the new environment's disconnect action.Track the pending environment ID and the initiating route key. Navigate home only when the initiating route remains active.
Proposed fix
useAtomCommandsettles command failures into anAtomCommandResult, so ordinary command failure does not bypass the state reset.🤖 Prompt for AI Agents