Last active
May 18, 2022 03:14
-
-
Save SamiAlsubhi/c963be4cc85024d616a150b3334422d4 to your computer and use it in GitHub Desktop.
Locking solution for Axios requests
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
class AxiosLock { | |
isLocked: boolean = false | |
#promiseResolvers: Array<(value: unknown) => void> = [] | |
constructor() { } | |
lock = () => { | |
this.isLocked = true; | |
} | |
unlock = () => { | |
this.isLocked = false; | |
this.#promiseResolvers.forEach(resolve => resolve(null)) | |
this.#promiseResolvers = [] | |
} | |
#timeout = (resolve: (value: unknown) => void) => setTimeout(() => { | |
this.isLocked = false; | |
resolve(null); | |
}, 60000);// timeout after 1 min | |
waitForUnlock = () => { | |
return new Promise((resolve, _) => { | |
this.#promiseResolvers.push(resolve) | |
this.#timeout(resolve) | |
}); | |
} | |
} | |
const axiosLock = new AxiosLock(); | |
export default axiosLock; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment