-
Notifications
You must be signed in to change notification settings - Fork 10
fix(workbench): revalidate session and workspace lists on session.changed #526
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
Open
Zerlight
wants to merge
4
commits into
master
Choose a base branch
from
ruocheng/code-654
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fef197b
fix(workbench): revalidate session and workspace lists on session.cha…
Zerlight bd9f642
fix(workbench): coalesce session.changed revalidation bursts
Zerlight 68841ed
test(workbench): verify lazy keys and coalesced list refreshes
Zerlight 66e3270
fix(workbench): respect session refresh lifecycle
Zerlight 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
Some comments aren't visible on the classic Files Changed page.
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
79 changes: 79 additions & 0 deletions
79
packages/client/workbench/src/runtime/__tests__/coalesce.test.ts
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,79 @@ | ||
| import { wait } from 'foxts/wait'; | ||
| import { expect, it, vi } from 'vitest'; | ||
| import { coalesceRuns } from '../coalesce'; | ||
| import { deferred } from './connection-controller-test-helpers'; | ||
|
|
||
| it('collapses a burst arriving mid-run into a single trailing run', async () => { | ||
| const gates = [deferred(), deferred()]; | ||
| let started = 0; | ||
| const trigger = coalesceRuns(() => { | ||
| const gate = gates[started] ?? deferred(); | ||
| started += 1; | ||
| return gate.promise; | ||
| }, new AbortController().signal); | ||
|
|
||
| trigger(); | ||
| expect(started).toBe(1); | ||
|
|
||
| // Three more frames while the first run is still in flight: they must collapse into one. | ||
| trigger(); | ||
| trigger(); | ||
| trigger(); | ||
| expect(started).toBe(1); | ||
|
|
||
| gates[0].resolve(); | ||
| await vi.waitFor(() => expect(started).toBe(2)); | ||
|
|
||
| gates[1].resolve(); | ||
| await wait(0); | ||
| expect(started).toBe(2); | ||
| }); | ||
|
|
||
| it('runs again for a trigger that arrives after the previous run settled', async () => { | ||
| let started = 0; | ||
| const trigger = coalesceRuns(() => { | ||
| started += 1; | ||
| return Promise.resolve(); | ||
| }, new AbortController().signal); | ||
|
|
||
| trigger(); | ||
| await wait(0); | ||
| trigger(); | ||
| await wait(0); | ||
|
|
||
| expect(started).toBe(2); | ||
| }); | ||
|
|
||
| it('keeps draining after a failed run', async () => { | ||
| let started = 0; | ||
| const trigger = coalesceRuns(() => { | ||
| started += 1; | ||
| return started === 1 ? Promise.reject(new Error('fetch failed')) : Promise.resolve(); | ||
| }, new AbortController().signal); | ||
|
|
||
| trigger(); | ||
| trigger(); | ||
| await vi.waitFor(() => expect(started).toBe(2)); | ||
| }); | ||
|
|
||
| it.each(['resolve', 'reject'] as const)( | ||
| 'drops queued work after abort when the in-flight run settles via %s', | ||
| async (outcome) => { | ||
| const controller = new AbortController(); | ||
| const gate = deferred(); | ||
| const run = vi.fn(() => gate.promise); | ||
| const trigger = coalesceRuns(run, controller.signal); | ||
|
|
||
| trigger(); | ||
| trigger(); | ||
| expect(run).toHaveBeenCalledTimes(1); | ||
| controller.abort(); | ||
| if (outcome === 'resolve') gate.resolve(); | ||
| else gate.reject(new Error('client disposed')); | ||
| await wait(0); | ||
| expect(run).toHaveBeenCalledTimes(1); | ||
|
|
||
| trigger(); | ||
| expect(run).toHaveBeenCalledTimes(1); | ||
| }, | ||
| ); |
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,39 @@ | ||
| import { noop } from 'foxts/noop'; | ||
|
|
||
| /** | ||
| * Mid-run triggers need one trailing run; abort discards it without cancelling active work. | ||
| * The caller owns error reporting; a failed run still drains queued changes unless aborted. | ||
| */ | ||
| export function coalesceRuns(run: () => Promise<unknown>, signal: AbortSignal): () => void { | ||
| let running = false; | ||
| let queued = false; | ||
|
|
||
| // Read through a call, not `while (queued)`: the flag is only ever set from the closure below | ||
| // while a run is awaited, which narrowing cannot see. | ||
| const takeQueued = (): boolean => { | ||
| const wasQueued = queued; | ||
| queued = false; | ||
| return wasQueued; | ||
| }; | ||
|
|
||
| const drain = async (): Promise<void> => { | ||
| running = true; | ||
| try { | ||
| do { | ||
| // eslint-disable-next-line no-await-in-loop -- serializing is the point: one run at a time | ||
| await run().catch(noop); | ||
| } while (!signal.aborted && takeQueued()); | ||
| } finally { | ||
| running = false; | ||
| } | ||
| }; | ||
|
|
||
| return () => { | ||
| if (signal.aborted) return; | ||
| if (running) { | ||
| queued = true; | ||
| return; | ||
| } | ||
| void drain().catch(noop); | ||
| }; | ||
| } |
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
Oops, something went wrong.
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.