Skip to content

Commit

Permalink
Add function to abort async operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Sep 2, 2024
1 parent 1a27c6f commit a30c7ee
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/AbstractTokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export abstract class AbstractTokenizer implements ITokenizer {
protected constructor(options?: ITokenizerOptions) {
this.fileInfo = options?.fileInfo ?? {};
this.onClose = options?.onClose;
if (options?.abortSignal) {
options.abortSignal.addEventListener('abort', () => {
this.abort();
})
}
}

/**
Expand Down Expand Up @@ -146,4 +151,8 @@ export abstract class AbstractTokenizer implements ITokenizer {
position: this.position
};
}

public abort(): Promise<void> {
return Promise.resolve(); // Ignore abort signal
}
}
4 changes: 4 additions & 0 deletions lib/ReadStreamTokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,8 @@ export class ReadStreamTokenizer extends AbstractTokenizer {
}
return totBytesRead;
}

public abort(): Promise<void> {
return this.streamReader.abort();
}
}
12 changes: 12 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ export interface ITokenizer {
* It does not close the stream for StreamReader, but is does close the file-descriptor.
*/
close(): Promise<void>;

/**
* Abort pending asynchronous operations
*/
abort(): Promise<void>;
}

export type OnClose = () => Promise<void>;
Expand All @@ -127,8 +132,15 @@ export interface ITokenizerOptions {
* Pass additional file information to the tokenizer
*/
fileInfo?: IFileInfo;

/**
* On tokenizer close handler
*/
onClose?: OnClose;

/**
* Pass `AbortSignal` which can stop active async operations
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
*/
abortSignal?: AbortSignal;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
},
"dependencies": {
"@tokenizer/token": "^0.3.0",
"peek-readable": "^5.1.4"
"peek-readable": "^5.2.0"
},
"keywords": [
"tokenizer",
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2829,10 +2829,10 @@ __metadata:
languageName: node
linkType: hard

"peek-readable@npm:^5.1.4":
version: 5.1.4
resolution: "peek-readable@npm:5.1.4"
checksum: 10c0/19015142b2f2556bec8b51c53111209bd6d774181df26dbf68908495819e35f4103e83915aa7ea35505d2a65d2712f2ab58f36b329cf61219c012621e65211b3
"peek-readable@npm:^5.2.0":
version: 5.2.0
resolution: "peek-readable@npm:5.2.0"
checksum: 10c0/7647d56786c94fc7f5f39923ef5f6ed1da7be92a2c221b743db74f1517821b1e944cc965d8f018a7e6984d969b6ced8a0c421aece7996c0b1981eac9daa4c323
languageName: node
linkType: hard

Expand Down Expand Up @@ -3591,7 +3591,7 @@ __metadata:
chai: "npm:^5.1.1"
del-cli: "npm:^5.1.0"
mocha: "npm:^10.7.3"
peek-readable: "npm:^5.1.4"
peek-readable: "npm:^5.2.0"
remark-cli: "npm:^12.0.1"
remark-preset-lint-recommended: "npm:^7.0.0"
token-types: "npm:^6.0.0"
Expand Down

0 comments on commit a30c7ee

Please sign in to comment.