From a0ce76ccf06f0763adf34477d0b1b67e28537a33 Mon Sep 17 00:00:00 2001 From: Yousif Yassi Date: Fri, 1 Dec 2023 14:44:42 -0500 Subject: [PATCH] fix: DIA-754: [FE] Search option does not reflect actual queries state --- src/stores/AppStore.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/stores/AppStore.js b/src/stores/AppStore.js index c0b597dd..8a086174 100644 --- a/src/stores/AppStore.js +++ b/src/stores/AppStore.js @@ -135,6 +135,7 @@ export const AppStore = types .volatile(() => ({ needsDataFetch: false, projectFetch: false, + requestsInFlight: new Map(), })) .actions((self) => ({ startPolling() { @@ -544,12 +545,21 @@ export const AppStore = types * @param {{ errorHandler?: fn }} [options] additional options like errorHandler */ apiCall: flow(function* (methodName, params, body, options) { + const controller = new AbortController(); + const signal = controller.signal; const apiTransform = self.SDK.apiTransform?.[methodName]; const requestParams = apiTransform?.params?.(params) ?? params ?? {}; - const requestBody = apiTransform?.body?.(body) ?? body ?? undefined; - + const requestBody = { signal, ...(apiTransform?.body?.(body) ?? body) }; + const requestKey = `${methodName}_${JSON.stringify(params || {})}`; + + if (self.requestsInFlight.has(requestKey)) { + /* if already in flight cancel the first in favor of new one */ + self.requestsInFlight.get(requestKey).abort(); + } + self.requestsInFlight.set(requestKey, controller); let result = yield self.API[methodName](requestParams, requestBody); + self.requestsInFlight.delete(requestKey); if (result.error && result.status !== 404) { if (options?.errorHandler?.(result)) { return result;