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

Conversation

mycodedstuff
Copy link

@mycodedstuff mycodedstuff commented Apr 5, 2022

Currently gramjs doesn't provide any way to cancel/abort an ongoing download.
I implemented a simple system which uses AbortController and AbortSignal to break the loop which fetches the chunks.

This is a POC so that we can discuss if there are any caveats in this idea. This can be simillary implemented for all other kinds of downloads (document, video, profile picture, etc).

Example:

const abortController = new AbortController()
const buffer = await msg.downloadMedia({
  abortSignal: abortController.signal
})

// In some place else we can call
abortController.abort()

This raises an USER_ABORTED error from downloadMedia which can be handled by the client.

Note: This implementation is only checked with Nodejs, I haven't tested it in browsers.

@painor
Copy link
Member

painor commented Apr 6, 2022

hmm I'm not sure.
Currently you can do the same thing with a progress callback. Just throw something inside the progress callback and it will stop the function.

downloadMedia and downloadFile use iterDownload

        for await (const chunk of iterDownload(client, {
            file: inputLocation,
            requestSize: partSize,
            dcId: dcId,
            msgData: msgData,
        })) {
            await writer.write(chunk);
            if (progressCallback) {
                await progressCallback(downloaded, fileSize || 0);
            }
            downloaded += chunk.length;
        }

so it's preferable if when the users wants to abort/stop/resume that they use iterDownload since they can stop it whenever they want

@mycodedstuff
Copy link
Author

Currently you can do the same thing with a progress callback. Just throw something inside the progress callback and it will stop the function.

Yeah that's true. I didn't consider using the callback for that purpose.
IMO, it's a trade off between ease and flexibility. iterDownload gives you the flexibility to add extra functionality where as having something built in gives ease to developers using the library.

At the end it's your call. We can close this PR. But this discussion can atleast help others figure things out.

@painor
Copy link
Member

painor commented Apr 6, 2022

I'm open to having a pause/stop/resume functionality

We could still use the controller thing and make it a downloadController instead of abort.
.pause = pauses until user calls .resume
.stop = stops the download by simply doing a "break" This will return the downloaded data so far
.resume = resumes a pause download

If you want you can do it or I can build on top of this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants