Files
OliveTin/webui.dev/js/Mutex.js
T
James Read af75afa82f
Build Snapshot / build-snapshot (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Codestyle checks / codestyle (push) Has been cancelled
DevSkim / DevSkim (push) Has been cancelled
bugfix: #401 Duplicate lines in output (#623)
2025-07-19 23:34:59 +00:00

25 lines
474 B
JavaScript

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);
}
}
}