-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replaced axios-concurrency with build-in typescript version (#869)
- Loading branch information
1 parent
d003a8f
commit 4b071b7
Showing
4 changed files
with
102 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
98 changes: 98 additions & 0 deletions
98
src/ui/libs/DatalensChartkit/modules/axios/axiosConcurrency.ts
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,98 @@ | ||
// based on https://github.com/bernawil/axios-concurrency/ | ||
|
||
import {AxiosInstance, AxiosResponse, InternalAxiosRequestConfig} from 'axios'; | ||
|
||
type Handler = { | ||
request: InternalAxiosRequestConfig; | ||
resolver(value: unknown): void; | ||
}; | ||
|
||
type Interceptors = { | ||
request: number; | ||
response: number; | ||
}; | ||
|
||
export type ConcurrencyManagerInstance = { | ||
queue: Handler[]; | ||
running: Handler[]; | ||
shiftInitial: () => void; | ||
push: (reqHandler: Handler) => void; | ||
shift: () => void; | ||
requestHandler(req: InternalAxiosRequestConfig): Promise<InternalAxiosRequestConfig>; | ||
responseHandler(res: AxiosResponse): AxiosResponse; | ||
responseErrorHandler(res: AxiosResponse): void; | ||
interceptors: Interceptors; | ||
detach(): void; | ||
}; | ||
|
||
export const concurrencyManager = ( | ||
axios: AxiosInstance, | ||
MAX_CONCURRENT = 10, | ||
): ConcurrencyManagerInstance => { | ||
if (MAX_CONCURRENT < 1) { | ||
throw 'Concurrency Manager Error: minimun concurrent requests is 1'; | ||
} | ||
const instance: ConcurrencyManagerInstance = { | ||
queue: [], | ||
running: [], | ||
shiftInitial: () => { | ||
setTimeout(() => { | ||
if (instance.running.length < MAX_CONCURRENT) { | ||
instance.shift(); | ||
} | ||
}, 0); | ||
}, | ||
push: (reqHandler) => { | ||
instance.queue.push(reqHandler); | ||
instance.shiftInitial(); | ||
}, | ||
shift: () => { | ||
if (instance.queue.length) { | ||
const queued = instance.queue.shift(); | ||
if (queued) { | ||
if (queued.request.cancelToken && queued.request.cancelToken.reason) { | ||
// the request was already cancelled - do not even start it, just forget it | ||
instance.shift(); | ||
return; | ||
} | ||
queued.resolver(queued.request); | ||
instance.running.push(queued); | ||
} | ||
} | ||
}, | ||
// Use as interceptor. Queue outgoing requests | ||
requestHandler: (req) => { | ||
return new Promise((resolve) => { | ||
instance.push({request: req, resolver: resolve}); | ||
}); | ||
}, | ||
// Use as interceptor. Execute queued request upon receiving a response | ||
responseHandler: (res) => { | ||
instance.running.shift(); | ||
instance.shift(); | ||
return res; | ||
}, | ||
responseErrorHandler: (res) => { | ||
return Promise.reject(instance.responseHandler(res)); | ||
}, | ||
interceptors: { | ||
request: -1, | ||
response: -1, | ||
}, | ||
detach: () => { | ||
if (instance.interceptors.request !== -1) { | ||
axios.interceptors.request.eject(instance.interceptors.request); | ||
} | ||
if (instance.interceptors.response !== -1) { | ||
axios.interceptors.response.eject(instance.interceptors.response); | ||
} | ||
}, | ||
}; | ||
// queue concurrent requests | ||
instance.interceptors.request = axios.interceptors.request.use(instance.requestHandler); | ||
instance.interceptors.response = axios.interceptors.response.use( | ||
instance.responseHandler, | ||
instance.responseErrorHandler, | ||
); | ||
return instance; | ||
}; |