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
       }
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
     let req = RNFetchBlob[nativeMethodName]
136
     let req = RNFetchBlob[nativeMethodName]
131
     req(options, taskId, method, url, headers || {}, body, (err, data) => {
137
     req(options, taskId, method, url, headers || {}, body, (err, data) => {
132
 
138
 
133
       // task done, remove event listener
139
       // task done, remove event listener
134
       subscription.remove()
140
       subscription.remove()
135
       subscriptionUpload.remove()
141
       subscriptionUpload.remove()
142
+      stateEvent.remove()
136
       if(err)
143
       if(err)
137
         reject(new Error(err, data))
144
         reject(new Error(err, data))
138
       else {
145
       else {

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

16
 extern NSString *const MSG_EVENT_LOG;
16
 extern NSString *const MSG_EVENT_LOG;
17
 extern NSString *const MSG_EVENT_WARN;
17
 extern NSString *const MSG_EVENT_WARN;
18
 extern NSString *const MSG_EVENT_ERROR;
18
 extern NSString *const MSG_EVENT_ERROR;
19
+extern NSString *const EVENT_STATE_CHANGE;
19
 
20
 
20
 extern NSString *const FILE_PREFIX;
21
 extern NSString *const FILE_PREFIX;
21
 extern NSString *const ASSET_PREFIX;
22
 extern NSString *const ASSET_PREFIX;

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

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

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

135
 - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
135
 - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
136
 {
136
 {
137
     expectedBytes = [response expectedContentLength];
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
     if(respFile == YES)
153
     if(respFile == YES)
140
     {
154
     {