Skip to content

Commit 69fb49d

Browse files
committed
fix(linux): settle the two open CodeRabbit threads
- The pause test bounded the post-resume frame by one slot past the clock read before the pause, so a resume or encode slower than a slot failed it even with the pause excluded. It now bounds the frame by the active clock measured after it, and checks separately that the clock left the pause out. - A toast replaced under the same id with identical content kept its running timer, and the toast it replaced dismissed it early; sonner gives a replacement its full duration. Each toast now carries a revision, new whenever sonner publishes it again (useSonner builds a new object per publish), and the panel restarts the timer on it.
1 parent b9ab7e6 commit 69fb49d

5 files changed

Lines changed: 107 additions & 15 deletions

File tree

‎electron/native/pipewire-capture/src/capture.rs‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -995,10 +995,20 @@ mod tests {
995995
capture.stage(&frame_at(Instant::now())).expect("stage");
996996
assert_eq!(capture.advance().expect("advance"), 1);
997997
let stamped = capture.next_index - 1;
998+
// Bounded by the active time actually measured across the pause, not by a
999+
// fixed slot: a slow resume or encode is active time, and may advance it.
1000+
// The one slot of slack is the original bound's: an arrival rounds into the
1001+
// slot the clock is about to read.
1002+
let after = capture.current_index();
1003+
assert!(
1004+
stamped <= after + 1,
1005+
"the frame after the pause was stamped at slot {stamped}, past the active clock's {after} + 1"
1006+
);
1007+
// And that clock left the pause out: counting it would put it 9 slots on.
9981008
assert!(
999-
stamped <= before_pause + 1,
1000-
"the frame after the pause was stamped at slot {stamped}, past {} active",
1001-
before_pause + 1
1009+
after < before_pause + 9,
1010+
"the active clock read {after} after the pause, {} slots past {before_pause}: the pause was counted",
1011+
after - before_pause
10021012
);
10031013
let _ = std::fs::remove_file(&output);
10041014
}

