class SingleLock { promise?: Promise; resolve?: (result?: T) => void; constructor (locked: boolean = false) { if (locked) this.lock(); } get locked (): boolean { return !!this.resolve; } lock (): Promise { console.assert(!this.locked, "[SingleLock] duplicated locking, last locking has't been released yet."); this.promise = new Promise(resolve => this.resolve = resolve); return this.promise; } release (result?: T) { if (this.resolve) { this.resolve(result); this.resolve = null; } } wait (): Promise { return this.promise; } }; export { SingleLock, };