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

Commit fa33d69

Browse files
committed
Code IOS refactor
1 parent 212822b commit fa33d69

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ function fetch(...args:any):Promise {
104104
let options = this || {}
105105

106106
let promise = new Promise((resolve, reject) => {
107-
108107
let [method, url, headers, body] = [...args]
109108
let nativeMethodName = Array.isArray(body) ? 'fetchBlobForm' : 'fetchBlob'
110109

src/ios/RNFetchBlob.xcodeproj/project.pbxproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F4261D052E49006FFD38 /* RNFetchBlobFS.m */; };
1111
A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */; };
12-
A158F4301D0539DB006FFD38 /* RNFetchBlobResp.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42F1D0539DB006FFD38 /* RNFetchBlobResp.m */; };
12+
A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */; };
1313
A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */ = {isa = PBXBuildFile; fileRef = A15C30131CD25C330074CB35 /* RNFetchBlob.m */; };
1414
A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */ = {isa = PBXBuildFile; fileRef = A15C30111CD25C330074CB35 /* RNFetchBlob.h */; };
1515
/* End PBXBuildFile section */
@@ -31,8 +31,8 @@
3131
A158F4281D052E57006FFD38 /* RNFetchBlobFS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobFS.h; sourceTree = "<group>"; };
3232
A158F4291D0534A9006FFD38 /* RNFetchBlobConst.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobConst.h; sourceTree = "<group>"; };
3333
A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobConst.m; sourceTree = "<group>"; };
34-
A158F42E1D0539CE006FFD38 /* RNFetchBlobResp.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobResp.h; sourceTree = "<group>"; };
35-
A158F42F1D0539DB006FFD38 /* RNFetchBlobResp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobResp.m; sourceTree = "<group>"; };
34+
A158F42E1D0539CE006FFD38 /* RNFetchBlobNetwork.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNFetchBlobNetwork.h; sourceTree = "<group>"; };
35+
A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNFetchBlobNetwork.m; sourceTree = "<group>"; };
3636
A15C300E1CD25C330074CB35 /* libRNFetchBlob.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNFetchBlob.a; sourceTree = BUILT_PRODUCTS_DIR; };
3737
A15C30111CD25C330074CB35 /* RNFetchBlob.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = RNFetchBlob.h; path = RNFetchBlob/RNFetchBlob.h; sourceTree = "<group>"; };
3838
A15C30131CD25C330074CB35 /* RNFetchBlob.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = RNFetchBlob.m; path = RNFetchBlob/RNFetchBlob.m; sourceTree = "<group>"; };
@@ -59,8 +59,8 @@
5959
A15C30051CD25C330074CB35 = {
6060
isa = PBXGroup;
6161
children = (
62-
A158F42F1D0539DB006FFD38 /* RNFetchBlobResp.m */,
63-
A158F42E1D0539CE006FFD38 /* RNFetchBlobResp.h */,
62+
A158F42F1D0539DB006FFD38 /* RNFetchBlobNetwork.m */,
63+
A158F42E1D0539CE006FFD38 /* RNFetchBlobNetwork.h */,
6464
A158F42C1D0535BB006FFD38 /* RNFetchBlobConst.m */,
6565
A158F4291D0534A9006FFD38 /* RNFetchBlobConst.h */,
6666
A158F4281D052E57006FFD38 /* RNFetchBlobFS.h */,
@@ -139,7 +139,7 @@
139139
A166D1AA1CE0647A00273590 /* RNFetchBlob.h in Sources */,
140140
A158F42D1D0535BB006FFD38 /* RNFetchBlobConst.m in Sources */,
141141
A158F4271D052E49006FFD38 /* RNFetchBlobFS.m in Sources */,
142-
A158F4301D0539DB006FFD38 /* RNFetchBlobResp.m in Sources */,
142+
A158F4301D0539DB006FFD38 /* RNFetchBlobNetwork.m in Sources */,
143143
A15C30141CD25C330074CB35 /* RNFetchBlob.m in Sources */,
144144
);
145145
runOnlyForDeploymentPostprocessing = 0;

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import "RCTBridge.h"
1111
#import "RCTEventDispatcher.h"
1212
#import "RNFetchBlobFS.h"
13-
#import "RNFetchBlobResp.h"
13+
#import "RNFetchBlobNetwork.h"
1414
#import "RNFetchBlobConst.h"
1515

1616

@@ -67,7 +67,7 @@ - (NSDictionary *)constantsToExport
6767
initWithURL:[NSURL
6868
URLWithString: url]];
6969

70-
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[ FetchBlobUtils normalizeHeaders:headers]];
70+
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[ RNFetchBlobNetwork normalizeHeaders:headers]];
7171

