Browse Source

Add Android upload and download progress debounce mechanism #140

Ben Hsieh 7 years ago
parent
commit
d2183fc08a

+ 6
- 4
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

@@ -236,13 +236,15 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
236 236
     }
237 237
 
238 238
     @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);
241 242
     }
242 243
 
243 244
     @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);
246 248
     }
247 249
 
248 250
     @ReactMethod

+ 3
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java View File

@@ -31,6 +31,7 @@ public class RNFetchBlobBody extends RequestBody{
31 31
     RNFetchBlobReq.RequestType requestType;
32 32
     MediaType mime;
33 33
     File bodyCache;
34
+    int reported = 0;
34 35
     Boolean chunkedEncoding = false;
35 36
 
36 37
 
@@ -370,7 +371,8 @@ public class RNFetchBlobBody extends RequestBody{
370 371
      * @param written
371 372
      */
372 373
     private void emitUploadProgress(int written) {
373
-        if(!RNFetchBlobReq.isReportUploadProgress(mTaskId))
374
+        RNFetchBlobProgressConfig config = RNFetchBlobReq.getReportUploadProgress(mTaskId);
375
+        if(!config.enable)
374 376
             return;
375 377
         WritableMap args = Arguments.createMap();
376 378
         args.putString("taskId", mTaskId);

+ 41
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobProgressConfig.java View File

@@ -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
+}

+ 6
- 6
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

@@ -70,8 +70,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
70 70
     }
71 71
 
72 72
     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<>();
75 75
     static ConnectionPool pool = new ConnectionPool();
76 76
 
77 77
     ReactApplicationContext ctx;
@@ -508,8 +508,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
508 508
      * @param taskId Task ID of the HTTP task.
509 509
      * @return Task ID of the target task
510 510
      */
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;
513 513
         return progressReport.get(taskId);
514 514
     }
515 515
 
@@ -518,8 +518,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
518 518
      * @param taskId Task ID of the HTTP task.
519 519
      * @return Task ID of the target task
520 520
      */
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;
523 523
         return uploadProgressReport.get(taskId);
524 524
     }
525 525
 

+ 4
- 6
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobDefaultResp.java View File

@@ -1,7 +1,7 @@
1 1
 package com.RNFetchBlob.Response;
2 2
 
3
-import com.RNFetchBlob.RNFetchBlob;
4 3
 import com.RNFetchBlob.RNFetchBlobConst;
4
+import com.RNFetchBlob.RNFetchBlobProgressConfig;
5 5
 import com.RNFetchBlob.RNFetchBlobReq;
6 6
 import com.facebook.react.bridge.Arguments;
7 7
 import com.facebook.react.bridge.ReactApplicationContext;
@@ -10,14 +10,10 @@ import com.facebook.react.modules.core.DeviceEventManagerModule;
10 10
 
11 11
 import java.io.IOException;
12 12
 
13
-import okhttp3.Call;
14
-import okhttp3.Callback;
15 13
 import okhttp3.MediaType;
16
-import okhttp3.Response;
17 14
 import okhttp3.ResponseBody;
18 15
 import okio.Buffer;
19 16
 import okio.BufferedSource;
20
-import okio.ForwardingSource;
21 17
 import okio.Okio;
22 18
 import okio.Source;
23 19
 import okio.Timeout;
@@ -66,7 +62,9 @@ public class RNFetchBlobDefaultResp extends ResponseBody {
66 62
 
67 63
             long read =  mOriginalSource.read(sink, byteCount);
68 64
             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());
70 68
                 WritableMap args = Arguments.createMap();
71 69
                 args.putString("taskId", mTaskId);
72 70
                 args.putString("written", String.valueOf(bytesRead));

+ 4
- 1
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java View File

@@ -3,6 +3,7 @@ package com.RNFetchBlob.Response;
3 3
 import android.util.Log;
4 4
 
5 5
 import com.RNFetchBlob.RNFetchBlobConst;
6
+import com.RNFetchBlob.RNFetchBlobProgressConfig;
6 7
 import com.RNFetchBlob.RNFetchBlobReq;
7 8
 import com.facebook.react.bridge.Arguments;
8 9
 import com.facebook.react.bridge.ReactApplicationContext;
@@ -75,7 +76,9 @@ public class RNFetchBlobFileResp extends ResponseBody {
75 76
             if(read > 0 ) {
76 77
                 ofStream.write(bytes, 0, (int) read);
77 78
             }
78
-            if(RNFetchBlobReq.isReportProgress(mTaskId)) {
79
+            RNFetchBlobProgressConfig reportConfig = RNFetchBlobReq.getReportProgress(mTaskId);
80
+            if(reportConfig != null && reportConfig.shouldReport()) {
81
+                reportConfig.tick(bytesDownloaded/contentLength());
79 82
                 WritableMap args = Arguments.createMap();
80 83
                 args.putString("taskId", mTaskId);
81 84
                 args.putString("written", String.valueOf(bytesDownloaded));

+ 5
- 4
src/index.js View File

@@ -193,14 +193,15 @@ function fetch(...args:any):Promise {
193 193
 
194 194
   // extend Promise object, add `progress`, `uploadProgress`, and `cancel`
195 195
   // 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) => {
197 198
     promise.onProgress = fn
198
-    RNFetchBlob.enableProgressReport(taskId)
199
+    RNFetchBlob.enableProgressReport(taskId, interval, count)
199 200
     return promise
200 201
   }
201
-  promise.uploadProgress = (fn) => {
202
+  promise.uploadProgress = (fn, interval:number=250, count:number=-1) => {
202 203
     promise.onUploadProgress = fn
203
-    RNFetchBlob.enableUploadProgressReport(taskId)
204
+    RNFetchBlob.enableUploadProgressReport(taskId, interval, count)
204 205
     return promise
205 206
   }
206 207
   promise.stateChange = (fn) => {