mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-19 01:24:20 +01:00
29 lines
725 B
TypeScript
29 lines
725 B
TypeScript
export class AsyncLock {
|
|
locked: boolean;
|
|
queue: any[];
|
|
constructor() {
|
|
this.locked = false;
|
|
this.queue = []
|
|
}
|
|
async acquire() {
|
|
if (!this.locked) {
|
|
this.locked = true;
|
|
return Promise.resolve();
|
|
} else {
|
|
const block = new Promise(resolve => { this.queue.push(resolve) });
|
|
return block;
|
|
}
|
|
}
|
|
release() {
|
|
if (this.locked == false) {
|
|
throw new Error("invalid state: lock was already unlocked");
|
|
}
|
|
const item = this.queue.shift();
|
|
if (item != null) {
|
|
this.locked = true;
|
|
item();
|
|
} else {
|
|
this.locked = false;
|
|
}
|
|
}
|
|
} |