Skip to content

Commit

Permalink
Improve abort function for async operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Sep 3, 2024
1 parent cb8db2e commit 5d6325c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
12 changes: 9 additions & 3 deletions lib/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ export class HttpClient implements IRangeRequestClient {

public resolvedUrl?: string;
private readonly config: HttpClientConfig;
private readonly abortController = new AbortController();

constructor(private url: string, private abortSignal: AbortSignal, config?: HttpClientConfig) {
constructor(private url: string, config?: HttpClientConfig) {
this.config = DEFAULT_CONFIG;
Object.assign(this.config, config);
}

public async getHeadInfo(): Promise<IHeadRequestInfo> {
const response = new ResponseInfo(await fetch(this.url, {method: 'HEAD', signal: this.abortSignal}));
const response = new ResponseInfo(await fetch(this.url, {method: 'HEAD', signal: this.abortController.signal}));
if (this.config.resolveUrl) this.resolvedUrl = response.response.url;
return response.toRangeRequestResponse();
}
Expand All @@ -44,12 +45,17 @@ export class HttpClient implements IRangeRequestClient {

const headers = new Headers();

const response = new ResponseInfo(await fetch(this.resolvedUrl || this.url, {method, headers, signal: this.abortSignal}));
const response = new ResponseInfo(await fetch(this.resolvedUrl || this.url, {method, headers, signal: this.abortController.signal}));
if (response.response.ok) {
if (this.config.resolveUrl) this.resolvedUrl = response.response.url;
return response.toRangeRequestResponse();
}
throw new Error(`Unexpected HTTP response status=${response.response.status}`);
}

public abort(): void {
debug('abort');
this.abortController.abort();
}

}
5 changes: 2 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function makeTokenizer(
tokenizerConfig?: rangeTokenizer.IRangeRequestConfig,
httpClientConfig?: HttpClientConfig
): Promise<ITokenizer> {
const abortController = tokenizerConfig?.abortController ?? new AbortController();
const httpClient = new HttpClient(url, abortController.signal, httpClientConfig);
return rangeTokenizer.tokenizer(httpClient, {...tokenizerConfig, abortController: abortController});
const httpClient = new HttpClient(url, httpClientConfig);
return rangeTokenizer.tokenizer(httpClient, tokenizerConfig);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@tokenizer/range": "^0.9.0",
"@tokenizer/range": "^0.10.0",
"debug": "^4.3.6",
"strtok3": "^9.0.0"
},
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ __metadata:
dependencies:
"@biomejs/biome": "npm:^1.8.3"
"@music-metadata/test-audio": "npm:^0.1.0"
"@tokenizer/range": "npm:^0.9.0"
"@tokenizer/range": "npm:^0.10.0"
"@types/chai": "npm:^4.3.16"
"@types/debug": "npm:^4.1.12"
"@types/mocha": "npm:^10.0.7"
Expand All @@ -334,13 +334,13 @@ __metadata:
languageName: unknown
linkType: soft

"@tokenizer/range@npm:^0.9.0":
version: 0.9.0
resolution: "@tokenizer/range@npm:0.9.0"
"@tokenizer/range@npm:^0.10.0":
version: 0.10.0
resolution: "@tokenizer/range@npm:0.10.0"
dependencies:
debug: "npm:^4.3.6"
strtok3: "npm:^9.0.0"
checksum: 10c0/394a3133d80d82b0bfee0f39e26de4689378945019ac61be8b8bcdbd4ef9f2cd047fc06f067ae67f9e2af795b063e30e0fe2f9a9a650b201adf610271f005f95
checksum: 10c0/3e036c79f20cd3a88dd58fa249c5d2ce1d986d85a4678f97d40db67c3eba4a2e9b5eaf70869b78919e61e13573a1209de96c421228d39b0025a540df606466c7
languageName: node
linkType: hard

Expand Down

0 comments on commit 5d6325c

Please sign in to comment.