mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-17 10:06:17 +08:00
37 lines
697 B
TypeScript
37 lines
697 B
TypeScript
export class AsyncLock {
|
|
private _lock: Promise<void> | null = null;
|
|
|
|
async acquire() {
|
|
let release: (() => void) | null = null;
|
|
const nextLock = new Promise<void>(resolve => {
|
|
release = () => {
|
|
this._lock = null;
|
|
resolve();
|
|
};
|
|
});
|
|
|
|
// Atomic check and set of lock state
|
|
const currentLock = this._lock;
|
|
this._lock = nextLock;
|
|
|
|
if (currentLock) {
|
|
await currentLock;
|
|
}
|
|
|
|
return {
|
|
release: () => {
|
|
if (release) {
|
|
release();
|
|
release = null;
|
|
}
|
|
},
|
|
[Symbol.dispose]: () => {
|
|
if (release) {
|
|
release();
|
|
release = null;
|
|
}
|
|
},
|
|
};
|
|
}
|
|
}
|