Skip to content

Add isLocked() to Storage and EventStore#312

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/add-method-check-storage-lock
Draft

Add isLocked() to Storage and EventStore#312
Copilot wants to merge 2 commits into
mainfrom
copilot/add-method-check-storage-lock

Conversation

Copilot AI commented May 10, 2026

Copy link
Copy Markdown
Contributor

There was no public API for clients to check whether a storage is currently locked by a writer — particularly useful for read-only consumers who want to know if a writer is active without attempting to acquire the lock themselves.

Changes

  • ReadableStorage: moves lockFile path computation into the base class constructor (was only in WritableStorage); adds isLocked() returning fs.existsSync(this.lockFile).
  • WritableStorage: removes now-redundant this.lockFile assignment (inherited); lock() relies solely on mkdirSync failing with EEXIST to detect an existing lock — no redundant pre-check.
  • EventStore: adds isLocked() delegating to this.storage.isLocked().

Usage

const writer = new EventStore({ storageDirectory });
const reader = new EventStore({ storageDirectory, readOnly: true });

reader.isLocked(); // true — writer is holding the lock
writer.close();
reader.isLocked(); // false

Likewise available directly on Storage and ReadOnlyStorage:

const reader = new Storage.ReadOnly({ dataDirectory });
reader.open();
reader.isLocked(); // true if a WritableStorage has the lock, false otherwise

@albe albe left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot address the single comment and rebase on main

Comment on lines +228 to +230
if (this.isLocked()) {
throw new StorageLockedError(`Storage ${this.storageFile} is locked by another process`);
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot This is unneccessary, as the mkdirSync will fail if the lock already exists at that point.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the redundant isLocked() pre-check from lock() — the mkdirSync call already throws EEXIST atomically if the lock exists. Also updated the catch block to use the c8 ignore + assert style that's consistent with the rest of main.

Copilot AI commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

@copilot address the single comment and rebase on main

Done — removed the redundant check and rebased onto main (resolved conflicts with makeReadOnly() in EventStore.js and the updated assert/c8-ignore style in WritableStorage.js).

Copilot AI requested a review from albe June 9, 2026 13:22
Copilot finished work on behalf of albe June 9, 2026 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants