Files
OliveTin/frontend/js/Mutex.js
jamesread 570c0ba087
Some checks failed
Build Snapshot / build-snapshot (push) Has been cancelled
DevSkim / DevSkim (push) Has been cancelled
Buf CI / buf (push) Has been cancelled
chore: Repair output streaming, lots of css/go lint
2025-09-06 08:42:13 +01:00

25 lines
467 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)
}
}
}