Skip to content

Commit fb64cd3

Browse files
committed
feat: add support for abortControllerRef and deprecate signal
1 parent c213386 commit fb64cd3

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.0.4-beta-2",
3+
"version": "1.0.4-beta-4",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Api.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,33 @@ function fetchInTime<T = object>(
192192
options?: APIOptions,
193193
isMultipartRequest?: boolean,
194194
): Promise<ResponseType> {
195-
const controller = new AbortController()
196-
const { signal } = controller
195+
const controller = options?.abortControllerRef?.current ?? new AbortController()
196+
const signal = options?.abortControllerRef?.current?.signal || options?.signal || controller.signal
197197
const timeoutPromise: Promise<ResponseType> = new Promise((resolve, reject) => {
198198
const requestTimeout = (window as any)?._env_?.GLOBAL_API_TIMEOUT || FALLBACK_REQUEST_TIMEOUT
199199
const timeout = options?.timeout ? options.timeout : requestTimeout
200200

201201
setTimeout(() => {
202202
controller.abort()
203+
if (options?.abortControllerRef?.current) {
204+
options.abortControllerRef.current = new AbortController()
205+
}
206+
203207
reject({
204208
code: 408,
205209
errors: [{ code: 408, internalMessage: 'Request cancelled', userMessage: 'Request Cancelled' }],
206210
})
207211
}, timeout)
208212
})
209213
return Promise.race([
210-
fetchAPI(url, type, data, options?.signal || signal, options?.preventAutoLogout || false, isMultipartRequest),
214+
fetchAPI(
215+
url,
216+
type,
217+
data,
218+
signal,
219+
options?.preventAutoLogout || false,
220+
isMultipartRequest,
221+
),
211222
timeoutPromise,
212223
]).catch((err) => {
213224
if (err instanceof ServerErrors) {

src/Common/Types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import React, { ReactNode, CSSProperties, ReactElement } from 'react'
17+
import React, { ReactNode, CSSProperties, ReactElement, MutableRefObject } from 'react'
1818
import { Placement } from 'tippy.js'
1919
import { UserGroupDTO } from '@Pages/GlobalConfigurations'
2020
import { ImageComment, ReleaseTag } from './ImageTags.Types'
@@ -50,7 +50,11 @@ export interface ResponseType<T = any> {
5050

5151
export interface APIOptions {
5252
timeout?: number
53+
/**
54+
* @deprecated Use abortController instead
55+
*/
5356
signal?: AbortSignal
57+
abortControllerRef?: MutableRefObject<AbortController>
5458
preventAutoLogout?: boolean
5559
}
5660

@@ -1007,4 +1011,4 @@ export interface WidgetEventDetails {
10071011
count: number
10081012
age: string
10091013
lastSeen: string
1010-
}
1014+
}

0 commit comments

Comments
 (0)