7272

7373
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
@@ -128,7 +128,7 @@ - (NSDictionary *)constantsToExport
128128

129129

130130
// send HTTP request
131-
FetchBlobUtils * utils = [[FetchBlobUtils alloc] init];
131+
RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
132132
[utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request withData:postData callback:callback];
133133
});
134134
}
@@ -146,7 +146,8 @@ - (NSDictionary *)constantsToExport
146146
initWithURL:[NSURL
147147
URLWithString: url]];
148148

149-
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[FetchBlobUtils normalizeHeaders:headers]];
149+
NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
150+
150151
// move heavy task to another thread
151152
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
152153
NSMutableData * blobData;
@@ -173,7 +174,7 @@ - (NSDictionary *)constantsToExport
173174
[request setAllHTTPHeaderFields:mheaders];
174175

175176
// send HTTP request
176-
FetchBlobUtils * utils = [[FetchBlobUtils alloc] init];
177+
RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
177178
[utils sendRequest:options bridge:self.bridge taskId:taskId withRequest:request withData:blobData callback:callback];
178179
});
179180
}

src/ios/RNFetchBlobNetwork.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//
2-
// RNFetchBlobResp.h
2+
// RNFetchBlobNetwork.h
33
// RNFetchBlob
44
//
5-
// Created by Ben Hsieh on 2016/6/6.
6-
// Copyright © 2016年 suzuri04x2. All rights reserved.
5+
// Created by wkh237 on 2016/6/6.
6+
// Copyright © 2016 wkh237. All rights reserved.
77
//
88

99
#ifndef RNFetchBlobResp_h
@@ -12,7 +12,7 @@
1212
#import <Foundation/Foundation.h>
1313
#import "RCTBridgeModule.h"
1414

15-
@interface FetchBlobUtils : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate> {
15+
@interface RNFetchBlobNetwork : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate> {
1616

1717
NSString * taskId;
1818
int expectedBytes;

src/ios/RNFetchBlobNetwork.m

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//
2-
// RNFetchBlobResp.m
2+
// RNFetchBlobNetwork.m
33
// RNFetchBlob
44
//
5-
// Created by Ben Hsieh on 2016/6/6.
6-
// Copyright © 2016年 suzuri04x2. All rights reserved.
5+
// Created by wkh237 on 2016/6/6.
6+
// Copyright © 2016 wkh237. All rights reserved.
77
//
88

99
#import "RCTConvert.h"
@@ -12,7 +12,7 @@
1212
#import "RCTBridge.h"
1313
#import "RCTEventDispatcher.h"
1414
#import "RNFetchBlobFS.h"
15-
#import "RNFetchBlobResp.h"
15+
#import "RNFetchBlobNetwork.h"
1616
#import "RNFetchBlobConst.h"
1717

1818
////////////////////////////////////////
@@ -21,7 +21,7 @@
2121
//
2222
////////////////////////////////////////
2323

24-
@implementation FetchBlobUtils
24+
@implementation RNFetchBlobNetwork
2525

2626

2727
@synthesize taskId;
@@ -32,6 +32,12 @@ @implementation FetchBlobUtils
3232
@synthesize bridge;
3333
@synthesize options;
3434

35+
// constructor
36+
- (id)init {
37+
self = [super init];
38+
return self;
39+
}
40+
3541

3642
// removing case from headers
3743
+ (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
@@ -44,12 +50,7 @@ + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
4450
return mheaders;
4551
}
4652

47-
- (id)init {
48-
self = [super init];
49-
return self;
50-
}
51-
52-
53+
// send HTTP request
5354
- (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req withData:( NSData * _Nullable )data callback:(RCTResponseSenderBlock) callback {
5455
self.taskId = taskId;
5556
self.respData = [[NSMutableData alloc] initWithLength:0];
@@ -120,14 +121,22 @@ - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskI
120121
}
121122
}
122123

124+
////////////////////////////////////////
125+
//
126+
// NSURLSession delegates
127+
//
128+
////////////////////////////////////////
129+
123130

124131
#pragma mark NSURLSession delegate methods
125132

133+
// set expected content length on response received
126134
- (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
127135
{
128136
expectedBytes = [response expectedContentLength];
129137
}
130138

139+
// download progress handler
131140
- (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
132141
{
133142
receivedBytes += [data length];
@@ -149,6 +158,11 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
149158
];
150159
}
151160

161+
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
162+
NSLog([error localizedDescription]);
163+
}
164+
165+
// upload progress handler
152166
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
153167
{
154168
expectedBytes = totalBytesExpectedToWrite;

0 commit comments

Comments
 (0)