Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

---

## [2.0.0] - 2025-11-17

### Changed
- `lock(name).request()` now returns `Promise<ReleasableLock | null>` instead of `Promise<ReleasableLock | NotHaveLock>`.
- When a lock cannot be acquired (e.g., `ifAvailable: true`), `null` is returned instead of a dummy object.
- Ensures compatibility with `await using` and AsyncDisposable patterns.

---

## [1.0.1] - 2025-11-14

### Fixed
Expand All @@ -28,5 +37,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

---

[2.0.0]: https://github.com/juner/disposable-lock/releases/tag/v2.0.0
[1.0.1]: https://github.com/juner/disposable-lock/releases/tag/v1.0.1
[1.0.0]: https://github.com/juner/disposable-lock/releases/tag/v1.0.0
21 changes: 8 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function main() {
// Request a lock
const acquired = await request({ mode: "exclusive" });

if (acquired.name) {
if (acquired) {
console.log(`✅ Lock acquired: ${acquired.name}`);

// Do something critical
Expand All @@ -62,7 +62,12 @@ async function autoRelease() {
// Automatically releases the lock when the block ends
await using acquired = await cacheLock.request();

console.log("Inside critical section...");
if (acquired) {
// Do something critical
await doSomething();
} else {
console.warn("⚠️ Lock was not available");
}
}
```

Expand All @@ -76,7 +81,7 @@ Returns an object with:

| Method | Description
| - | -
| `request(options?: LockOptions)` | Request a lock. Returns a `ReleasableLock` or a `NotHaveLock`.
| `request(options?: LockOptions)` | Request a lock. Returns a `ReleasableLock` or a null.
| `query()` | Query the current state (held and pending locks) for this name.

Throws if `navigator.locks` is unavailable and no custom `LockManager` is provided.
Expand All @@ -94,16 +99,6 @@ Represents a successfully acquired lock.
| `release()` | `() => Promise<boolean>` | Releases the lock
| `[Symbol.asyncDispose]()` | `() => Promise<void>` | Enables await using syntax

### `NotHaveLock`

Returned when the lock cannot be obtained (for example, when using `{ ifAvailable: true }`).

| Property / Method | Type | Description
| `name` | `undefined` | —
| `mode` | `undefined` | —
| `release()` | `() => Promise<false>` | No-op release
| `[Symbol.asyncDispose]()` | `() => Promise<void>` | No-op

## Querying lock state

```ts
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "disposable-lock",
"version": "1.0.1",
"version": "2.0.0",
"description": "A disposable lock controller that wraps navigator.locks",
"type": "module",
"main": "./index.js",
Expand Down
Loading