Skip to content
TypeScript

fix(web): threads finishing together play one completion sound (#17540)

lukec1f9d2026365
diff --git a/apps/web/src/threadNotifications.test.ts b/apps/web/src/threadNotifications.test.ts
new file mode 100644
index 00000000000..0da83eb281e
--- /dev/null
+++ b/apps/web/src/threadNotifications.test.ts
@@ -0,0 +1,100 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
+
+const SOUND_SECONDS = 0.6;
+
+const audio = { currentTime: 0, started: 0 };
+
+class FakeAudioContext {
+ state = "running";
+ destination = {};
+ get currentTime() {
+ return audio.currentTime;
+ }
+ resume() {
+ return Promise.resolve();
+ }
+ decodeAudioData() {
+ return Promise.resolve({ duration: SOUND_SECONDS });
+ }
+ createBufferSource() {
+ return {
+ buffer: null,
+ connect() {},
+ start() {
+ audio.started += 1;
+ },
+ };
+ }
+}
+
+async function loadUnlocked() {
+ const notifications = await import("./threadNotifications");
+ notifications.unlockNotificationAudio();
+ return notifications;
+}
+
+beforeEach(() => {
+ vi.resetModules();
+ audio.currentTime = 0;
+ audio.started = 0;
+ vi.stubGlobal("AudioContext", FakeAudioContext);
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => ({ arrayBuffer: async () => new ArrayBuffer(0) })),
+ );
+});
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+});
+
+describe("playNotificationSound", () => {
+ it("plays one sound for threads that complete together", async () => {
+ const { playNotificationSound } = await loadUnlocked();
+
+ // All four are waiting on the first decode when the sound becomes playable.
+ await Promise.all(
+ Array.from({ length: 4 }, () => playNotificationSound("completion", () => true)),
+ );
+
+ expect(audio.started).toBe(1);
+ });
+
+ it("skips a completion that lands while the sound is still playing", async () => {
+ const { playNotificationSound } = await loadUnlocked();
+ await playNotificationSound("completion", () => true);
+
+ audio.currentTime = SOUND_SECONDS / 2;
+ await playNotificationSound("completion", () => true);
+
+ expect(audio.started).toBe(1);
+ });
+
+ it("plays again once the previous sound has ended", async () => {
+ const { playNotificationSound } = await loadUnlocked();
+ await playNotificationSound("completion", () => true);
+
+ audio.currentTime = SOUND_SECONDS;
+ await playNotificationSound("completion", () => true);
+
+ expect(audio.started).toBe(2);
+ });
+
+ it("still plays an input alert over a completion sound", async () => {
+ const { playNotificationSound } = await loadUnlocked();
+ await playNotificationSound("completion", () => true);
+
+ await playNotificationSound("input", () => true);
+
+ expect(audio.started).toBe(2);
+ });
+
+ it("leaves the slot free when a sound is turned off before it plays", async () => {
+ const { playNotificationSound } = await loadUnlocked();
+ await playNotificationSound("completion", () => false);
+
+ await playNotificationSound("completion", () => true);
+
+ expect(audio.started).toBe(1);
+ });
+});
diff --git a/apps/web/src/threadNotifications.ts b/apps/web/src/threadNotifications.ts
index 2f9d4549766..3453a2931e9 100644
--- a/apps/web/src/threadNotifications.ts
+++ b/apps/web/src/threadNotifications.ts
@@ -66,6 +66,8 @@ export function setNotificationBadge(count: number) {
let audioContext: AudioContext | undefined;
const buffers = new Map<string, Promise<AudioBuffer>>();
+/** When each sound's current playback ends, on the audio context's clock. */
+const playingUntil = new Map<string, number>();
/** Called from a gesture so browsers allow later background playback. */
export function unlockNotificationAudio() {
@@ -90,10 +92,16 @@ export async function playNotificationSound(
}
const decoded = await buffer;
if (!shouldPlay() || context.state !== "running") return;
+ // Threads that finish together each ask for this sound, and full-volume copies sum into
+ // clipping. One playback stands for the burst. Checked after the decode, since every caller
+ // in a burst is waiting on it. The other kind keeps its own slot, so an input alert is still
+ // heard over a completion.
+ if (context.currentTime < (playingUntil.get(url) ?? 0)) return;
const source = context.createBufferSource();
source.buffer = decoded;
source.connect(context.destination);
source.start();
+ playingUntil.set(url, context.currentTime + decoded.duration);
} catch {
buffers.delete(url);
}