fix(android): use float division for download progress ratio - #482
Open
armamini wants to merge 1 commit into
Open
fix(android): use float division for download progress ratio#482armamini wants to merge 1 commit into
armamini wants to merge 1 commit into
Conversation
was always receiving 0 for in-flight downloads because the ratio was computed with long/long integer division, which truncates to 0 until the transfer completes. The count-based throttle in ReactNativeBlobUtilProgressConfig is gated on , so that guard never became true and stayed at its default of . The result is that was silently ignored on Android and every consumer fell back to interval-only reporting. Cast to float at the three sites that compute the ratio: - ReactNativeBlobUtilFileResp: download-to-file responses - ReactNativeBlobUtilDefaultResp: in-memory responses - ReactNativeBlobUtilReq: DownloadManager polling loop DefaultResp additionally guarded against chunked responses, where contentLength() is -1 and the old expression produced a negative ratio. Both the old and new code take the interval-only path there, so this is a clarification rather than a behaviour change. Default usage (, count -1) is unaffected.
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.
Problem
On Android, the
countoption of.progress()is silently ignored for downloads:No matter what
countis set to, callbacks arrive on the interval schedule only. iOS honourscountcorrectly, so the same JS produces different callback cadence per platform.Root cause
Three sites compute the progress ratio using
long / long, which is integer division. The result truncates to0for the entire transfer and only reaches1on the final read:Assigning to a
floathappens after the truncation, so the cast never helps.ReactNativeBlobUtilProgressConfig.shouldReportgates the count throttle on the ratio being positive:Since
progressis always0,count > 0 && progress > 0is never true,checkCountkeeps its default oftrue, and the count bucket is never consulted. Reporting degrades to interval-only.The upload path already gets this right —
ReactNativeBlobUtilBody.java:438readsconfig.shouldReport((float) written / contentLength)— so the intended semantics are unambiguous; the three download sites simply missed the cast.Fix
Cast to
floatbefore dividing, at all three sites:Response/ReactNativeBlobUtilFileResp.java— download-to-file responsesResponse/ReactNativeBlobUtilDefaultResp.java— in-memory responsesReactNativeBlobUtilReq.java— DownloadManager polling loopDefaultRespadditionally guards the chunked case, wherecontentLength()is-1and the old expression yielded a negative ratio. Old and new code both take the interval-only path there, so this is a clarification rather than a behaviour change.Compatibility
.progress(fn)(count-1).progress({ interval: N }, fn).progress({ count: N }, fn)No public API change. iOS and Windows are untouched.
Verification
Download a file large enough to span many reads with
.progress({ count: 10, interval: 0 }, fn)and count the callbacks: previously they tracked the interval only; now they arrive at roughly each 10% boundary, andwrittenadvances monotonically toContent-Length.