Repository navigation
feat: cancel downloads per caller without breaking FileService - #548
Open
ben-milanko wants to merge 1 commit into
Open
ben-milanko wants to merge 1 commit into
ben-milanko wants to merge 1 commit into
Conversation
Adds an optional CancellationToken to getFileStream, getSingleFile, downloadFile and ImageCacheManager.getImageFile. - FileService.get and BaseCacheManager keep their signatures. The token reaches the file service as CancellationToken.current, a zone value set with runScoped; HttpFileService aborts its request on it, and a service that ignores it is abandoned with its late body released. flutter_cache_manager_firebase compiles and passes unchanged. - Callers of the same url share one download through SharedCancellation, which gives each its own stream; the download (and a shared resize) stops only once every caller has cancelled, and a caller without a token keeps it alive. - A cancelled or failed download deletes its partial file. Downloads go to a fresh path, so the previously cached file stays valid. - The sink is closed when the response stream errors. - Tests cover mid-stream cancellation, a stream error, two callers on one url in both orders, every caller cancelling, a queued request, an already-cancelled token, a service that ignores the token, and the token through getFileStream, getSingleFile, downloadFile and getImageFile with and without resizing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)
Feature: per-caller request cancellation. This is the second of the two pull requests suggested in the review of #504. Web persistence is in a separate PR.
A download cannot be cancelled once requested. A widget that is disposed, or scrolled away, still waits for its download to finish.
🆕 What is the new behavior (if this is a feature change)?
getFileStream,getSingleFile,downloadFileandImageCacheManager.getImageFiletake an optionalCancellationToken. Cancelling ends that call with aCancelledException..pipe(sink)did.💥 Does this PR introduce a breaking change?
No.
FileService.getandBaseCacheManagerkeep their exact signatures. The token reaches the file service asCancellationToken.current, a zone value.HttpFileServiceaborts its request on it, and a customFileServicethat ignores it is simply abandoned and its late response body released.flutter_cache_manager_firebaseanalyzes and passes its tests unchanged against this branch, without a dependency override.🐛 Recommendations for testing
flutter test: 168 tests pass, including a newtest/cancellation_test.dart. It covers mid-stream cancellation, a response stream error, two callers on one url (in both orders), every caller cancelling, a queued request, an already-cancelled token, a service that ignores the token, and the token passing throughgetFileStream,getSingleFile,downloadFileandgetImageFile, with and without resizing.test/mock.mocks.dartis regenerated for the new optional parameter onWebHelper.downloadFile.flutter build web --wasminexample/builds.📝 Links to relevant issues/docs
Follows the review of #504.
🤔 Checklist before submitting
Both this PR and the web persistence PR bump to 3.6.0. Whichever merges second will move to the next minor version when rebased.