1
0
mirror of https://git.hibas.dev/OpenServer/NodeLogging.git synced 2025-09-03 21:36:59 +00:00

Redesign File writing and some tweaks

This commit is contained in:
Fabian Stamm
2018-08-09 19:52:01 +02:00
parent 55344fb638
commit 29fee51b63
11 changed files with 418 additions and 254 deletions

36
src/lock.ts Normal file
View File

@ -0,0 +1,36 @@
export type Release = { release: () => void };
export default class Lock {
private _locked: boolean = false;
get locked() {
return this._locked;
}
private toCome: (() => void)[] = [];
constructor() {
this.release = this.release.bind(this);
}
async getLock(): Promise<Release> {
if (!this._locked) return { release: this.lock() };
else {
return new Promise<Release>((resolve) => {
this.toCome.push(() => {
resolve({ release: this.lock() });
})
})
}
}
private lock() {
this._locked = true;
return this.release;
}
private async release() {
if (this.toCome.length > 0) {
this.toCome.shift()();
} else {
this._locked = false;
}
}
}