refactor(plugin-sdk): dedupe file lock release

This commit is contained in:
Peter Steinberger
2026-02-15 13:11:25 +00:00
parent d7079b5578
commit d80ccdb9e0

View File

@@ -107,18 +107,7 @@ export type FileLockHandle = {
release: () => Promise<void>;
};
export async function acquireFileLock(
filePath: string,
options: FileLockOptions,
): Promise<FileLockHandle> {
const normalizedFile = await resolveNormalizedFilePath(filePath);
const lockPath = `${normalizedFile}.lock`;
const held = HELD_LOCKS.get(normalizedFile);
if (held) {
held.count += 1;
return {
lockPath,
release: async () => {
async function releaseHeldLock(normalizedFile: string): Promise<void> {
const current = HELD_LOCKS.get(normalizedFile);
if (!current) {
return;
@@ -130,7 +119,20 @@ export async function acquireFileLock(
HELD_LOCKS.delete(normalizedFile);
await current.handle.close().catch(() => undefined);
await fs.rm(current.lockPath, { force: true }).catch(() => undefined);
},
}
export async function acquireFileLock(
filePath: string,
options: FileLockOptions,
): Promise<FileLockHandle> {
const normalizedFile = await resolveNormalizedFilePath(filePath);
const lockPath = `${normalizedFile}.lock`;
const held = HELD_LOCKS.get(normalizedFile);
if (held) {
held.count += 1;
return {
lockPath,
release: () => releaseHeldLock(normalizedFile),
};
}
@@ -145,19 +147,7 @@ export async function acquireFileLock(
HELD_LOCKS.set(normalizedFile, { count: 1, handle, lockPath });
return {
lockPath,
release: async () => {
const current = HELD_LOCKS.get(normalizedFile);
if (!current) {
return;
}
current.count -= 1;
if (current.count > 0) {
return;
}
HELD_LOCKS.delete(normalizedFile);
await current.handle.close().catch(() => undefined);
await fs.rm(current.lockPath, { force: true }).catch(() => undefined);
},
release: () => releaseHeldLock(normalizedFile),
};
} catch (err) {
const code = (err as { code?: string }).code;