mirror of
https://github.com/OliveTin/OliveTin
synced 2025-12-12 09:05:39 +00:00
chore: OliveTin 3k progress
This commit is contained in:
24
frontend/js/Mutex.js
Normal file
24
frontend/js/Mutex.js
Normal file
@@ -0,0 +1,24 @@
|
||||
export class Mutex {
|
||||
constructor() {
|
||||
this._locked = false;
|
||||
this._waiting = [];
|
||||
}
|
||||
|
||||
lock() {
|
||||
const unlock = () => {
|
||||
const next = this._waiting.shift();
|
||||
if (next) {
|
||||
next(unlock);
|
||||
} else {
|
||||
this._locked = false;
|
||||
}
|
||||
};
|
||||
|
||||
if (this._locked) {
|
||||
return new Promise(resolve => this._waiting.push(resolve)).then(() => unlock);
|
||||
} else {
|
||||
this._locked = true;
|
||||
return Promise.resolve(unlock);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user