Skip to content

Instantly share code, notes, and snippets.

@SamiAlsubhi
Last active May 18, 2022 03:14
Show Gist options
  • Save SamiAlsubhi/c963be4cc85024d616a150b3334422d4 to your computer and use it in GitHub Desktop.
Save SamiAlsubhi/c963be4cc85024d616a150b3334422d4 to your computer and use it in GitHub Desktop.
Locking solution for Axios requests
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