diff --git a/frontend/sync-client/src/sync-operations/document-locks.test.ts b/frontend/sync-client/src/file-operations/document-locks.test.ts similarity index 93% rename from frontend/sync-client/src/sync-operations/document-locks.test.ts rename to frontend/sync-client/src/file-operations/document-locks.test.ts index ce661d0..1b8394b 100644 --- a/frontend/sync-client/src/sync-operations/document-locks.test.ts +++ b/frontend/sync-client/src/file-operations/document-locks.test.ts @@ -1,12 +1,14 @@ +import { Logger } from "../tracing/logger"; import type { RelativePath } from "../persistence/database"; import { DocumentLocks } from "./document-locks"; describe("Document lock", () => { const testPath: RelativePath = "test/document/path"; - let locks = new DocumentLocks(); + const logger = new Logger(); + let locks = new DocumentLocks(logger); beforeEach(() => { - locks = new DocumentLocks(); + locks = new DocumentLocks(logger); }); test("should lock a document successfully", () => { diff --git a/frontend/sync-client/src/sync-operations/document-locks.ts b/frontend/sync-client/src/file-operations/document-locks.ts similarity index 84% rename from frontend/sync-client/src/sync-operations/document-locks.ts rename to frontend/sync-client/src/file-operations/document-locks.ts index e8e0eb1..3dc7ec2 100644 --- a/frontend/sync-client/src/sync-operations/document-locks.ts +++ b/frontend/sync-client/src/file-operations/document-locks.ts @@ -1,15 +1,19 @@ +import type { Logger } from "../tracing/logger"; import type { RelativePath } from "../persistence/database"; export class DocumentLocks { private readonly locked = new Set(); private readonly waiters = new Map void)[]>(); + public constructor(private readonly logger: Logger) {} + public tryLockDocument(relativePath: RelativePath): boolean { if (this.locked.has(relativePath)) { return false; } this.locked.add(relativePath); + return true; } @@ -20,6 +24,8 @@ export class DocumentLocks { return Promise.resolve(); } + this.logger.debug(`Waiting for lock on ${relativePath}`); + return new Promise((resolve) => { let waiting = this.waiters.get(relativePath); if (!waiting) { @@ -42,6 +48,7 @@ export class DocumentLocks { const nextWaiting = this.waiters.get(relativePath)?.shift(); if (nextWaiting) { + this.logger.debug(`Granted lock on ${relativePath}`); nextWaiting(); } else { this.locked.delete(relativePath);