Skip to content

Commit ef41537

Browse files
committed
feat: enhance cancellable request functionality with unique keys
1 parent b1a47f7 commit ef41537

3 files changed

Lines changed: 14 additions & 18 deletions

File tree

mobile/slices/asset.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export const reducer = slice.reducer;
169169
export const getAssets =
170170
(criteria: SearchCriteria): AppThunk =>
171171
async (dispatch) => {
172-
const { signal } = createCancellableRequest();
172+
const { signal } = createCancellableRequest('getAssets');
173173
try {
174174
dispatch(slice.actions.setLoadingGet({ loading: true }));
175175
const assets = await api.post<Page<AssetDTO>>(
@@ -188,7 +188,7 @@ export const getAssets =
188188
export const getMoreAssets =
189189
(criteria: SearchCriteria, pageNum: number): AppThunk =>
190190
async (dispatch) => {
191-
const { signal } = createCancellableRequest();
191+
const { signal } = createCancellableRequest('getMoreAssets');
192192
criteria = { ...criteria, pageNum };
193193
try {
194194
dispatch(slice.actions.setLoadingGet({ loading: true }));
@@ -208,7 +208,7 @@ export const getMoreAssets =
208208
export const getAssetsMini =
209209
(locationId?: number | null): AppThunk =>
210210
async (dispatch) => {
211-
const { signal } = createCancellableRequest();
211+
const { signal } = createCancellableRequest('getAssetsMini');
212212
try {
213213
dispatch(slice.actions.setLoadingGet({ loading: true }));
214214
const assets = await api.get<AssetMiniDTO[]>(

mobile/slices/location.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export const reducer = slice.reducer;
145145
export const getLocations =
146146
(criteria: SearchCriteria): AppThunk =>
147147
async (dispatch) => {
148-
const { signal } = createCancellableRequest();
148+
const { signal } = createCancellableRequest('getLocations');
149149
try {
150150
dispatch(slice.actions.setLoadingGet({ loading: true }));
151151
const locations = await api.post<Page<Location>>(
@@ -164,7 +164,7 @@ export const getLocations =
164164
export const getMoreLocations =
165165
(criteria: SearchCriteria, pageNum: number): AppThunk =>
166166
async (dispatch) => {
167-
const { signal } = createCancellableRequest();
167+
const { signal } = createCancellableRequest('getMoreLocations');
168168
criteria = { ...criteria, pageNum };
169169
try {
170170
dispatch(slice.actions.setLoadingGet({ loading: true }));
@@ -195,7 +195,7 @@ export const getLocationDetails =
195195
dispatch(slice.actions.setLoadingGet({ loading: false }));
196196
};
197197
export const getLocationsMini = (): AppThunk => async (dispatch) => {
198-
const { signal } = createCancellableRequest();
198+
const { signal } = createCancellableRequest('getLocationsMini');
199199
try {
200200
dispatch(slice.actions.setLoadingGet({ loading: true }));
201201
const locations = await api.get<LocationMiniDTO[]>('locations/mini', {

mobile/utils/cancellableRequest.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,23 @@ interface CancellableRequest {
88
signal: AbortSignal | null;
99
}
1010

11-
let currentController: AbortController | null = null;
12-
1311
/**
1412
* Creates a new cancellable request, aborting any previous request.
1513
* @returns CancellableRequest object with abort function and signal
1614
*/
17-
export function createCancellableRequest(): CancellableRequest {
18-
// Abort previous request if it exists
19-
if (currentController) {
20-
currentController.abort();
21-
}
15+
const controllers = new Map<string, AbortController>();
16+
17+
export function createCancellableRequest(key: string): CancellableRequest {
18+
controllers.get(key)?.abort();
2219

23-
// Create new controller
24-
currentController = new AbortController();
20+
const controller = new AbortController();
21+
controllers.set(key, controller);
2522

2623
return {
27-
abort: () => currentController?.abort(),
28-
signal: currentController.signal
24+
abort: () => controller.abort(),
25+
signal: controller.signal
2926
};
3027
}
31-
3228
/**
3329
* Checks if an error is an AbortError (request was cancelled).
3430
*/

0 commit comments

Comments
 (0)