-
Notifications
You must be signed in to change notification settings - Fork 0
lock
awekrx edited this page May 29, 2026
·
1 revision
import { lock } from '@dev-suite/decorators/lock'
method
Prevent overlapping method execution for critical sections.
- Manual mutex/boolean guard patterns
- Repeated race-condition boilerplate
class WalletService {
private busy = false;
async transfer(input: TransferInput) {
if (this.busy) throw new Error('transfer in progress');
this.busy = true;
try {
return await this.engine.transfer(input);
} finally {
this.busy = false;
}
}
}import { lock } from '@dev-suite/decorators/lock';
class WalletService {
@lock()
async transfer(input: TransferInput) {
return this.engine.transfer(input);
}
}- Critical section is declared at method level.
- Avoids forgotten unlock paths in error branches.
class SequenceService {
private locks = new Map<string, boolean>();
async next(sequence: string) {
if (this.locks.get(sequence)) throw new Error('locked');
this.locks.set(sequence, true);
try {
return await this.repo.next(sequence);
} finally {
this.locks.delete(sequence);
}
}
}import { lock } from '@dev-suite/decorators/lock';
class SequenceService {
@lock({ keyResolver: ([sequence]) => String(sequence) })
async next(sequence: string) {
return this.repo.next(sequence);
}
}- Supports per-key locking for independent resources.
- Cleaner than hand-rolled lock maps.