forked from AutomaApp/automa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request AutomaApp#1471 from Siykt/refactor/sychronized-lock
Refactor: More fine-grained locking mechanism
- Loading branch information
Showing
5 changed files
with
80 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class SynchronizedLock { | ||
constructor() { | ||
this.lock = false; | ||
this.queue = []; | ||
} | ||
|
||
async getLock(timeout = 10000) { | ||
while (this.lock) { | ||
await new Promise((resolve) => { | ||
this.queue.push(resolve); | ||
setTimeout(() => { | ||
const index = this.queue.indexOf(resolve); | ||
if (index !== -1) { | ||
this.queue.splice(index, 1); | ||
console.warn('SynchronizedLock timeout'); | ||
resolve(); | ||
} | ||
}, timeout); | ||
}); | ||
} | ||
|
||
this.lock = true; | ||
} | ||
|
||
releaseLock() { | ||
this.lock = false; | ||
const resolve = this.queue.shift(); | ||
if (resolve) resolve(); | ||
} | ||
} | ||
|
||
const synchronizedLock = new SynchronizedLock(); | ||
|
||
export default synchronizedLock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters