API reference › @evolu/common › Task › createMutexByKey
Call Signature
function createMutexByKey<K>(
options?: CreateMutexByKeyOptions<K, unknown>,
): MutexByKey<K>;
Defined in: packages/common/src/Task.ts:6107
Creates a MutexByKey.
Example
import {
assertOk,
assertTrue,
createMutexByKey,
createRun,
ok,
type Task,
} from "@evolu/common";
const accountLocks = createMutexByKey<string>();
const balancesByAccount = new Map<string, number>();
const deposit = (account: string, amount: number): Task<number> =>
accountLocks.withLock(account, () => {
const balance = (balancesByAccount.get(account) ?? 0) + amount;
balancesByAccount.set(account, balance);
return ok(balance);
});
await using run = createRun();
assertOk(await run(deposit("checking", 2)), 2);
assertOk(await run(deposit("checking", 3)), 5);
assertTrue(accountLocks.isIdle("checking"));
Call Signature
function createMutexByKey<K, L>(
options: CreateMutexByKeyOptions<K, L>,
): MutexByKey<K>;
Defined in: packages/common/src/Task.ts:6112
Creates a MutexByKey with custom logical key lookup.