First Commit

This commit is contained in:
Fabian
2019-09-18 21:54:28 +02:00
commit 429ba7e291
27 changed files with 5815 additions and 0 deletions

43
src/database/lock.ts Normal file
View File

@ -0,0 +1,43 @@
export type Release = { release: () => void };
export default class PathLock {
locks: {
path: string[],
next: (() => void)[]
}[] = [];
constructor() { }
async lock(path: string[]) {
let locks = this.locks.filter(lock => {
let idxs = Math.min(lock.path.length, path.length);
if (idxs === 0) return true;
for (let i = 0; i < idxs; i++) {
if (lock.path[i] !== path[i])
return false;
}
return true;
})
if (locks.length > 0) { // await till release
await Promise.all(locks.map(l => new Promise(res => l.next.push(res))))
} else {
let lock = {
path: path,
next: []
}
this.locks.push(lock);
locks = [lock];
}
return () => {
locks.forEach(lock => {
if (lock.next.length > 0) {
setImmediate(() => lock.next.shift()());
} else {
this.locks.splice(this.locks.indexOf(lock), 1);
}
})
}
}
}