‎src/components/launch/HudPanelWindow.test.tsx‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe("HudPanelWindow", () => {
137137
act(() =>
138138
pushState?.({
139139
...EMPTY_HUD_PANEL_STATE,
140-
toasts: [{ id: 1, type: "default", title: "Saved" }],
140+
toasts: [{ id: 1, type: "default", title: "Saved", revision: 1 }],
141141
}),
142142
);
143143

@@ -160,7 +160,9 @@ describe("HudPanelWindow", () => {
160160
vi.useFakeTimers();
161161
renderPanel({
162162
...EMPTY_HUD_PANEL_STATE,
163-
toasts: [{ id: "fail", type: "error", title: "Recording failed", duration: 5000 }],
163+
toasts: [
164+
{ id: "fail", type: "error", title: "Recording failed", revision: 1, duration: 5000 },
165+
],
164166
});
165167
expect(screen.getByRole("alert")).toHaveTextContent("Recording failed");
166168

@@ -174,7 +176,7 @@ describe("HudPanelWindow", () => {
174176
vi.useFakeTimers();
175177
renderPanel({
176178
...EMPTY_HUD_PANEL_STATE,
177-
toasts: [{ id: "saved", type: "success", title: "Saved" }],
179+
toasts: [{ id: "saved", type: "success", title: "Saved", revision: 1 }],
178180
});
179181

180182
act(() => vi.advanceTimersByTime(2999));
@@ -185,7 +187,13 @@ describe("HudPanelWindow", () => {
185187

186188
it("restarts the timer of a toast replaced under the same id", () => {
187189
vi.useFakeTimers();
188-
const toast = { id: "upload", type: "default" as const, title: "Uploading", duration: 3000 };
190+
const toast = {
191+
id: "upload",
192+
type: "default" as const,
193+
title: "Uploading",
194+
revision: 1,
195+
duration: 3000,
196+
};
189197
renderPanel({ ...EMPTY_HUD_PANEL_STATE, toasts: [toast] });
190198

191199
act(() => vi.advanceTimersByTime(2000));
@@ -196,8 +204,29 @@ describe("HudPanelWindow", () => {
196204
expect(sentActions).toEqual([{ kind: "toast-dismiss", id: "upload" }]);
197205
});
198206

207+
// sonner restarts a same-id replacement's full duration even when nothing about it changed;
208+
// the toast it replaced must not dismiss it with the time it had left.
209+
it("restarts the timer of a same-id toast replaced with identical content", () => {
210+
vi.useFakeTimers();
211+
const toast = {
212+
id: "copy",
213+
type: "default" as const,
214+
title: "Copied",
215+
duration: 3000,
216+
revision: 1,
217+
};
218+
renderPanel({ ...EMPTY_HUD_PANEL_STATE, toasts: [toast] });
219+
220+
act(() => vi.advanceTimersByTime(2000));
221+
act(() => pushState?.({ ...EMPTY_HUD_PANEL_STATE, toasts: [{ ...toast, revision: 2 }] }));
222+
act(() => vi.advanceTimersByTime(2000));
223+
expect(sentActions).toEqual([]);
224+
act(() => vi.advanceTimersByTime(1000));
225+
expect(sentActions).toEqual([{ kind: "toast-dismiss", id: "copy" }]);
226+
});
227+
199228
it("names the dismiss button with the label the HUD translated, and follows a change", () => {
200-
const toasts = [{ id: "note", type: "default" as const, title: "Heads up" }];
229+
const toasts = [{ id: "note", type: "default" as const, title: "Heads up", revision: 1 }];
201230
renderPanel({ ...EMPTY_HUD_PANEL_STATE, toasts, toastCloseLabel: "Close notification" });
202231
expect(screen.getByRole("button", { name: "Close notification" })).toBeInTheDocument();
203232

@@ -213,7 +242,7 @@ describe("HudPanelWindow", () => {
213242
vi.useFakeTimers();
214243
renderPanel({
215244
...EMPTY_HUD_PANEL_STATE,
216-
toasts: [{ id: "save", type: "loading", title: "Saving…" }],
245+
toasts: [{ id: "save", type: "loading", title: "Saving…", revision: 1 }],
217246
});
218247
act(() => vi.advanceTimersByTime(60_000));
219248
expect(sentActions).toEqual([]);
@@ -222,7 +251,15 @@ describe("HudPanelWindow", () => {
222251
it("sends a toast's action back to the HUD", () => {
223252
renderPanel({
224253
...EMPTY_HUD_PANEL_STATE,
225-
toasts: [{ id: "perm", type: "warning", title: "Needs a permission", actionLabel: "Open" }],
254+
toasts: [
255+
{
256+
id: "perm",
257+
type: "warning",
258+
title: "Needs a permission",
259+
revision: 1,
260+
actionLabel: "Open",
261+
},
262+
],
226263
});
227264
fireEvent.click(screen.getByRole("button", { name: "Open" }));
228265
expect(sentActions).toEqual([{ kind: "toast-action", id: "perm" }]);

‎src/components/launch/HudPanelWindow.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ const noop = () => undefined;
2828
* since the HUD mounts no toaster on Linux to run one, and reports the dismissal back.
2929
*/
3030
function HudPanelToastCard({ toast, closeLabel }: { toast: HudPanelToast; closeLabel: string }) {
31-
const { id, duration, type } = toast;
31+
const { id, duration, type, revision } = toast;
32+
// `revision`: a replacement with the same id and content gets its full duration again, as
33+
// sonner gives it, rather than what the toast it replaced had left.
34+
// biome-ignore lint/correctness/useExhaustiveDependencies: `revision` is the trigger, not an input.
3235
useEffect(() => {
3336
if (type === "loading") return;
3437
const ms = duration ?? TOAST_DURATION_MS;
3538
if (!Number.isFinite(ms)) return;
3639
const timer = window.setTimeout(() => send({ kind: "toast-dismiss", id }), ms);
3740
return () => window.clearTimeout(timer);
38-
}, [id, duration, type]);
41+
}, [id, duration, type, revision]);
3942

4043
const accent =
4144
type === "error"

‎src/components/launch/hudPanel.test.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("hudPanelHasContent", () => {
88
expect(
99
hudPanelHasContent({
1010
...EMPTY_HUD_PANEL_STATE,
11-
toasts: [{ id: 1, type: "default", title: "Saved" }],
11+
toasts: [{ id: 1, type: "default", title: "Saved", revision: 1 }],
1212
}),
1313
).toBe(true);
1414
expect(
@@ -40,6 +40,7 @@ describe("toHudPanelToasts", () => {
4040
description: "Open the settings",
4141
duration: 10_000,
4242
actionLabel: "Open",
43+
revision: expect.any(Number),
4344
},
4445
]);
4546
});
@@ -49,11 +50,29 @@ describe("toHudPanelToasts", () => {
4950
{ id: 1, type: "normal", title: "Plain" },
5051
{ id: 2, type: "error", title: "Gone", delete: true },
5152
];
52-
expect(toHudPanelToasts(toasts)).toEqual([{ id: 1, type: "default", title: "Plain" }]);
53+
expect(toHudPanelToasts(toasts)).toEqual([
54+
{ id: 1, type: "default", title: "Plain", revision: expect.any(Number) },
55+
]);
5356
});
5457

5558
it("drops markup it cannot send between windows rather than sending an object", () => {
5659
const toasts: ToastT[] = [{ id: 1, title: { type: "b" } as unknown as ToastT["title"] }];
57-
expect(toHudPanelToasts(toasts)).toEqual([{ id: 1, type: "default", title: "" }]);
60+
expect(toHudPanelToasts(toasts)).toEqual([
61+
{ id: 1, type: "default", title: "", revision: expect.any(Number) },
62+
]);
63+
});
64+
65+
// useSonner builds a new object for a toast each time it is published, same-id replacement
66+
// included, and keeps every other toast's object: the revision follows the object.
67+
it("gives a republished toast a new revision and an untouched one the same", () => {
68+
const first: ToastT = { id: "copy", type: "default", title: "Copied" };
69+
const other: ToastT = { id: "saved", type: "success", title: "Saved" };
70+
const [a, b] = toHudPanelToasts([first, other]);
71+
const [aAgain, bAgain] = toHudPanelToasts([first, other]);
72+
expect(aAgain.revision).toBe(a.revision);
73+
expect(bAgain.revision).toBe(b.revision);
74+
75+
const [replaced] = toHudPanelToasts([{ ...first }, other]);
76+
expect(replaced.revision).not.toBe(a.revision);
5877
});
5978
});

‎src/components/launch/hudPanel.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ export interface HudPanelToast {
6262
description?: string;
6363
actionLabel?: string;
6464
duration?: number;
65+
/**
66+
* Changes each time sonner publishes this toast again, even with the same id and content,
67+
* so the panel restarts its timer as sonner does. See `toHudPanelToasts`.
68+
*/
69+
revision: number;
6570
}
6671

6772
export interface HudPanelState {
@@ -113,6 +118,23 @@ function toastText(value: ToastT["title"]): string | undefined {
113118
: undefined;
114119
}
115120

121+
/**
122+
* A revision per toast object. `useSonner` builds a new object each time a toast is published,
123+
* a same-id replacement included, and leaves every other toast's object as it was: a new
124+
* object is the one sign of a replacement that repeats the id, the content and the duration.
125+
*/
126+
const toastRevisions = new WeakMap<ToastT, number>();
127+
let nextToastRevision = 1;
128+
129+
function toastRevision(toast: ToastT): number {
130+
let revision = toastRevisions.get(toast);
131+
if (revision === undefined) {
132+
revision = nextToastRevision++;
133+
toastRevisions.set(toast, revision);
134+
}
135+
return revision;
136+
}
137+
116138
/**
117139
* The HUD's live toasts (sonner's `useSonner`) as plain data. Dismissed ones are left out;
118140
* an action is carried as its label, and the click comes back as a `toast-action`.
@@ -136,6 +158,7 @@ export function toHudPanelToasts(toasts: readonly ToastT[]): HudPanelToast[] {
136158
...(description ? { description } : {}),
137159
...(action ? { actionLabel: action } : {}),
138160
...(toast.duration !== undefined ? { duration: toast.duration } : {}),
161+
revision: toastRevision(toast),
139162
};
140163
});
141164
}

0 commit comments

Comments
 (0)