Skip to content

Commit 96a7e80

Browse files
committed
Abort asynchronous calls using AbortController
1 parent 695bd56 commit 96a7e80

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

components/pages/submission/Environmental/Details/index.tsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ const SubmissionDetails = ({ ID }: SubmissionDetailsProps): ReactElement => {
6060

6161
// gets the initial status for all the uploads
6262
useEffect(() => {
63+
const controller = new AbortController();
6364
async function getDetailsSubmission() {
6465
try {
65-
const submissionResponse = await fetchSubmissionById(ID);
66+
const submissionResponse = await fetchSubmissionById(ID, { signal: controller.signal });
6667

6768
if (!submissionResponse?.data || !('inserts' in submissionResponse.data)) {
6869
console.error('Unexpected response getting submission details', submissionResponse);
@@ -109,21 +110,28 @@ const SubmissionDetails = ({ ID }: SubmissionDetailsProps): ReactElement => {
109110
if (token && totalUploads === 0) {
110111
getDetailsSubmission();
111112
}
113+
return () => {
114+
// Abort the request when the component unmounts or when a dependency changes
115+
controller.abort();
116+
};
112117
}, [token, ID]);
113118

114119
// get status updates if any are available from Submission Service
115120
useEffect(() => {
121+
const controller = new AbortController();
116122
async function commit() {
117123
// Commit submission
118-
const commitSubmissionResponse = await commitSubmission(ID);
124+
const commitSubmissionResponse = await commitSubmission(ID, { signal: controller.signal });
119125
if (commitSubmissionResponse.status === UploadStatus.PROCESSING) {
120126
setSubmissionStatus(SubmissionStatus.COMMITTED);
121127
}
122128
}
123129

124130
async function trackPendingData() {
125131
try {
126-
const analysisIds = await getAnalysisIds(organization, submissionDetails.PROCESSING);
132+
const analysisIds = await getAnalysisIds(organization, submissionDetails.PROCESSING, {
133+
signal: controller.signal,
134+
});
127135

128136
analysisIds.forEach((analysis) => {
129137
submissionDetailsDispatch({
@@ -154,6 +162,10 @@ const SubmissionDetails = ({ ID }: SubmissionDetailsProps): ReactElement => {
154162
break;
155163
}
156164
}
165+
return () => {
166+
// Abort the request when the component unmounts or when a dependency changes
167+
controller.abort();
168+
};
157169
}, [dataIsPending, submissionStatus, ID]);
158170

159171
return (

components/pages/submission/Environmental/PreviousSubmissions/index.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,21 @@ const PreviousSubmissions = (): ReactElement => {
4040
const [previousSubmissions, setPreviousSubmissions] = useState<DataRecord[]>([]);
4141

4242
useEffect(() => {
43+
const controller = new AbortController();
4344
token &&
4445
userHasWriteScopes &&
45-
fetchPreviousSubmissions()
46+
fetchPreviousSubmissions({ signal: controller.signal })
4647
.then((response) => {
4748
response.data && setPreviousSubmissions(response.data);
4849
})
4950
.catch((error) => {
5051
console.error('Error fetching previous submissions:', error);
5152
});
53+
54+
return () => {
55+
// Abort the request when the component unmounts or when a dependency changes
56+
controller.abort();
57+
};
5258
}, [token]);
5359

5460
return (

global/hooks/useEnvironmentalData/index.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ const useEnvironmentalData = (origin: string) => {
5252
method = 'GET',
5353
headers,
5454
body,
55+
signal,
5556
}: {
5657
url: string;
5758
headers?: HeadersInit;
5859
method: string;
5960
body?: BodyInit;
61+
signal?: AbortSignal;
6062
}) => {
6163
setAwaitingResponse(true);
6264

@@ -65,6 +67,7 @@ const useEnvironmentalData = (origin: string) => {
6567
headers,
6668
method,
6769
body,
70+
signal,
6871
});
6972
const stream = response?.body;
7073
return stream?.constructor?.name === 'ReadableStream'
@@ -85,7 +88,10 @@ const useEnvironmentalData = (origin: string) => {
8588
}
8689
};
8790

88-
const commitSubmission = async (id: string): Promise<CommitSubmissionResult> => {
91+
const commitSubmission = async (
92+
id: string,
93+
{ signal }: { signal?: AbortSignal } = {},
94+
): Promise<CommitSubmissionResult> => {
8995
return handleRequest({
9096
url: urlJoin(
9197
NEXT_PUBLIC_ENVIRONMENTAL_SUBMISSION_API_URL,
@@ -96,6 +102,7 @@ const useEnvironmentalData = (origin: string) => {
96102
id,
97103
),
98104
method: 'POST',
105+
signal,
99106
});
100107
};
101108

@@ -104,14 +111,20 @@ const useEnvironmentalData = (origin: string) => {
104111
* @param id
105112
* @returns
106113
*/
107-
const fetchSubmissionById = async (id: string): Promise<Submission> => {
114+
const fetchSubmissionById = async (
115+
id: string,
116+
{ signal }: { signal?: AbortSignal } = {},
117+
): Promise<Submission> => {
108118
return handleRequest({
109119
url: urlJoin(NEXT_PUBLIC_ENVIRONMENTAL_SUBMISSION_API_URL, 'submission', id),
110120
method: 'GET',
121+
signal,
111122
});
112123
};
113124

114-
const fetchPreviousSubmissions = async (): Promise<{ data: DataRecord[] }> => {
125+
const fetchPreviousSubmissions = async ({ signal }: { signal?: AbortSignal } = {}): Promise<{
126+
data: DataRecord[];
127+
}> => {
115128
const response = await handleRequest({
116129
url: urlJoin(
117130
NEXT_PUBLIC_ENVIRONMENTAL_SUBMISSION_API_URL,
@@ -120,6 +133,7 @@ const useEnvironmentalData = (origin: string) => {
120133
NEXT_PUBLIC_ENVIRONMENTAL_SUBMISSION_CATEGORY_ID,
121134
),
122135
method: 'GET',
136+
signal,
123137
});
124138

125139
return { data: response.records };
@@ -181,7 +195,11 @@ const useEnvironmentalData = (origin: string) => {
181195
* @param records
182196
* @returns
183197
*/
184-
const getAnalysisIds = async (organization: string, records: UploadData[]) => {
198+
const getAnalysisIds = async (
199+
organization: string,
200+
records: UploadData[],
201+
{ signal }: { signal?: AbortSignal } = {},
202+
) => {
185203
// Construct query parameters
186204
const queryParams = new URLSearchParams({
187205
entityName: 'sample',
@@ -217,6 +235,7 @@ const useEnvironmentalData = (origin: string) => {
217235
method: 'POST',
218236
headers: { 'Content-Type': 'application/json' },
219237
body: JSON.stringify(sqonFilter),
238+
signal,
220239
});
221240

222241
if (!queryResponse.records) {

0 commit comments

Comments
 (0)