Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
reduced hammering as a result of cancelations
Browse files Browse the repository at this point in the history
  • Loading branch information
yyassi-heartex committed Dec 5, 2023
1 parent a0ce76c commit 92241fb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/mixins/DataStore/DataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export const DataStore = (
// We cancel current request processing if request id
// cnhaged during the request. It indicates that something
// triggered another request while current one is not yet finished
if (requestId !== self.requestId) {
if (requestId !== self.requestId || data.isCancelled) {
console.log(`Request ${requestId} was cancelled by another request`);
return;
}
Expand Down
9 changes: 6 additions & 3 deletions src/stores/AppStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,18 +549,21 @@ export const AppStore = types
const signal = controller.signal;
const apiTransform = self.SDK.apiTransform?.[methodName];
const requestParams = apiTransform?.params?.(params) ?? params ?? {};
const requestBody = { signal, ...(apiTransform?.body?.(body) ?? body) };
const requestBody = apiTransform?.body?.(body) ?? body ?? {};
const requestHeaders = { signal, ...(apiTransform?.headers?.(options?.headers) ?? options?.headers ?? {}) };
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();
console.log(`Request ${requestKey} canceled`);
}
self.requestsInFlight.set(requestKey, controller);
let result = yield self.API[methodName](requestParams, requestBody);
let result = yield self.API[methodName](requestParams, { headers: requestHeaders, body: requestBody.body ?? requestBody });

result.isCanceled = signal.aborted;
self.requestsInFlight.delete(requestKey);
if (result.error && result.status !== 404) {
if (result.error && result.status !== 404 && !signal.aborted) {
if (options?.errorHandler?.(result)) {
return result;
}
Expand Down
5 changes: 5 additions & 0 deletions src/stores/Tabs/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ export const TabStore = types
!view.saved && root.apiVersion === 2 ? "createTab" : "updateTab";

const result = yield root.apiCall(apiMethod, params, body);

if (result.isCanceled) {
view.unlock();
return view;
}
const viewSnapshot = getSnapshot(view);
const newViewSnapshot = {
...viewSnapshot,
Expand Down

0 comments on commit 92241fb

Please sign in to comment.