Created
January 14, 2022 11:54
-
-
Save cefn/151b6fd36ac1bc64b53613524f5b0c31 to your computer and use it in GitHub Desktop.
Simple lock implementation
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
let releaseGlobalsPromise: null | Promise<void> = null; | |
async function acquireGlobalsLock() { | |
while (releaseGlobalsPromise) { | |
await releaseGlobalsPromise; | |
} | |
let release!: () => void; | |
releaseGlobalsPromise = new Promise( | |
(resolve) => | |
(release = () => { | |
releaseGlobalsPromise = null; | |
resolve(); | |
}), | |
); | |
return release; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment