Skip to content

Commit 9d19829

Browse files
committed
Refactored networking logic out into RCTDownloadTask
1 parent 6fea7c2 commit 9d19829

File tree

9 files changed

+299
-270
lines changed

9 files changed

+299
-270
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <Foundation/Foundation.h>
11+
12+
#import "RCTURLRequestDelegate.h"
13+
#import "RCTURLRequestHandler.h"
14+
15+
typedef void (^RCTURLRequestCompletionBlock)(NSURLResponse *response, NSData *data, NSError *error);
16+
typedef void (^RCTURLRequestCancellationBlock)(void);
17+
typedef void (^RCTURLRequestIncrementalDataBlock)(NSData *data);
18+
typedef void (^RCTURLRequestProgressBlock)(double progress, double total);
19+
typedef void (^RCTURLRequestResponseBlock)(NSURLResponse *response);
20+
21+
@interface RCTDownloadTask : NSObject <RCTURLRequestDelegate>
22+
23+
@property (nonatomic, readonly) NSURLRequest *request;
24+
@property (nonatomic, readonly) NSNumber *requestID;
25+
@property (nonatomic, readonly) id requestToken;
26+
@property (nonatomic, readonly) NSURLResponse *response;
27+
@property (nonatomic, readonly) RCTURLRequestCompletionBlock completionBlock;
28+
29+
@property (nonatomic, copy) RCTURLRequestProgressBlock downloadProgressBlock;
30+
@property (nonatomic, copy) RCTURLRequestIncrementalDataBlock incrementalDataBlock;
31+
@property (nonatomic, copy) RCTURLRequestResponseBlock responseBlock;
32+
@property (nonatomic, copy) RCTURLRequestProgressBlock uploadProgressBlock;
33+
34+
- (instancetype)initWithRequest:(NSURLRequest *)request
35+
handler:(id<RCTURLRequestHandler>)handler
36+
completionBlock:(RCTURLRequestCompletionBlock)completionBlock NS_DESIGNATED_INITIALIZER;
37+
38+
- (void)cancel;
39+
40+
@end
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "RCTDownloadTask.h"
11+
12+
#import "RCTAssert.h"
13+
14+
@implementation RCTDownloadTask
15+
{
16+
NSMutableData *_data;
17+
id<RCTURLRequestHandler> _handler;
18+
RCTDownloadTask *_selfReference;
19+
}
20+
21+
- (instancetype)initWithRequest:(NSURLRequest *)request
22+
handler:(id<RCTURLRequestHandler>)handler
23+
completionBlock:(RCTURLRequestCompletionBlock)completionBlock
24+
{
25+
RCTAssertParam(request);
26+
RCTAssertParam(handler);
27+
RCTAssertParam(completionBlock);
28+
29+
static NSUInteger requestID = 0;
30+
31+
if ((self = [super init])) {
32+
if (!(_requestToken = [handler sendRequest:request withDelegate:self])) {
33+
return nil;
34+
}
35+
_requestID = @(requestID++);
36+
_request = request;
37+
_handler = handler;
38+
_completionBlock = completionBlock;
39+
_selfReference = self;
40+
}
41+
return self;
42+
}
43+
44+
- (void)invalidate
45+
{
46+
_selfReference = nil;
47+
_completionBlock = nil;
48+
_downloadProgressBlock = nil;
49+
_incrementalDataBlock = nil;
50+
_responseBlock = nil;
51+
_uploadProgressBlock = nil;
52+
}
53+
54+
RCT_NOT_IMPLEMENTED(-init)
55+
56+
- (void)cancel
57+
{
58+
if ([_handler respondsToSelector:@selector(cancelRequest:)]) {
59+
[_handler cancelRequest:_requestToken];
60+
}
61+
[self invalidate];
62+
}
63+
64+
- (void)URLRequest:(id)requestToken didSendDataWithProgress:(int64_t)bytesSent
65+
{
66+
RCTAssert([requestToken isEqual:_requestToken], @"Unrecognized request token: %@", requestToken);
67+
if (_uploadProgressBlock) {
68+
_uploadProgressBlock(bytesSent, _request.HTTPBody.length);
69+
}
70+
}
71+
72+
- (void)URLRequest:(id)requestToken didReceiveResponse:(NSURLResponse *)response
73+
{
74+
RCTAssert([requestToken isEqual:_requestToken], @"Unrecognized request token: %@", requestToken);
75+
_response = response;
76+
if (_responseBlock) {
77+
_responseBlock(response);
78+
}
79+
}
80+
81+
- (void)URLRequest:(id)requestToken didReceiveData:(NSData *)data
82+
{
83+
RCTAssert([requestToken isEqual:_requestToken], @"Unrecognized request token: %@", requestToken);
84+
if (!_data) {
85+
_data = [[NSMutableData alloc] init];
86+
}
87+
[_data appendData:data];
88+
if (_incrementalDataBlock) {
89+
_incrementalDataBlock(data);
90+
}
91+
if (_downloadProgressBlock && _response.expectedContentLength > 0) {
92+
_downloadProgressBlock(_data.length, _response.expectedContentLength);
93+
}
94+
}
95+
96+
- (void)URLRequest:(id)requestToken didCompleteWithError:(NSError *)error
97+
{
98+
_completionBlock(_response, _data, error);
99+
[self invalidate];
100+
}
101+
102+
@end

Libraries/Network/RCTHTTPRequestHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#import "RCTURLRequestHandler.h"
1111
#import "RCTInvalidating.h"
1212

13+
/**
14+
* This is the default RCTURLRequestHandler implementation for HTTP requests.
15+
*/
1316
@interface RCTHTTPRequestHandler : NSObject <RCTURLRequestHandler, RCTInvalidating>
1417

1518
@end

Libraries/Network/RCTHTTPRequestHandler.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ - (BOOL)canHandleRequest:(NSURLRequest *)request
5050
return [@[@"http", @"https", @"file"] containsObject:[request.URL.scheme lowercaseString]];
5151
}
5252

53-
- (id)sendRequest:(NSURLRequest *)request
54-
withDelegate:(id<RCTURLRequestDelegate>)delegate
53+
- (NSURLSessionDataTask *)sendRequest:(NSURLRequest *)request
54+
withDelegate:(id<RCTURLRequestDelegate>)delegate
5555
{
5656
// Lazy setup
5757
if (!_session && [self isValid]) {
@@ -68,9 +68,10 @@ - (id)sendRequest:(NSURLRequest *)request
6868
return task;
6969
}
7070

71-
- (void)cancelRequest:(NSURLSessionDataTask *)requestToken
71+
- (void)cancelRequest:(NSURLSessionDataTask *)task
7272
{
73-
[requestToken cancel];
73+
[task cancel];
74+
[_delegates removeObjectForKey:task];
7475
}
7576

7677
#pragma mark - NSURLSession delegate
@@ -81,10 +82,9 @@ - (void)URLSession:(NSURLSession *)session
8182
totalBytesSent:(int64_t)totalBytesSent
8283
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
8384
{
84-
[[_delegates objectForKey:task] URLRequest:task didUploadProgress:(double)totalBytesSent total:(double)totalBytesExpectedToSend];
85+
[[_delegates objectForKey:task] URLRequest:task didSendDataWithProgress:totalBytesSent];
8586
}
8687

87-
8888
- (void)URLSession:(NSURLSession *)session
8989
dataTask:(NSURLSessionDataTask *)task
9090
didReceiveResponse:(NSURLResponse *)response

Libraries/Network/RCTNetwork.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
1372B7371AB03E7B00659ED6 /* RCTReachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 1372B7361AB03E7B00659ED6 /* RCTReachability.m */; };
11+
13D6D66A1B5FCF8200883BE9 /* RCTDownloadTask.m in Sources */ = {isa = PBXBuildFile; fileRef = 13D6D6691B5FCF8200883BE9 /* RCTDownloadTask.m */; };
1112
352DA0BA1B17855800AA15A8 /* RCTHTTPRequestHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 352DA0B81B17855800AA15A8 /* RCTHTTPRequestHandler.m */; };
1213
58B512081A9E6CE300147676 /* RCTNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = 58B512071A9E6CE300147676 /* RCTNetworking.m */; };
1314
/* End PBXBuildFile section */
@@ -27,6 +28,8 @@
2728
/* Begin PBXFileReference section */
2829
1372B7351AB03E7B00659ED6 /* RCTReachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTReachability.h; sourceTree = "<group>"; };
2930
1372B7361AB03E7B00659ED6 /* RCTReachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTReachability.m; sourceTree = "<group>"; };
31+
13D6D6681B5FCF8200883BE9 /* RCTDownloadTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTDownloadTask.h; sourceTree = "<group>"; };
32+
13D6D6691B5FCF8200883BE9 /* RCTDownloadTask.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDownloadTask.m; sourceTree = "<group>"; };
3033
352DA0B71B17855800AA15A8 /* RCTHTTPRequestHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTHTTPRequestHandler.h; sourceTree = "<group>"; };
3134
352DA0B81B17855800AA15A8 /* RCTHTTPRequestHandler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTHTTPRequestHandler.m; sourceTree = "<group>"; };
3235
58B511DB1A9E6C8500147676 /* libRCTNetwork.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTNetwork.a; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -48,6 +51,8 @@
4851
58B511D21A9E6C8500147676 = {
4952
isa = PBXGroup;
5053
children = (
54+
13D6D6681B5FCF8200883BE9 /* RCTDownloadTask.h */,
55+
13D6D6691B5FCF8200883BE9 /* RCTDownloadTask.m */,
5156
352DA0B71B17855800AA15A8 /* RCTHTTPRequestHandler.h */,
5257
352DA0B81B17855800AA15A8 /* RCTHTTPRequestHandler.m */,
5358
58B512061A9E6CE300147676 /* RCTNetworking.h */,
@@ -124,6 +129,7 @@
124129
isa = PBXSourcesBuildPhase;
125130
buildActionMask = 2147483647;
126131
files = (
132+
13D6D66A1B5FCF8200883BE9 /* RCTDownloadTask.m in Sources */,
127133
1372B7371AB03E7B00659ED6 /* RCTReachability.m in Sources */,
128134
58B512081A9E6CE300147676 /* RCTNetworking.m in Sources */,
129135
352DA0BA1B17855800AA15A8 /* RCTHTTPRequestHandler.m in Sources */,

Libraries/Network/RCTNetworking.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@
1414
@interface RCTNetworking : NSObject <RCTBridgeModule>
1515

1616
@end
17-

0 commit comments

Comments
 (0)