Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(cursor): stage the click samples once when two callers ask together

EtienneLescot1d1be1f83245
diff --git a/src/lib/ai-edition/clickSound.test.ts b/src/lib/ai-edition/clickSound.test.ts
index 6a7c827c9..2ab987154 100644
--- a/src/lib/ai-edition/clickSound.test.ts
+++ b/src/lib/ai-edition/clickSound.test.ts
@@ -7,6 +7,7 @@ import {
clickCuesFitTake,
clickSoundForDocument,
crossedClickHits,
+ ensureClickHitPaths,
levelClickCues,
loadClickCues,
placeClickHits,
@@ -282,3 +283,22 @@ describe("playClickHits", () => {
expect(started).toEqual([]);
});
});
+
+describe("ensureClickHitPaths", () => {
+ it("stages the two samples once when two callers ask at the same time", async () => {
+ const stage = vi.fn(async (name: string) => ({ success: true, path: `/sfx/${name}` }));
+ vi.stubGlobal("window", {
+ location: { href: "http://localhost/" },
+ electronAPI: { stageClickSoundHit: stage },
+ });
+ vi.stubGlobal("fetch", async () => new Response(new Uint8Array([1])));
+ try {
+ const [a, b] = await Promise.all([ensureClickHitPaths(), ensureClickHitPaths()]);
+ expect(a).toEqual({ down: "/sfx/click-down.wav", up: "/sfx/click-up.wav" });
+ expect(b).toBe(a);
+ expect(stage).toHaveBeenCalledTimes(2);
+ } finally {
+ vi.unstubAllGlobals();
+ }
+ });
+});
diff --git a/src/lib/ai-edition/clickSound.ts b/src/lib/ai-edition/clickSound.ts
index 3e0518dca..34fbbd3f8 100644
--- a/src/lib/ai-edition/clickSound.ts
+++ b/src/lib/ai-edition/clickSound.ts
@@ -111,6 +111,7 @@ export function clickHitUrls(): { down: string; up: string } {
// the same bytes in every project.
const cueCache = new Map<string, ClickCue[]>();
let hitPaths: { down: string; up: string } | null = null;
+let hitPathsPending: Promise<{ down: string; up: string } | null> | null = null;
/**
* The take's cues, read from disk once per run. A take whose sidecar cannot be read is cached as
@@ -141,8 +142,17 @@ export async function loadClickCues(takePath: string): Promise<ClickCue[]> {
* and it writes them under userData. `null` outside Electron (the browser shim keeps the URLs,
* which is all the preview needs).
*/
-export async function ensureClickHitPaths(): Promise<{ down: string; up: string } | null> {
- if (hitPaths) return hitPaths;
+export function ensureClickHitPaths(): Promise<{ down: string; up: string } | null> {
+ if (hitPaths) return Promise.resolve(hitPaths);
+ // One staging at a time: the toggle and an export can both ask before the first one lands,
+ // and a second write of the same file could overlap the compositor reading it.
+ hitPathsPending ??= stageClickHits().finally(() => {
+ hitPathsPending = null;
+ });
+ return hitPathsPending;
+}
+
+async function stageClickHits(): Promise<{ down: string; up: string } | null> {
const stage = window.electronAPI?.stageClickSoundHit;
if (!stage) return null;
const urls = clickHitUrls();