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

Commit

Permalink
restricting cancelable requests to use as needed and DE only
Browse files Browse the repository at this point in the history
  • Loading branch information
yyassi-heartex committed Dec 5, 2023
1 parent 92241fb commit 2686bbc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/mixins/DataStore/DataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,13 @@ export const DataStore = (
fetch: flow(function* ({ id, query, pageNumber = null, reload = false, interaction, pageSize } = {}) {
let currentViewId, currentViewQuery;
const requestId = self.requestId = guidGenerator();
const root = getRoot(self);

if (id) {
currentViewId = id;
currentViewQuery = query;
} else {
const currentView = getRoot(self).viewsStore.selected;
const currentView = root.viewsStore.selected;

currentViewId = currentView?.id;
currentViewQuery = currentView?.virtual ? currentView?.query : null;
Expand Down Expand Up @@ -226,18 +227,18 @@ export const DataStore = (

if (interaction) Object.assign(params, { interaction });

const data = yield getRoot(self).apiCall(apiMethod, params);
const data = yield root.apiCall(apiMethod, params, {}, { allowCancel: root.SDK.type === 'DE' });

// We cancel current request processing if request id
// cnhaged during the request. It indicates that something
// changed during the request. It indicates that something
// triggered another request while current one is not yet finished
if (requestId !== self.requestId || data.isCancelled) {
console.log(`Request ${requestId} was cancelled by another request`);
return;
}

const highlightedID = self.highlighted;
const apiMethodSettings = getRoot(self).API.getSettingsByMethodName(apiMethod);
const apiMethodSettings = root.API.getSettingsByMethodName(apiMethod);
const { total, [apiMethod]: list } = data;
let associatedList = [];

Expand All @@ -260,7 +261,7 @@ export const DataStore = (

self.loading = false;

getRoot(self).SDK.invoke('dataFetched', self);
root.SDK.invoke('dataFetched', self);
}),

reload: flow(function* ({ id, query, interaction } = {}) {
Expand Down
24 changes: 15 additions & 9 deletions src/stores/AppStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,27 +542,33 @@ export const AppStore = types
* @param {string} methodName one of the methods in api-config
* @param {object} params url vars and query string params
* @param {object} body for POST/PATCH requests
* @param {{ errorHandler?: fn }} [options] additional options like errorHandler
* @param {{ errorHandler?: fn, headers?: object, allowCancel?: boolean }} [options] additional options like errorHandler
*/
apiCall: flow(function* (methodName, params, body, options) {
const isAllowCancel = options?.allowCancel;
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 ?? {};
const requestHeaders = { signal, ...(apiTransform?.headers?.(options?.headers) ?? options?.headers ?? {}) };
const requestHeaders = 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`);
if (isAllowCancel) {
requestHeaders.signal = signal;
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);
}
self.requestsInFlight.set(requestKey, controller);
let result = yield self.API[methodName](requestParams, { headers: requestHeaders, body: requestBody.body ?? requestBody });

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

const result = yield root.apiCall(apiMethod, params, body);
const result = yield root.apiCall(apiMethod, params, body, { allowCancel: root.SDK.type === 'DE' });

if (result.isCanceled) {
view.unlock();
Expand Down

0 comments on commit 2686bbc

Please sign in to comment.