Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit d2183fc

Browse files
committed
Add Android upload and download progress debounce mechanism #140
1 parent b239dfa commit d2183fc

File tree

7 files changed

+69
-22
lines changed

7 files changed

+69
-22
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,15 @@ public void slice(String src, String dest, int start, int end, Promise promise)
236236
}
237237

238238
@ReactMethod
239-
public void enableProgressReport(String taskId) {
240-
RNFetchBlobReq.progressReport.put(taskId, true);
239+
public void enableProgressReport(String taskId, int interval, int count) {
240+
RNFetchBlobProgressConfig config = new RNFetchBlobProgressConfig(true, interval, count, RNFetchBlobProgressConfig.ReportType.Download);
241+
RNFetchBlobReq.progressReport.put(taskId, config);
241242
}
242243

243244
@ReactMethod
244-
public void enableUploadProgressReport(String taskId) {
245-
RNFetchBlobReq.uploadProgressReport.put(taskId, true);
245+
public void enableUploadProgressReport(String taskId, int interval, int count) {
246+
RNFetchBlobProgressConfig config = new RNFetchBlobProgressConfig(true, interval, count, RNFetchBlobProgressConfig.ReportType.Upload);
247+
RNFetchBlobReq.uploadProgressReport.put(taskId, config);
246248
}
247249

248250
@ReactMethod

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class RNFetchBlobBody extends RequestBody{
3131
RNFetchBlobReq.RequestType requestType;
3232
MediaType mime;
3333
File bodyCache;
34+
int reported = 0;
3435
Boolean chunkedEncoding = false;
3536

3637

@@ -370,7 +371,8 @@ public FormField(ReadableMap rawData) {
370371
* @param written
371372
*/
372373
private void emitUploadProgress(int written) {
373-
if(!RNFetchBlobReq.isReportUploadProgress(mTaskId))
374+
RNFetchBlobProgressConfig config = RNFetchBlobReq.getReportUploadProgress(mTaskId);
375+
if(!config.enable)
374376
return;
375377
WritableMap args = Arguments.createMap();
376378
args.putString("taskId", mTaskId);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.RNFetchBlob;
2+
3+
/**
4+
* Created by wkh237 on 2016/9/24.
5+
*/
6+
public class RNFetchBlobProgressConfig {
7+
8+
public boolean shouldReport() {
9+
boolean checkCount = true;
10+
if(count > 0)
11+
checkCount = Math.floor(progress*100/count)> tick;
12+
return (lastTick - System.currentTimeMillis() > interval) && enable && checkCount;
13+
}
14+
15+
public void tick(float progress) {
16+
this.progress = progress;
17+
this.tick ++;
18+
this.lastTick = System.currentTimeMillis();
19+
}
20+
21+
public enum ReportType {
22+
Upload,
23+
Download
24+
};
25+
26+
long lastTick = 0;
27+
float progress = 0;
28+
int tick = 0;
29+
int count = -1;
30+
public int interval = -1;
31+
public boolean enable = false;
32+
public ReportType type = ReportType.Download;
33+
34+
RNFetchBlobProgressConfig(boolean report, int interval, int count, ReportType type) {
35+
this.enable = report;
36+
this.interval = interval;
37+
this.type = type;
38+
this.count = count;
39+
}
40+
41+
}

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ enum ResponseFormat {
7070
}
7171

7272
public static HashMap<String, Call> taskTable = new HashMap<>();
73-
static HashMap<String, Boolean> progressReport = new HashMap<>();
74-
static HashMap<String, Boolean> uploadProgressReport = new HashMap<>();
73+
static HashMap<String, RNFetchBlobProgressConfig> progressReport = new HashMap<>();
74+
static HashMap<String, RNFetchBlobProgressConfig> uploadProgressReport = new HashMap<>();
7575
static ConnectionPool pool = new ConnectionPool();
7676

7777
ReactApplicationContext ctx;
@@ -508,8 +508,8 @@ private void done(Response resp) {
508508
* @param taskId Task ID of the HTTP task.
509509
* @return Task ID of the target task
510510
*/
511-
public static boolean isReportProgress(String taskId) {
512-
if(!progressReport.containsKey(taskId)) return false;
511+
public static RNFetchBlobProgressConfig getReportProgress(String taskId) {
512+
if(!progressReport.containsKey(taskId)) return null;
513513
return progressReport.get(taskId);
514514
}
515515

@@ -518,8 +518,8 @@ public static boolean isReportProgress(String taskId) {
518518
* @param taskId Task ID of the HTTP task.
519519
* @return Task ID of the target task
520520
*/
521-
public static boolean isReportUploadProgress(String taskId) {
522-
if(!uploadProgressReport.containsKey(taskId)) return false;
521+
public static RNFetchBlobProgressConfig getReportUploadProgress(String taskId) {
522+
if(!uploadProgressReport.containsKey(taskId)) return null;
523523
return uploadProgressReport.get(taskId);
524524
}
525525

src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobDefaultResp.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.RNFetchBlob.Response;
22

3-
import com.RNFetchBlob.RNFetchBlob;
43
import com.RNFetchBlob.RNFetchBlobConst;
4+
import com.RNFetchBlob.RNFetchBlobProgressConfig;
55
import com.RNFetchBlob.RNFetchBlobReq;
66
import com.facebook.react.bridge.Arguments;
77
import com.facebook.react.bridge.ReactApplicationContext;
@@ -10,14 +10,10 @@
1010

1111
import java.io.IOException;
1212

13-
import okhttp3.Call;
14-
import okhttp3.Callback;
1513
import okhttp3.MediaType;
16-
import okhttp3.Response;
1714
import okhttp3.ResponseBody;
1815
import okio.Buffer;
1916
import okio.BufferedSource;
20-
import okio.ForwardingSource;
2117
import okio.Okio;
2218
import okio.Source;
2319
import okio.Timeout;
@@ -66,7 +62,9 @@ public long read(Buffer sink, long byteCount) throws IOException {
6662

6763
long read = mOriginalSource.read(sink, byteCount);
6864
bytesRead += read > 0 ? read : 0;
69-
if(RNFetchBlobReq.isReportProgress(mTaskId)) {
65+
RNFetchBlobProgressConfig reportConfig = RNFetchBlobReq.getReportProgress(mTaskId);
66+
if(reportConfig != null && reportConfig.shouldReport()) {
67+
reportConfig.tick(bytesRead/contentLength());
7068
WritableMap args = Arguments.createMap();
7169
args.putString("taskId", mTaskId);
7270
args.putString("written", String.valueOf(bytesRead));

src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.util.Log;
44

55
import com.RNFetchBlob.RNFetchBlobConst;
6+
import com.RNFetchBlob.RNFetchBlobProgressConfig;
67
import com.RNFetchBlob.RNFetchBlobReq;
78
import com.facebook.react.bridge.Arguments;
89
import com.facebook.react.bridge.ReactApplicationContext;
@@ -75,7 +76,9 @@ public long read(Buffer sink, long byteCount) throws IOException {
7576
if(read > 0 ) {
7677
ofStream.write(bytes, 0, (int) read);
7778
}
78-
if(RNFetchBlobReq.isReportProgress(mTaskId)) {
79+
RNFetchBlobProgressConfig reportConfig = RNFetchBlobReq.getReportProgress(mTaskId);
80+
if(reportConfig != null && reportConfig.shouldReport()) {
81+
reportConfig.tick(bytesDownloaded/contentLength());
7982
WritableMap args = Arguments.createMap();
8083
args.putString("taskId", mTaskId);
8184
args.putString("written", String.valueOf(bytesDownloaded));

src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,15 @@ function fetch(...args:any):Promise {
193193

194194
// extend Promise object, add `progress`, `uploadProgress`, and `cancel`
195195
// method for register progress event handler and cancel request.
196-
promise.progress = (fn) => {
196+
// Add second parameter for performance purpose #140
197+
promise.progress = (fn, interval:number=250, count:number=-1) => {
197198
promise.onProgress = fn
198-
RNFetchBlob.enableProgressReport(taskId)
199+
RNFetchBlob.enableProgressReport(taskId, interval, count)
199200
return promise
200201
}
201-
promise.uploadProgress = (fn) => {
202+
promise.uploadProgress = (fn, interval:number=250, count:number=-1) => {
202203
promise.onUploadProgress = fn
203-
RNFetchBlob.enableUploadProgressReport(taskId)
204+
RNFetchBlob.enableUploadProgressReport(taskId, interval, count)
204205
return promise
205206
}
206207
promise.stateChange = (fn) => {

0 commit comments

Comments
 (0)