Repository navigation
fix(clients): sync status no longer flickers when opening running threads #13551
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
3 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,18 @@ | ||
| import { createDelayedStatus, type ShownStatus } from "@t3tools/client-runtime/delayed-status"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| /** | ||
| * Returns `value` only once it has lasted past the show delay, then holds it | ||
| * for a minimum time, so a short status never flashes. `key` is what the | ||
| * status belongs to (for example a thread). A new key drops it at once. | ||
| * Web has the same hook. | ||
| */ | ||
| export function useDelayedStatus<A>(key: string, value: A | null): A | null { | ||
| const [shown, setShown] = useState<ShownStatus<A> | null>(null); | ||
| const [status] = useState(() => createDelayedStatus<A>(setShown)); | ||
| useEffect(() => () => status.dispose(), [status]); | ||
| useEffect(() => { | ||
| status.update(key, value); | ||
| }, [status, key, value]); | ||
| return shown?.key === key ? shown.value : null; | ||
| } |
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,18 @@ | ||
| import { createDelayedStatus, type ShownStatus } from "@t3tools/client-runtime/delayed-status"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| /** | ||
| * Returns `value` only once it has lasted past the show delay, then holds it | ||
| * for a minimum time, so a short status never flashes. `key` is what the | ||
| * status belongs to (for example a thread). A new key drops it at once. | ||
| * Mobile has the same hook. | ||
| */ | ||
| export function useDelayedStatus<A>(key: string, value: A | null): A | null { | ||
| const [shown, setShown] = useState<ShownStatus<A> | null>(null); | ||
| const [status] = useState(() => createDelayedStatus<A>(setShown)); | ||
| useEffect(() => () => status.dispose(), [status]); | ||
| useEffect(() => { | ||
| status.update(key, value); | ||
| }, [status, key, value]); | ||
| return shown?.key === key ? shown.value : null; | ||
| } |
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 { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import { | ||
| createDelayedStatus, | ||
| STATUS_MIN_VISIBLE_MS, | ||
| STATUS_SHOW_DELAY_MS, | ||
| type ShownStatus, | ||
| } from "./delayedStatus.ts"; | ||
|
|
||
| function track() { | ||
| const changes: Array<ShownStatus<string> | null> = []; | ||
| const status = createDelayedStatus<string>((shown) => changes.push(shown)); | ||
| return { changes, status }; | ||
| } | ||
|
|
||
| describe("createDelayedStatus", () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| }); | ||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("never shows a status that clears before the show delay", () => { | ||
| const { changes, status } = track(); | ||
| status.update("a", "syncing"); | ||
| vi.advanceTimersByTime(STATUS_SHOW_DELAY_MS - 1); | ||
| status.update("a", null); | ||
| vi.runAllTimers(); | ||
|
|
||
| expect(changes).toEqual([]); | ||
| }); | ||
|
|
||
| it("holds each shown status for the minimum time, then hides it at once", () => { | ||
| const { changes, status } = track(); | ||
| status.update("a", "loading"); | ||
| vi.advanceTimersByTime(STATUS_SHOW_DELAY_MS); | ||
| expect(changes).toEqual([{ key: "a", value: "loading" }]); | ||
|
|
||
| // A new label gets its own full hold, even during the first label's hold. | ||
| vi.advanceTimersByTime(1); | ||
| status.update("a", "syncing"); | ||
| status.update("a", null); | ||
| vi.advanceTimersByTime(STATUS_MIN_VISIBLE_MS - 1); | ||
| expect(changes.at(-1)).toEqual({ key: "a", value: "syncing" }); | ||
| vi.advanceTimersByTime(1); | ||
| expect(changes.at(-1)).toBeNull(); | ||
|
|
||
| status.update("a", "syncing"); | ||
| vi.advanceTimersByTime(STATUS_SHOW_DELAY_MS + STATUS_MIN_VISIBLE_MS); | ||
| status.update("a", null); | ||
| expect(changes.at(-1)).toBeNull(); | ||
| }); | ||
|
|
||
| it("drops the shown status at once when the key changes", () => { | ||
| const { changes, status } = track(); | ||
| status.update("a", "syncing"); | ||
| vi.advanceTimersByTime(STATUS_SHOW_DELAY_MS); | ||
| status.update("b", "syncing"); | ||
| expect(changes.at(-1)).toBeNull(); | ||
|
|
||
| vi.advanceTimersByTime(STATUS_SHOW_DELAY_MS); | ||
| expect(changes.at(-1)).toEqual({ key: "b", value: "syncing" }); | ||
| }); | ||
| }); |
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,86 @@ | ||
| // @effect-diagnostics globalTimers:off - Display timing for React hooks, outside an Effect runtime. | ||
|
|
||
| /** How long a status must last before a client shows it. */ | ||
| export const STATUS_SHOW_DELAY_MS = 400; | ||
| /** How long a shown status stays up, so it cannot flash at the show delay. */ | ||
| export const STATUS_MIN_VISIBLE_MS = 400; | ||
|
|
||
| /** The status a client should show, and the key it belongs to. */ | ||
| export interface ShownStatus<A> { | ||
| readonly key: string; | ||
| readonly value: A; | ||
| } | ||
|
|
||
| export interface DelayedStatus<A> { | ||
| /** Reports the real status for `key`. A new key drops the shown status at once. */ | ||
| readonly update: (key: string, value: A | null) => void; | ||
| /** Cancels pending timers. A later `update` starts again from the real status. */ | ||
| readonly dispose: () => void; | ||
| } | ||
|
|
||
| /** | ||
| * Turns a real status (for example the thread sync phase) into the status a | ||
| * client shows. A status that clears within `STATUS_SHOW_DELAY_MS` is never | ||
| * shown. A shown status stays for at least `STATUS_MIN_VISIBLE_MS`. Values | ||
| * compare by identity, so use strings or other stable values. | ||
| * | ||
| * Web and mobile wrap this in a small `useDelayedStatus` hook. | ||
| */ | ||
| export function createDelayedStatus<A>( | ||
| onChange: (shown: ShownStatus<A> | null) => void, | ||
| ): DelayedStatus<A> { | ||
| let key = ""; | ||
| let latest: A | null = null; | ||
| let shown: A | null = null; | ||
| // While hidden, this is the show delay. While shown, the minimum visible time. | ||
| let timer: ReturnType<typeof setTimeout> | undefined; | ||
|
|
||
| const clearTimer = () => { | ||
| clearTimeout(timer); | ||
| timer = undefined; | ||
| }; | ||
| const hide = () => { | ||
| shown = null; | ||
| onChange(null); | ||
| }; | ||
| // Every shown value, including a new label, gets the full minimum visible time. | ||
| const show = (value: A) => { | ||
| shown = value; | ||
| onChange({ key, value }); | ||
| clearTimer(); | ||
| timer = setTimeout(() => { | ||
| timer = undefined; | ||
| if (latest === null) hide(); | ||
| }, STATUS_MIN_VISIBLE_MS); | ||
| }; | ||
|
|
||
| return { | ||
| update: (nextKey, value) => { | ||
| if (nextKey !== key) { | ||
| key = nextKey; | ||
| clearTimer(); | ||
| if (shown !== null) hide(); | ||
| } | ||
| latest = value; | ||
|
|
||
| if (shown === null) { | ||
| if (value === null) { | ||
| clearTimer(); | ||
| } else if (timer === undefined) { | ||
| timer = setTimeout(() => { | ||
| timer = undefined; | ||
| if (latest !== null) show(latest); | ||
| }, STATUS_SHOW_DELAY_MS); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (value !== null) { | ||
| if (value !== shown) show(value); | ||
| } else if (timer === undefined) { | ||
| hide(); | ||
| } | ||
| }, | ||
| dispose: clearTimer, | ||
| }; | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.