This repository has been archived on 2021-06-02. You can view files and clone it, but cannot push or open issues or pull requests.
2019-11-06 23:27:29 +01:00

27 lines
673 B
TypeScript

export type Release = { release: () => void };
export default class DocumentLock {
private locks = new Map<string, (() => void)[]>();
getLocks() {
return Array.from(this.locks.keys())
}
async lock(collection: string = "", document: string = "") {
let key = collection + "/" + document;
let l = this.locks.get(key);
if (l)
await new Promise(resolve => { l.push(resolve); this.locks.set(key, l) });
else {
l = [];
this.locks.set(key, l);
}
return () => {
if (l.length > 0)
setImmediate(() => l.shift()());
else
this.locks.delete(key)
}
}
}