Refactored RCTImageDownloader to use RCTNetworking instead of a separate download system

This commit is contained in:
Nick Lockwood
2015-07-27 13:46:59 -07:00
parent 3cff9be3d1
commit 1d852624fd
11 changed files with 159 additions and 326 deletions

View File

@@ -15,7 +15,7 @@
typedef void (^RCTURLRequestCompletionBlock)(NSURLResponse *response, NSData *data, NSError *error);
typedef void (^RCTURLRequestCancellationBlock)(void);
typedef void (^RCTURLRequestIncrementalDataBlock)(NSData *data);
typedef void (^RCTURLRequestProgressBlock)(double progress, double total);
typedef void (^RCTURLRequestProgressBlock)(int64_t progress, int64_t total);
typedef void (^RCTURLRequestResponseBlock)(NSURLResponse *response);
@interface RCTDownloadTask : NSObject <RCTURLRequestDelegate>

View File

@@ -9,8 +9,18 @@
#import <Foundation/Foundation.h>
#import "RCTBridgeModule.h"
#import "RCTBridge.h"
#import "RCTDownloadTask.h"
@interface RCTNetworking : NSObject <RCTBridgeModule>
- (RCTDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request
completionBlock:(RCTURLRequestCompletionBlock)completionBlock;
@end
@interface RCTBridge (RCTNetworking)
@property (nonatomic, readonly) RCTNetworking *networking;
@end

View File

@@ -233,13 +233,8 @@ RCT_EXPORT_MODULE()
NSURLRequest *request = [RCTConvert NSURLRequest:query[@"uri"]];
if (request) {
id<RCTURLRequestHandler> handler = [self handlerForRequest:request];
if (!handler) {
return callback(nil, nil);
}
__block RCTURLRequestCancellationBlock cancellationBlock = nil;
RCTDownloadTask *task = [[RCTDownloadTask alloc] initWithRequest:request handler:handler completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
RCTDownloadTask *task = [self downloadTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
cancellationBlock = callback(error, data ? @{@"body": data, @"contentType": RCTNullIfNil(response.MIMEType)} : nil);
}];
@@ -291,16 +286,11 @@ RCT_EXPORT_MODULE()
incrementalUpdates:(BOOL)incrementalUpdates
responseSender:(RCTResponseSenderBlock)responseSender
{
id<RCTURLRequestHandler> handler = [self handlerForRequest:request];
if (!handler) {
return;
}
__block RCTDownloadTask *task;
RCTURLRequestProgressBlock uploadProgressBlock = ^(double progress, double total) {
RCTURLRequestProgressBlock uploadProgressBlock = ^(int64_t progress, int64_t total) {
dispatch_async(_methodQueue, ^{
NSArray *responseJSON = @[task.requestID, @(progress), @(total)];
NSArray *responseJSON = @[task.requestID, @((double)progress), @((double)total)];
[_bridge.eventDispatcher sendDeviceEventWithName:@"didSendNetworkData" body:responseJSON];
});
};
@@ -345,10 +335,7 @@ RCT_EXPORT_MODULE()
});
};
task = [[RCTDownloadTask alloc] initWithRequest:request
handler:handler
completionBlock:completionBlock];
task = [self downloadTaskWithRequest:request completionBlock:completionBlock];
task.incrementalDataBlock = incrementalDataBlock;
task.responseBlock = responseBlock;
task.uploadProgressBlock = uploadProgressBlock;
@@ -359,6 +346,21 @@ RCT_EXPORT_MODULE()
}
}
#pragma mark - Public API
- (RCTDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request
completionBlock:(RCTURLRequestCompletionBlock)completionBlock
{
id<RCTURLRequestHandler> handler = [self handlerForRequest:request];
if (!handler) {
return nil;
}
return [[RCTDownloadTask alloc] initWithRequest:request
handler:handler
completionBlock:completionBlock];
}
#pragma mark - JS API
RCT_EXPORT_METHOD(sendRequest:(NSDictionary *)query
@@ -383,3 +385,12 @@ RCT_EXPORT_METHOD(cancelRequest:(NSNumber *)requestID)
}
@end
@implementation RCTBridge (RCTNetworking)
- (RCTNetworking *)networking
{
return self.modules[RCTBridgeModuleNameForClass([RCTNetworking class])];
}
@end