Skip to content

Commit

Permalink
Optimization: Provide signals for the http client to break redundant …
Browse files Browse the repository at this point in the history
…requests
  • Loading branch information
usavkov-epam committed Dec 3, 2024
1 parent 359c293 commit 459d630
Show file tree
Hide file tree
Showing 24 changed files with 47 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/OrderLinesList/hooks/useOrderLines/useOrderLines.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const useOrderLines = ({ pagination, fetchReferences, customFields }) =>

const { isFetching, data = {} } = useQuery(
[namespace, pagination.timestamp, pagination.limit, pagination.offset],
async () => {
async ({ signal }) => {
moment.tz.setDefault(timezone);

const query = await buildQuery();
Expand All @@ -46,7 +46,7 @@ export const useOrderLines = ({ pagination, fetchReferences, customFields }) =>
query,
};

const { poLines, totalRecords } = await ky.get(LINES_API, { searchParams }).json();
const { poLines, totalRecords } = await ky.get(LINES_API, { searchParams, signal }).json();
const { ordersMap = {}, acqUnitsMap = {} } = await fetchReferences(poLines);
const orderLines = poLines.map(orderLine => ({
...orderLine,
Expand Down
4 changes: 2 additions & 2 deletions src/OrdersList/hooks/useOrders/useOrders.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export const useOrders = ({ pagination, fetchReferences, customFields }) => {

const { isFetching, data = {} } = useQuery(
[namespace, pagination.timestamp, pagination.limit, pagination.offset],
async () => {
async ({ signal }) => {
if (!filtersCount) {
return { orders: [], ordersCount: 0 };
}

const { purchaseOrders, totalRecords } = await ky.get(ORDERS_API, { searchParams }).json();
const { purchaseOrders, totalRecords } = await ky.get(ORDERS_API, { searchParams, signal }).json();
const { usersMap = {}, vendorsMap = {}, acqUnitsMap = {} } = await fetchReferences(purchaseOrders);
const orders = purchaseOrders.map(order => ({
...order,
Expand Down
2 changes: 1 addition & 1 deletion src/common/hooks/useAcqMethod/useAcqMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const useAcqMethod = (methodId, options = {}) => {

const { isLoading, data } = useQuery(
[namespace, methodId],
() => ky.get(`${ACQUISITION_METHODS_API}/${methodId}`).json(),
({ signal }) => ky.get(`${ACQUISITION_METHODS_API}/${methodId}`, { signal }).json(),
{
enabled: Boolean(methodId),
...options,
Expand Down
2 changes: 1 addition & 1 deletion src/common/hooks/useAcqMethods/useAcqMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const useAcqMethods = (options = {}) => {
data = DEFAULT_DATA,
} = useQuery(
[namespace, 'acq-methods'],
() => ky.get(ACQUISITION_METHODS_API, { searchParams }).json(),
({ signal }) => ky.get(ACQUISITION_METHODS_API, { searchParams, signal }).json(),
options,
);

Expand Down
2 changes: 1 addition & 1 deletion src/common/hooks/useInstance/useInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useInstance = (instanceId, options = DEFAULT_OPTIONS) => {
data: instance = DEFAULT_DATA,
} = useQuery(
[namespace, instanceId, tenantId],
() => ky.get(`inventory/instances/${instanceId}`).json(),
({ signal }) => ky.get(`inventory/instances/${instanceId}`, { signal }).json(),
{ enabled: Boolean(instanceId) },
);

Expand Down
2 changes: 1 addition & 1 deletion src/common/hooks/useLinesLimit/useLinesLimit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useLinesLimit = (enabled = true) => {

const { isLoading, data = {} } = useQuery(
[namespace],
() => ky.get(CONFIG_API, { searchParams }).json(),
({ signal }) => ky.get(CONFIG_API, { searchParams, signal }).json(),
{ enabled },
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const useOpenOrderSettings = (options = {}) => {

const { isFetching, data = {} } = useQuery(
[namespace],
() => ky.get(CONFIG_API, { searchParams }).json(),
({ signal }) => ky.get(CONFIG_API, { searchParams, signal }).json(),
options,
);

Expand Down
6 changes: 3 additions & 3 deletions src/common/hooks/useOrder/useOrder.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export const useOrder = (orderId) => {

const { isLoading, data } = useQuery(
['ui-orders', 'order', orderId],
async () => {
async ({ signal }) => {
try {
return ky.get(`${ORDERS_API}/${orderId}`).json();
return ky.get(`${ORDERS_API}/${orderId}`, { signal }).json();
} catch {
const { purchaseOrders } = await ky.get(`${ORDERS_API}`, { searchParams }).json();
const { purchaseOrders } = await ky.get(`${ORDERS_API}`, { searchParams, signal }).json();

return purchaseOrders[0] || {};
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/hooks/useOrderLine/useOrderLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useOrderLine = (lineId) => {

const { isLoading, data } = useQuery(
[namespace, lineId],
async () => ky.get(`${LINES_API}/${lineId}`).json(),
async ({ signal }) => ky.get(`${LINES_API}/${lineId}`, { signal }).json(),
{
enabled: Boolean(lineId),
},
Expand Down
2 changes: 1 addition & 1 deletion src/common/hooks/useOrderTemplate/useOrderTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const useOrderTemplate = (orderTemplateId) => {
data = {},
} = useQuery(
[namespace, orderTemplateId],
() => ky.get(`${ORDER_TEMPLATES_API}/${orderTemplateId}`).json(),
({ signal }) => ky.get(`${ORDER_TEMPLATES_API}/${orderTemplateId}`, { signal }).json(),
{ enabled: Boolean(orderTemplateId) },
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const usePOLineRelatedItems = (poLine, { offset, limit } = {}) => {
};

const queryKey = [namespace, limit, offset, poLine.id];
const queryFn = () => ky.get(ITEMS_API, { searchParams }).json();
const queryFn = ({ signal }) => ky.get(ITEMS_API, { searchParams, signal }).json();

const {
data,
Expand Down
2 changes: 1 addition & 1 deletion src/common/hooks/useVendor/useVendor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const useVendor = (vendorId) => {

const { isLoading, data } = useQuery(
['ui-orders', 'vendor', vendorId],
() => ky.get(`${VENDORS_API}/${vendorId}`).json(),
({ signal }) => ky.get(`${VENDORS_API}/${vendorId}`, { signal }).json(),
{ enabled: Boolean(vendorId) },
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useChangeInstanceModalConfigs = (poLine) => {
isLoading,
} = useQuery({
queryKey: [namespace, poLine.id],
queryFn: () => checkRelatedHoldings(ky)(poLine),
queryFn: ({ signal }) => checkRelatedHoldings(ky.extend({ signal }))(poLine),
queryOptions: {
enabled: isDetailed,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export const useInstanceRelationTypes = () => {

const { refetch } = useQuery(
['ui-orders', 'linked-lines'],
() => {
({ signal }) => {
const searchParams = {
limit: LIMIT_MAX,
query: 'cql.allRecords=1',
};

return ky.get('instance-relationship-types', { searchParams }).json();
return ky.get('instance-relationship-types', { searchParams, signal }).json();
},
{ enabled: false },
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ export const useConnectedInvoiceLines = (orderLineId) => {

const { isLoading, data = [] } = useQuery(
[namespace],
async () => {
async ({ signal }) => {
const { invoiceLines = [], totalRecords } = await ky.get(INVOICE_LINES_API, {
searchParams: {
query: `poLineId==${orderLineId}`,
limit: LIMIT_MAX,
},
signal,
}).json();

const invoicesIds = invoiceLines.map(({ invoiceId }) => invoiceId);
const invoices = await batchRequest(
async ({ params: searchParams }) => {
const invoicesData = await ky.get(INVOICES_API, { searchParams }).json();
const invoicesData = await ky.get(INVOICES_API, { searchParams, signal }).json();

return invoicesData.invoices;
},
Expand All @@ -50,7 +51,7 @@ export const useConnectedInvoiceLines = (orderLineId) => {
const vendorIds = invoices.map(({ vendorId }) => vendorId);
const vendors = await batchRequest(
async ({ params: searchParams }) => {
const vendorsData = await ky.get(VENDORS_API, { searchParams }).json();
const vendorsData = await ky.get(VENDORS_API, { searchParams, signal }).json();

return vendorsData.organizations;
},
Expand All @@ -61,7 +62,7 @@ export const useConnectedInvoiceLines = (orderLineId) => {
const fiscalYearIds = [...new Set(invoices.map(({ fiscalYearId }) => fiscalYearId))];
const fiscalYears = await batchRequest(
async ({ params: searchParams }) => {
const fiscalYearData = await ky.get(FISCAL_YEARS_API, { searchParams }).json();
const fiscalYearData = await ky.get(FISCAL_YEARS_API, { searchParams, signal }).json();

return fiscalYearData.fiscalYears;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const usePOLineVersions = (poLineId, options = {}) => {

const { isLoading, data } = useQuery(
[namespace, poLineId],
() => ky.get(`${AUDIT_ACQ_EVENTS_API}/order-line/${poLineId}`).json(),
({ signal }) => ky.get(`${AUDIT_ACQ_EVENTS_API}/order-line/${poLineId}`, { signal }).json(),
{
enabled: Boolean(poLineId),
...options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export const useSelectedPOLineVersion = ({ versionId, versions, snapshotPath, ce
data: selectedVersion = {},
} = useQuery(
[namespace, versionId, versionSnapshot?.id],
async () => {
async ({ signal }) => {
const kyExtended = ky.extend({ signal });

const accessProviderId = versionSnapshot?.accessProvider;
const eresource = versionSnapshot?.eresource || {};
const physical = versionSnapshot?.physical || {};
Expand All @@ -96,8 +98,8 @@ export const useSelectedPOLineVersion = ({ versionId, versions, snapshotPath, ce
organizationsMap,
materialTypesMap,
] = await Promise.all([
getOrganizationsByIds(ky)(organizationIds).then(keyBy('id')),
getMaterialTypes(ky)()
getOrganizationsByIds(kyExtended)(organizationIds).then(keyBy('id')),
getMaterialTypes(kyExtended)()
.then(({ mtypes }) => mtypes)
.then(keyBy('id')),
]);
Expand Down
8 changes: 4 additions & 4 deletions src/components/PurchaseOrder/POInvoices/useRelatedInvoices.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export const useRelatedInvoices = (invoiceIds = []) => {

const { isLoading, data = [] } = useQuery(
[namespace, ...invoiceIds],
async () => {
async ({ signal }) => {
const invoices = await batchRequest(
async ({ params: searchParams }) => {
const invoicesData = await ky.get(INVOICES_API, { searchParams }).json();
const invoicesData = await ky.get(INVOICES_API, { searchParams, signal }).json();

return invoicesData.invoices;
},
Expand All @@ -31,7 +31,7 @@ export const useRelatedInvoices = (invoiceIds = []) => {
const vendorIds = invoices.map(({ vendorId }) => vendorId);
const vendors = await batchRequest(
async ({ params: searchParams }) => {
const vendorsData = await ky.get(VENDORS_API, { searchParams }).json();
const vendorsData = await ky.get(VENDORS_API, { searchParams, signal }).json();

return vendorsData.organizations;
},
Expand All @@ -42,7 +42,7 @@ export const useRelatedInvoices = (invoiceIds = []) => {
const fiscalYearIds = invoices.map(({ fiscalYearId }) => fiscalYearId);
const fiscalYears = await batchRequest(
async ({ params: searchParams }) => {
const fiscalYearData = await ky.get(FISCAL_YEARS_API, { searchParams }).json();
const fiscalYearData = await ky.get(FISCAL_YEARS_API, { searchParams, signal }).json();

return fiscalYearData.fiscalYears;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const usePOVersions = (orderId, options = {}) => {

const { isLoading, data } = useQuery(
[namespace, orderId],
() => ky.get(`${AUDIT_ACQ_EVENTS_API}/order/${orderId}`).json(),
({ signal }) => ky.get(`${AUDIT_ACQ_EVENTS_API}/order/${orderId}`, { signal }).json(),
{
enabled: Boolean(orderId),
...options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export const useSelectedPOVersion = ({ versionId, versions, snapshotPath }, opti
data: selectedVersion = {},
} = useQuery(
[namespace, versionId, versionSnapshot?.id],
async () => {
async ({ signal }) => {
const kyExtended = ky.extend({ signal });

const organizationIds = [vendorId];
const acqUnitsIds = versionSnapshot?.acqUnitIds || [];

Expand All @@ -80,9 +82,9 @@ export const useSelectedPOVersion = ({ versionId, versions, snapshotPath }, opti
acqUnitsMap,
addressesMap,
] = await Promise.all([
getOrganizationsByIds(ky)(organizationIds).then(keyBy('id')),
getAcqUnitsByIds(ky)(acqUnitsIds).then(keyBy('id')),
getTenantAddresses(ky)()
getOrganizationsByIds(kyExtended)(organizationIds).then(keyBy('id')),
getAcqUnitsByIds(kyExtended)(acqUnitsIds).then(keyBy('id')),
getTenantAddresses(kyExtended)()
.then(({ configs }) => getAddresses(configs))
.then(keyBy('id')),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export const useRoutingAddressSettings = (options = {}) => {
error,
} = useQuery({
queryKey: [namespace],
queryFn: async () => {
const response = await ky.get(ORDERS_STORAGE_SETTINGS_API, { searchParams }).json();
queryFn: async ({ signal }) => {
const response = await ky.get(ORDERS_STORAGE_SETTINGS_API, { searchParams, signal }).json();

return response?.settings?.[0];
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const useUserAddressTypes = (options = {}) => {
data = {},
} = useQuery(
[namespace, 'user-address-types'],
() => ky.get(USER_ADDRESS_TYPES_API, { searchParams }).json(),
({ signal }) => ky.get(USER_ADDRESS_TYPES_API, { searchParams, signal }).json(),
options,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const useListConfiguration = () => {

const { isFetching, data, refetch } = useQuery(
[namespace, LIST_CONFIGURATION_TEMPLATE_ID],
() => ky.get(`${TEMPLATES_API}/${LIST_CONFIGURATION_TEMPLATE_ID}`).json(),
({ signal }) => ky.get(`${TEMPLATES_API}/${LIST_CONFIGURATION_TEMPLATE_ID}`, { signal }).json(),
);

return ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export const useDefaultReceivingSearchSettings = (options = {}) => {
refetch,
} = useQuery({
queryKey: [namespace],
queryFn: async () => {
const response = await ky.get(ORDERS_STORAGE_SETTINGS_API, { searchParams }).json();
queryFn: async ({ signal }) => {
const response = await ky.get(ORDERS_STORAGE_SETTINGS_API, { searchParams, signal }).json();

return response?.settings?.[0];
},
Expand Down

0 comments on commit 459d630

Please sign in to comment.