Skip to content

Commit

Permalink
Merge pull request #63 from AkatQuas/main
Browse files Browse the repository at this point in the history
clear timeout when resolving
  • Loading branch information
DirtyHairy authored Jan 17, 2024
2 parents 11be851 + 038eddf commit ae9c5b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ that are hard to fix and even harder to track down.
This library solves the problem by applying the concept of mutexes to Javascript.
Locking the mutex will return a promise that resolves once the mutex becomes
available. Once the async process is complete (usually taking multiple
spins of the event loop), a callback supplied to the caller is called in order
spins of the event loop), a callback supplied to the caller should be called in order
to release the mutex, allowing the next scheduled worker to execute.

# Semaphore
Expand All @@ -39,7 +39,7 @@ a shared resource. For example, you might want to distribute images between seve
worker processes that perform transformations, or you might want to create a web
crawler that performs a defined number of requests in parallel.

A semaphore is a data structure that is initialized to an arbitrary integer value and that
A semaphore is a data structure that is initialized with an arbitrary integer value and that
can be locked multiple times.
As long as the semaphore value is positive, locking it will return the current value
and the locking process will continue execution immediately; the semaphore will
Expand Down Expand Up @@ -107,10 +107,10 @@ Create a new mutex.
Promise style:
```typescript
mutex
.runExclusive(function() {
.runExclusive(() => {
// ...
})
.then(function(result) {
.then((result) => {
// ...
});
```
Expand Down Expand Up @@ -177,7 +177,7 @@ mutex.release();
mutex.isLocked();
```

### Cancelling pending locks.
### Cancelling pending locks

Pending locks can be cancelled by calling `cancel()` on the mutex. This will reject
all pending locks with `E_CANCELED`:
Expand Down Expand Up @@ -233,7 +233,7 @@ revoked. In consequence, the mutex may not be available even after `cancel()` ha
You can wait until the mutex is available without locking it by calling `waitForUnlock()`.
This will return a promise that resolve once the mutex can be acquired again. This operation
will not lock the mutex, and there is no guarantee that the mutex will still be available
once a async barrier has been encountered.
once an async barrier has been encountered.

Promise style:
```typescript
Expand Down Expand Up @@ -330,15 +330,15 @@ and handle exceptions accordingly.

`runExclusive` accepts an optional argument `weight`. Specifying a `weight` will decrement the
semaphore by the specified value, and the semaphore will only be acquired once the its
value is greater or equal to.
value is greater or equal to `weight`.

### Unscoped release

As an alternative to calling the `release` callback return by `acquire`, the mutex
As an alternative to calling the `release` callback returned by `acquire`, the semaphore
can be released by calling `release` directly on it:

```typescript
mutex.release();
semaphore.release();
```

`release` accepts an optional argument `weight` and increments the semaphore accordingly.
Expand All @@ -360,7 +360,7 @@ semaphore.getValue()
semaphore.isLocked();
```

The semaphore is considered to be locked if its value is either zero or negative;
The semaphore is considered to be locked if its value is either zero or negative.

### Setting the semaphore value

Expand All @@ -371,7 +371,7 @@ cause the semaphore to schedule any pending waiters accordingly.
semaphore.setValue();
```

### Cancelling pending locks.
### Cancelling pending locks

Pending locks can be cancelled by calling `cancel()` on the semaphore. This will reject
all pending locks with `E_CANCELED`:
Expand Down Expand Up @@ -427,7 +427,7 @@ revoked. In consequence, the semaphore may not be available even after `cancel()
You can wait until the semaphore is available without locking it by calling `waitForUnlock()`.
This will return a promise that resolve once the semaphore can be acquired again. This operation
will not lock the semaphore, and there is no guarantee that the semaphore will still be available
once a async barrier has been encountered.
once an async barrier has been encountered.

Promise style:
```typescript
Expand All @@ -445,7 +445,7 @@ await semaphore.waitForUnlock();
```

`waitForUnlock` accepts an optional argument `weight`. If `weight` is specified the promise
will only resolve once the semaphore's value is greater or equal to weight;
will only resolve once the semaphore's value is greater or equal to `weight`;

## Limiting the time waiting for a mutex or semaphore to become available

Expand Down Expand Up @@ -499,7 +499,6 @@ tryAcquire(semaphoreOrMutex)
// ...
}
});

```

async/await:
Expand All @@ -515,12 +514,17 @@ try {
// ...
}
}

```

Again, the error can be customized by providing a custom error as second argument to
`tryAcquire`.

```typescript
tryAcquire(semaphoreOrMutex, new Error('new fancy error'))
.runExclusive(() => {
// ...
});
```
# License

Feel free to use this library under the conditions of the MIT license.
8 changes: 5 additions & 3 deletions src/withTimeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ export function withTimeout(sync: MutexInterface | SemaphoreInterface, timeout:
}

return new Promise((resolve, reject) => {
sync.waitForUnlock(weight).then(resolve);

setTimeout(() => reject(timeoutError), timeout);
const handle = setTimeout(() => reject(timeoutError), timeout);
sync.waitForUnlock(weight).then(() => {
clearTimeout(handle);
resolve();
});
});
},

Expand Down

0 comments on commit ae9c5b2

Please sign in to comment.