Skip to content

Commit b86362b

Browse files
committed
Use component scoped cancel signals
1 parent 631d7f8 commit b86362b

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

src/ServerAPI.mjs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ export class ServerAPI extends APIInterface {
55
constructor(apiUrl) {
66
super();
77
this.apiUrl = apiUrl;
8-
this.fetchCanceler = new AbortController();
9-
this.cancelSignal = this.fetchCanceler.signal;
108
}
119

12-
async getChunkedData(viewTarget) {
10+
// Each function takes a cancelSignal to cancel the fetch request if we will unmount component
11+
12+
async getChunkedData(viewTarget, cancelSignal) {
1313
const json = await fetchAndParse(`${this.apiUrl}/getChunkedData`, {
14-
signal: this.cancelSignal, // (so we can cancel the fetch request if we will unmount component)
14+
signal: cancelSignal,
1515
method: "POST",
1616
headers: {
1717
"Content-Type": "application/json",
@@ -21,9 +21,9 @@ export class ServerAPI extends APIInterface {
2121
return json;
2222
}
2323

24-
async getFilenames() {
24+
async getFilenames(cancelSignal) {
2525
const json = await fetchAndParse(`${this.apiUrl}/getFilenames`, {
26-
signal: this.cancelSignal, // (so we can cancel the fetch request if we will unmount component)
26+
signal: cancelSignal,
2727
method: "GET",
2828
headers: {
2929
"Content-Type": "application/json",
@@ -32,9 +32,9 @@ export class ServerAPI extends APIInterface {
3232
return json;
3333
}
3434

35-
async getBedRegions(bedFile) {
35+
async getBedRegions(bedFile, cancelSignal) {
3636
const json = await fetchAndParse(`${this.apiUrl}/getBedRegions`, {
37-
signal: this.cancelSignal, // (so we can cancel the fetch request if we will unmount component)
37+
signal: cancelSignal,
3838
method: "POST",
3939
headers: {
4040
"Content-Type": "application/json",
@@ -44,9 +44,9 @@ export class ServerAPI extends APIInterface {
4444
return json;
4545
}
4646

47-
async getPathNames(graphFile) {
47+
async getPathNames(graphFile, cancelSignal) {
4848
const json = await fetchAndParse(`${this.apiUrl}/getPathNames`, {
49-
signal: this.cancelSignal, // (so we can cancel the fetch request if we will unmount component)
49+
signal: cancelSignal,
5050
method: "POST",
5151
headers: {
5252
"Content-Type": "application/json",
@@ -56,8 +56,9 @@ export class ServerAPI extends APIInterface {
5656
return json
5757
}
5858

59-
async getChunkTracks(bedFile, chunk) {
59+
async getChunkTracks(bedFile, chunk, cancelSignal) {
6060
const json = await fetchAndParse(`${this.apiUrl}/getChunkTracks`, {
61+
signal: cancelSignal,
6162
method: "POST",
6263
headers: {
6364
"Content-Type": "application/json",
@@ -66,10 +67,6 @@ export class ServerAPI extends APIInterface {
6667
});
6768
return json;
6869
}
69-
70-
abortRequests() {
71-
this.fetchCanceler.abort();
72-
}
7370
}
7471

7572
export default ServerAPI;

src/components/HeaderForm.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,19 @@ function viewTargetsEqual(currViewTarget, nextViewTarget) {
169169
class HeaderForm extends Component {
170170
state = EMPTY_STATE;
171171
componentDidMount() {
172-
this.fetchCanceled = false;
172+
this.fetchCanceler = new AbortController();
173+
this.cancelSignal = this.fetchCanceler.signal;
173174
this.api = this.props.APIInterface;
174175
this.initState();
175176
this.getMountedFilenames();
176177
this.setUpWebsocket();
177178
}
178179
componentWillUnmount() {
179180
// Cancel the requests since we may have long running requests pending.
180-
this.api.abortRequests();
181-
this.fetchCanceled = true;
181+
this.fetchCanceler.abort();
182182
}
183183
handleFetchError(error, message) {
184-
if (!this.fetchCanceled) {
184+
if (!this.cancelSignal.aborted) {
185185
console.log(message, error.name, error.message);
186186
this.setState({ error: error });
187187
} else {
@@ -298,7 +298,7 @@ class HeaderForm extends Component {
298298
getMountedFilenames = async () => {
299299
this.setState({ error: null });
300300
try {
301-
const json = await this.api.getFilenames();
301+
const json = await this.api.getFilenames(this.cancelSignal);
302302
if (!json.files || json.files.length === 0) {
303303
// We did not get back a graph, only (possibly) an error.
304304
const error =
@@ -349,7 +349,7 @@ class HeaderForm extends Component {
349349
getBedRegions = async (bedFile) => {
350350
this.setState({ error: null });
351351
try {
352-
const json = await this.api.getBedRegions(bedFile);
352+
const json = await this.api.getBedRegions(bedFile, this.cancelSignal);
353353
// We need to do all our parsing here, if we expect the catch to catch errors.
354354
if (!json.bedRegions || !(json.bedRegions["desc"] instanceof Array)) {
355355
throw new Error(
@@ -379,7 +379,7 @@ class HeaderForm extends Component {
379379
getPathNames = async (graphFile) => {
380380
this.setState({ error: null });
381381
try {
382-
const json = await this.api.getPathNames(graphFile);
382+
const json = await this.api.getPathNames(graphFile, this.cancelSignal);
383383
// We need to do all our parsing here, if we expect the catch to catch errors.
384384
let pathNames = json.pathNames;
385385
if (!(pathNames instanceof Array)) {
@@ -545,7 +545,7 @@ class HeaderForm extends Component {
545545
console.log("New tracks have been applied");
546546
} else if (this.state.bedFile && chunk) {
547547
// Try to retrieve tracks from the server
548-
const json = await this.api.getChunkTracks(this.state.bedFile, chunk);
548+
const json = await this.api.getChunkTracks(this.state.bedFile, chunk, this.cancelSignal);
549549

550550
// Replace tracks if request returns non-falsey value
551551
if (json.tracks) {

src/components/TubeMapContainer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ class TubeMapContainer extends Component {
1717
};
1818

1919
componentDidMount() {
20-
this.fetchCanceled = false;
20+
this.fetchCanceler = new AbortController();
21+
this.cancelSignal = this.fetchCanceler.signal;
2122
this.api = this.props.APIInterface;
2223
this.getRemoteTubeMapData();
2324
}
2425

2526
componentWillUnmount() {
2627
// Cancel the requests since we may have long running requests pending.
27-
this.api.abortRequests();
28-
this.fetchCanceled = true;
28+
this.fetchCanceler.abort();
2929
}
3030

3131
handleFetchError(error, message) {
32-
if (!this.fetchCanceled) {
32+
if (!this.cancelSignal.aborted) {
3333
console.error(message, error);
3434
this.setState({ error: error, isLoading: false });
3535
} else {
@@ -129,7 +129,7 @@ class TubeMapContainer extends Component {
129129
getRemoteTubeMapData = async () => {
130130
this.setState({ isLoading: true, error: null });
131131
try {
132-
const json = await this.api.getChunkedData(this.props.viewTarget);
132+
const json = await this.api.getChunkedData(this.props.viewTarget, this.cancelSignal);
133133
if (json.graph === undefined) {
134134
// We did not get back a graph, even if we didn't get an error either.
135135
const error = "Fetching remote data returned error";

0 commit comments

Comments
 (0)