API reference › @evolu/common › Task › Mutex
Defined in: packages/common/src/Task.ts:2737
A mutex (mutual exclusion) that ensures only one Task runs at a time.
This is a specialized version of a Semaphore with a permit count of 1.
Example
await using run = createRun();
const mutex = createMutex();
const task =
(id: string): Task<void> =>
async (run) => {
const { console } = run.deps;
console.log("start", id);
await run(sleep("10ms"));
console.log("end", id);
return ok();
};
await Promise.all([
run(mutex.withLock(task("1"))),
run(mutex.withLock(task("2"))),
]);
// start 1
// end 1
// start 2
// end 2
Extends
Methods
[dispose]()
dispose: void;
Defined in: node_modules/.bun/typescript@5.9.3/node_modules/typescript/lib/lib.esnext.disposable.d.ts:36
Inherited from
Disposable.[dispose]
Properties
snapshot
readonly snapshot: () => SemaphoreSnapshot;
Defined in: packages/common/src/Task.ts:2747
Returns the current mutex state for monitoring/debugging.
withLock
readonly withLock: <T, E, D>(task: Task<T, E, D>) => Task<T, E, D>;
Defined in: packages/common/src/Task.ts:2744
Executes a Task while holding the mutex lock.
Only one Task can hold the lock at a time. Other Tasks wait until the lock is released.