mirror of
https://github.com/foxhui/WebAI2API.git
synced 2026-06-16 21:03:59 +08:00
35 lines
956 B
JavaScript
35 lines
956 B
JavaScript
/**
|
|
* @fileoverview 轻量异步互斥锁
|
|
* @description 保证同一时刻只有一个 async 任务持有锁,用于防止同一浏览器实例的多个 Worker 并发操作鼠标。
|
|
*/
|
|
|
|
export class AsyncMutex {
|
|
constructor() {
|
|
this._queue = [];
|
|
this._locked = false;
|
|
}
|
|
|
|
/**
|
|
* 获取锁,返回释放函数
|
|
* @returns {Promise<() => void>}
|
|
*/
|
|
acquire() {
|
|
return new Promise(resolve => {
|
|
const tryAcquire = () => {
|
|
if (!this._locked) {
|
|
this._locked = true;
|
|
resolve(() => {
|
|
this._locked = false;
|
|
if (this._queue.length > 0) {
|
|
this._queue.shift()();
|
|
}
|
|
});
|
|
} else {
|
|
this._queue.push(tryAcquire);
|
|
}
|
|
};
|
|
tryAcquire();
|
|
});
|
|
}
|
|
}
|