diff --git a/plugin/src/sync-operations/locks.ts b/plugin/src/sync-operations/document-lock.ts similarity index 77% rename from plugin/src/sync-operations/locks.ts rename to plugin/src/sync-operations/document-lock.ts index 8a28249..a8a3b35 100644 --- a/plugin/src/sync-operations/locks.ts +++ b/plugin/src/sync-operations/document-lock.ts @@ -1,7 +1,7 @@ import type { RelativePath } from "src/database/document-metadata"; -const locked = new Set(), - waiters = new Map void)[]>(); +const locked = new Set(); +const waiters = new Map void)[]>(); export function tryLockDocument(relativePath: RelativePath): boolean { if (locked.has(relativePath)) { @@ -32,10 +32,14 @@ export async function waitForDocumentLock( export function unlockDocument(relativePath: RelativePath): void { if (!locked.has(relativePath)) { - throw new Error(`Document ${relativePath} is not locked`); + throw new Error( + `Document ${relativePath} is not locked, cannot unlock` + ); } + // Remove the first element to ensure FIFO unblocking order const nextWaiting = waiters.get(relativePath)?.shift(); + if (nextWaiting) { nextWaiting(); } else {