Browse Source

Add HTTP state change event

Ben Hsieh 8 years ago
parent
commit
21dd265d69
4 changed files with 23 additions and 1 deletions
  1. 7
    0
      src/index.js
  2. 1
    0
      src/ios/RNFetchBlobConst.h
  3. 1
    1
      src/ios/RNFetchBlobConst.m
  4. 14
    0
      src/ios/RNFetchBlobNetwork.m

+ 7
- 0
src/index.js View File

@@ -127,12 +127,19 @@ function fetch(...args:any):Promise {
127 127
       }
128 128
     })
129 129
 
130
+    let stateEvent = emitter.addListener('RNFetchBlobState', (e) => {
131
+      if(e.taskId === taskId && promise.onUploadProgress) {
132
+        promise.onStateChange(e)
133
+      }
134
+    })
135
+
130 136
     let req = RNFetchBlob[nativeMethodName]
131 137
     req(options, taskId, method, url, headers || {}, body, (err, data) => {
132 138
 
133 139
       // task done, remove event listener
134 140
       subscription.remove()
135 141
       subscriptionUpload.remove()
142
+      stateEvent.remove()
136 143
       if(err)
137 144
         reject(new Error(err, data))
138 145
       else {

+ 1
- 0
src/ios/RNFetchBlobConst.h View File

@@ -16,6 +16,7 @@ extern NSString *const MSG_EVENT;
16 16
 extern NSString *const MSG_EVENT_LOG;
17 17
 extern NSString *const MSG_EVENT_WARN;
18 18
 extern NSString *const MSG_EVENT_ERROR;
19
+extern NSString *const EVENT_STATE_CHANGE;
19 20
 
20 21
 extern NSString *const FILE_PREFIX;
21 22
 extern NSString *const ASSET_PREFIX;

+ 1
- 1
src/ios/RNFetchBlobConst.m View File

@@ -20,7 +20,7 @@ extern NSString *const CONFIG_FILE_EXT = @"appendExt";
20 20
 extern NSString *const CONFIG_TRUSTY = @"trusty";
21 21
 extern NSString *const CONFIG_INDICATOR = @"indicator";
22 22
 
23
-
23
+extern NSString *const EVENT_STATE_CHANGE = @"RNFetchBlobState";
24 24
 extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";
25 25
 extern NSString *const MSG_EVENT_LOG = @"log";
26 26
 extern NSString *const MSG_EVENT_WARN = @"warn";

+ 14
- 0
src/ios/RNFetchBlobNetwork.m View File

@@ -135,6 +135,20 @@ NSOperationQueue *taskQueue;
135 135
 - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
136 136
 {
137 137
     expectedBytes = [response expectedContentLength];
138
+ 
139
+    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
140
+    if ([response respondsToSelector:@selector(allHeaderFields)])
141
+    {
142
+        NSDictionary *headers = [httpResponse allHeaderFields];
143
+        [self.bridge.eventDispatcher
144
+         sendDeviceEventWithName: EVENT_STATE_CHANGE
145
+         body:@{
146
+                @"taskId": taskId,
147
+                @"state": @"2",
148
+                @"headers": headers
149
+            }
150
+         ];
151
+    }
138 152
     
139 153
     if(respFile == YES)
140 154
     {