Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC to use AbortSignal to cancel an ongoing download #296

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gramjs/client/TelegramClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,8 @@ export class TelegramClient extends TelegramBaseClient {
messageOrMedia,
downloadParams.outputFile,
downloadParams.thumb,
downloadParams.progressCallback
downloadParams.progressCallback,
downloadParams.abortSignal
);
}

Expand Down
18 changes: 15 additions & 3 deletions gramjs/client/downloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export interface DownloadFileParamsV2 {
/** */

msgData?: [EntityLike, number];

abortSignal?: AbortSignal;
}

/**
Expand Down Expand Up @@ -399,6 +401,7 @@ export async function downloadFileV2(
progressCallback = undefined,
dcId = undefined,
msgData = undefined,
abortSignal = undefined
}: DownloadFileParamsV2
) {
if (!partSizeKb) {
Expand All @@ -423,6 +426,7 @@ export async function downloadFileV2(
dcId: dcId,
msgData: msgData,
})) {
if (abortSignal?.aborted) throw new Error("USER_ABORTED")
await writer.write(chunk);
if (progressCallback) {
await progressCallback(downloaded, fileSize || 0);
Expand Down Expand Up @@ -499,6 +503,10 @@ export interface DownloadMediaInterface {
* ``(received bytes, total)``.
*/
progressCallback?: ProgressCallback;
/**
* This is used to abort/cancel the ongoing download
*/
abortSignal?: AbortSignal
}

/** @hidden */
Expand All @@ -507,7 +515,8 @@ export async function downloadMedia(
messageOrMedia: Api.Message | Api.TypeMessageMedia,
outputFile?: OutFile,
thumb?: number | Api.TypePhotoSize,
progressCallback?: ProgressCallback
progressCallback?: ProgressCallback,
abortSignal?: AbortSignal
): Promise<Buffer | string | undefined> {
/*
Downloading large documents may be slow enough to require a new file reference
Expand Down Expand Up @@ -556,7 +565,8 @@ export async function downloadMedia(
date,
thumb,
progressCallback,
msgData
msgData,
abortSignal
);
} else if (media instanceof Api.MessageMediaContact) {
return _downloadContact(client, media, {});
Expand All @@ -578,7 +588,8 @@ export async function _downloadDocument(
date: number,
thumb?: number | string | Api.TypePhotoSize,
progressCallback?: ProgressCallback,
msgData?: [EntityLike, number]
msgData?: [EntityLike, number],
abortSignal?: AbortSignal
): Promise<Buffer | string | undefined> {
if (doc instanceof Api.MessageMediaDocument) {
if (!doc.document) {
Expand Down Expand Up @@ -620,6 +631,7 @@ export async function _downloadDocument(
fileSize: size && "size" in size ? size.size : doc.size,
progressCallback: progressCallback,
msgData: msgData,
abortSignal: abortSignal
}
);
}
Expand Down