Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
1.1 kB · 39 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940import { realpathSync } from "node:fs";import { resolve } from "node:path";
const fileMutationQueues = new Map<string, Promise<void>>();
function getMutationQueueKey(filePath: string): string { const resolvedPath = resolve(filePath); try { return realpathSync.native(resolvedPath); } catch { return resolvedPath; }}
/** * Serialize file mutation operations targeting the same file. * Operations for different files still run in parallel. */export async function withFileMutationQueue<T>(filePath: string, fn: () => Promise<T>): Promise<T> { const key = getMutationQueueKey(filePath); const currentQueue = fileMutationQueues.get(key) ?? Promise.resolve();
let releaseNext!: () => void; const nextQueue = new Promise<void>((resolveQueue) => { releaseNext = resolveQueue; }); const chainedQueue = currentQueue.then(() => nextQueue); fileMutationQueues.set(key, chainedQueue);
await currentQueue; try { return await fn(); } finally { releaseNext(); if (fileMutationQueues.get(key) === chainedQueue) { fileMutationQueues.delete(key); } }}