瀏覽代碼

Add IOS readStream event frequency limitation

Ben Hsieh 8 年之前
父節點
當前提交
05e84da25b
共有 2 個檔案被更改,包括 24 行新增8 行删除
  1. 12
    5
      src/ios/RNFetchBlob/RNFetchBlob.m
  2. 12
    3
      src/ios/RNFetchBlobFS.m

+ 12
- 5
src/ios/RNFetchBlob/RNFetchBlob.m 查看文件

@@ -15,6 +15,8 @@
15 15
 
16 16
 
17 17
 RCTBridge * bridgeRef;
18
+dispatch_queue_t commonTaskQueue;
19
+dispatch_queue_t fsQueue;
18 20
 
19 21
 ////////////////////////////////////////
20 22
 //
@@ -30,7 +32,9 @@ RCTBridge * bridgeRef;
30 32
 @synthesize bridge = _bridge;
31 33
 
32 34
 - (dispatch_queue_t) methodQueue {
33
-    return dispatch_queue_create("RNFetchBlob.queue", DISPATCH_QUEUE_SERIAL);
35
+    if(commonTaskQueue == nil)
36
+        commonTaskQueue = dispatch_queue_create("RNFetchBlob.queue", DISPATCH_QUEUE_SERIAL);
37
+    return commonTaskQueue;
34 38
 }
35 39
 
36 40
 + (RCTBridge *)getRCTBridge
@@ -43,6 +47,10 @@ RCT_EXPORT_MODULE();
43 47
 - (id) init {
44 48
     self = [super init];
45 49
     self.filePathPrefix = FILE_PREFIX;
50
+    if(commonTaskQueue == nil)
51
+        commonTaskQueue = dispatch_queue_create("RNFetchBlob.queue", DISPATCH_QUEUE_SERIAL);
52
+    if(fsQueue == nil)
53
+        fsQueue = dispatch_queue_create("RNFetchBlob.fs.queue", DISPATCH_QUEUE_SERIAL);
46 54
     BOOL isDir;
47 55
     // if temp folder not exists, create one
48 56
     if(![[NSFileManager defaultManager] fileExistsAtPath: [RNFetchBlobFS getTempPath] isDirectory:&isDir]) {
@@ -376,8 +384,6 @@ RCT_EXPORT_METHOD(readFile:(NSString *)path encoding:(NSString *)encoding resolv
376 384
 #pragma mark - fs.readStream
377 385
 RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int)bufferSize tick:(int)tick streamId:(NSString *)streamId
378 386
 {
379
-
380
-//    RNFetchBlobFS *fileStream = [[RNFetchBlobFS alloc] initWithBridgeRef:self.bridge];
381 387
     if(bufferSize == nil) {
382 388
         if([[encoding lowercaseString] isEqualToString:@"base64"])
383 389
             bufferSize = 4095;
@@ -385,8 +391,9 @@ RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding
385 391
             bufferSize = 4096;
386 392
     }
387 393
     
388
-//    [fileStream readWithPath:path useEncoding:encoding bufferSize:bufferSize];
389
-    [RNFetchBlobFS readStream:path encoding:encoding bufferSize:bufferSize tick:tick streamId:streamId bridgeRef:_bridge];
394
+    dispatch_async(fsQueue, ^{
395
+        [RNFetchBlobFS readStream:path encoding:encoding bufferSize:bufferSize tick:tick streamId:streamId bridgeRef:_bridge];
396
+    });
390 397
 })
391 398
 
392 399
 #pragma mark - fs.getEnvionmentDirs

+ 12
- 3
src/ios/RNFetchBlobFS.m 查看文件

@@ -128,9 +128,10 @@ NSMutableDictionary *fileStreams = nil;
128 128
 {
129 129
     [[self class] getPathFromUri:uri completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
130 130
     
131
-        RCTEventDispatcher * event = bridgeRef.eventDispatcher;
132
-        int read = 0;
133
-        int chunkSize = bufferSize;
131
+        __block RCTEventDispatcher * event = bridgeRef.eventDispatcher;
132
+        __block int read = 0;
133
+        __block int backoff = tick *1000;
134
+        __block int chunkSize = bufferSize;
134 135
         // allocate buffer in heap instead of stack
135 136
         uint8_t * buffer;
136 137
         @try
@@ -151,6 +152,10 @@ NSMutableDictionary *fileStreams = nil;
151 152
                 while((read = [stream read:buffer maxLength:bufferSize]) > 0)
152 153
                 {
153 154
                     [[self class] emitDataChunks:[NSData dataWithBytes:buffer length:read] encoding:encoding streamId:streamId event:event];
155
+                    if(tick > 0)
156
+                    {
157
+                        usleep(backoff);
158
+                    }
154 159
                 }
155 160
                 [stream close];
156 161
             }
@@ -162,6 +167,10 @@ NSMutableDictionary *fileStreams = nil;
162 167
                 {
163 168
                     cursor += read;
164 169
                     [[self class] emitDataChunks:[NSData dataWithBytes:buffer length:read] encoding:encoding streamId:streamId event:event];
170
+                    if(tick > 0)
171
+                    {
172
+                        usleep(backoff);
173
+                    }
165 174
                 }
166 175
             }
167 176
             else