mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-26 23:05:00 +08:00
Updates from Tue Feb 17
- [ReactNative] Make run on iOS7 again | Philipp von Weitershausen - [ReactNative] Make it possible to use fbobjc's RKJSModules for ReactAndroid | Philipp von Weitershausen - [React Native] Add image/network cache | Alex Akers - [ReactNative] Sync fbandroid to fbobjc | Philipp von Weitershausen
This commit is contained in:
22
ReactKit/Base/RCTCache.h
Normal file
22
ReactKit/Base/RCTCache.h
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface RCTCache : NSObject
|
||||
|
||||
- (instancetype)init; // name = @"default"
|
||||
- (instancetype)initWithName:(NSString *)name;
|
||||
|
||||
@property (nonatomic, assign) NSUInteger maximumDiskSize; // in bytes
|
||||
|
||||
#pragma mark - Retrieval
|
||||
|
||||
- (BOOL)hasDataForKey:(NSString *)key;
|
||||
- (void)fetchDataForKey:(NSString *)key completionHandler:(void (^)(NSData *data))completionHandler;
|
||||
|
||||
#pragma mark - Insertion
|
||||
|
||||
- (void)setData:(NSData *)data forKey:(NSString *)key;
|
||||
- (void)removeAllData;
|
||||
|
||||
@end
|
||||
209
ReactKit/Base/RCTCache.m
Normal file
209
ReactKit/Base/RCTCache.m
Normal file
@@ -0,0 +1,209 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import "RCTCache.h"
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <sys/xattr.h>
|
||||
|
||||
static NSString *const CacheSubdirectoryName = @"ReactKit";
|
||||
static NSString *const KeyExtendedAttributeName = @"com.facebook.ReactKit.RCTCacheManager.Key";
|
||||
static dispatch_queue_t Queue;
|
||||
|
||||
#pragma mark - Cache Record -
|
||||
|
||||
@interface RCTCacheRecord : NSObject
|
||||
|
||||
@property (nonatomic, copy) NSUUID *UUID;
|
||||
@property (nonatomic, copy) NSData *data;
|
||||
|
||||
@end
|
||||
|
||||
@implementation RCTCacheRecord
|
||||
|
||||
@end
|
||||
|
||||
#pragma mark - Cache
|
||||
|
||||
@implementation RCTCache
|
||||
{
|
||||
NSString *_name;
|
||||
NSFileManager *_fileManager;
|
||||
NSMutableDictionary *_storage;
|
||||
NSURL *_cacheDirectoryURL;
|
||||
}
|
||||
|
||||
+ (void)initialize
|
||||
{
|
||||
if (self == [RCTCache class]) {
|
||||
Queue = dispatch_queue_create("com.facebook.ReactKit.RCTCache", DISPATCH_QUEUE_SERIAL);
|
||||
}
|
||||
}
|
||||
- (instancetype)init
|
||||
{
|
||||
return [self initWithName:@"default"];
|
||||
}
|
||||
|
||||
- (instancetype)initWithName:(NSString *)name
|
||||
{
|
||||
NSParameterAssert(name.length < NAME_MAX);
|
||||
if ((self = [super init])) {
|
||||
_name = [name copy];
|
||||
_fileManager = [[NSFileManager alloc] init];
|
||||
_storage = [NSMutableDictionary dictionary];
|
||||
|
||||
NSURL *cacheDirectoryURL = [[_fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
|
||||
cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:CacheSubdirectoryName isDirectory:YES];
|
||||
_cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:name isDirectory:YES];
|
||||
[_fileManager createDirectoryAtURL:_cacheDirectoryURL withIntermediateDirectories:YES attributes:nil error:NULL];
|
||||
|
||||
NSArray *fileURLs = [_fileManager contentsOfDirectoryAtURL:_cacheDirectoryURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:NULL];
|
||||
for (NSURL *fileURL in fileURLs) {
|
||||
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:fileURL.lastPathComponent];
|
||||
if (!uuid) continue;
|
||||
|
||||
NSString *key = [self keyOfItemAtURL:fileURL error:NULL];
|
||||
if (!key) {
|
||||
[_fileManager removeItemAtURL:fileURL error:NULL];
|
||||
continue;
|
||||
}
|
||||
|
||||
RCTCacheRecord *record = [[RCTCacheRecord alloc] init];
|
||||
record.UUID = uuid;
|
||||
_storage[key] = record;
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)runOnQueue:(dispatch_block_t)block
|
||||
{
|
||||
UIBackgroundTaskIdentifier identifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
|
||||
dispatch_async(Queue, ^{
|
||||
if (block) block();
|
||||
if (identifier != UIBackgroundTaskInvalid) {
|
||||
[[UIApplication sharedApplication] endBackgroundTask:identifier];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
- (BOOL)hasDataForKey:(NSString *)key
|
||||
{
|
||||
return _storage[key] != nil;
|
||||
}
|
||||
|
||||
- (void)fetchDataForKey:(NSString *)key completionHandler:(void (^)(NSData *))completionHandler
|
||||
{
|
||||
NSParameterAssert(key.length > 0);
|
||||
NSParameterAssert(completionHandler != nil);
|
||||
[self runOnQueue:^{
|
||||
RCTCacheRecord *record = _storage[key];
|
||||
if (record && !record.data) {
|
||||
record.data = [NSData dataWithContentsOfURL:[_cacheDirectoryURL URLByAppendingPathComponent:record.UUID.UUIDString]];
|
||||
}
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
completionHandler(record.data);
|
||||
});
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)setData:(NSData *)data forKey:(NSString *)key
|
||||
{
|
||||
NSParameterAssert(key.length > 0);
|
||||
[self runOnQueue:^{
|
||||
RCTCacheRecord *record = _storage[key];
|
||||
if (data) {
|
||||
if (!record) {
|
||||
record = [[RCTCacheRecord alloc] init];
|
||||
record.UUID = [NSUUID UUID];
|
||||
_storage[key] = record;
|
||||
}
|
||||
|
||||
record.data = data;
|
||||
|
||||
NSURL *fileURL = [_cacheDirectoryURL URLByAppendingPathComponent:record.UUID.UUIDString];
|
||||
[data writeToURL:fileURL options:NSDataWritingAtomic error:NULL];
|
||||
} else if (record) {
|
||||
[_storage removeObjectForKey:key];
|
||||
|
||||
NSURL *fileURL = [_cacheDirectoryURL URLByAppendingPathComponent:record.UUID.UUIDString];
|
||||
[_fileManager removeItemAtURL:fileURL error:NULL];
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)removeAllData
|
||||
{
|
||||
[self runOnQueue:^{
|
||||
[_storage removeAllObjects];
|
||||
|
||||
NSDirectoryEnumerator *enumerator = [_fileManager enumeratorAtURL:_cacheDirectoryURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles errorHandler:nil];
|
||||
for (NSURL *fileURL in enumerator) {
|
||||
[_fileManager removeItemAtURL:fileURL error:NULL];
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
#pragma mark - Extended Attributes
|
||||
|
||||
- (NSError *)errorWithPOSIXErrorNumber:(int)errorNumber
|
||||
{
|
||||
NSDictionary *userInfo = @{
|
||||
NSLocalizedDescriptionKey: @(strerror(errorNumber))
|
||||
};
|
||||
return [NSError errorWithDomain:NSPOSIXErrorDomain code:errorNumber userInfo:userInfo];
|
||||
}
|
||||
|
||||
- (BOOL)setAttribute:(NSString *)key value:(NSString *)value ofItemAtURL:(NSURL *)fileURL error:(NSError **)error
|
||||
{
|
||||
const char *path = fileURL.fileSystemRepresentation;
|
||||
|
||||
int result;
|
||||
if (value) {
|
||||
const char *valueUTF8String = value.UTF8String;
|
||||
result = setxattr(path, key.UTF8String, valueUTF8String, strlen(valueUTF8String), 0, 0);
|
||||
} else {
|
||||
result = removexattr(path, key.UTF8String, 0);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
if (error) *error = [self errorWithPOSIXErrorNumber:errno];
|
||||
return NO;
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSString *)attribute:(NSString *)key ofItemAtURL:(NSURL *)fileURL error:(NSError **)error
|
||||
{
|
||||
const char *path = fileURL.fileSystemRepresentation;
|
||||
const ssize_t length = getxattr(path, key.UTF8String, NULL, 0, 0, 0);
|
||||
if (length <= 0) {
|
||||
if (error) *error = [self errorWithPOSIXErrorNumber:errno];
|
||||
return nil;
|
||||
}
|
||||
|
||||
char *buffer = malloc(length);
|
||||
ssize_t result = getxattr(path, key.UTF8String, buffer, length, 0, 0);
|
||||
if (result == 0) {
|
||||
return [[NSString alloc] initWithBytesNoCopy:buffer length:length encoding:NSUTF8StringEncoding freeWhenDone:YES];
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
if (error) *error = [self errorWithPOSIXErrorNumber:errno];
|
||||
return nil;
|
||||
}
|
||||
|
||||
#pragma mark - Extended Attributes - Key
|
||||
|
||||
- (NSString *)keyOfItemAtURL:(NSURL *)fileURL error:(NSError **)error
|
||||
{
|
||||
return [self attribute:KeyExtendedAttributeName ofItemAtURL:fileURL error:error];
|
||||
}
|
||||
|
||||
- (BOOL)setKey:(NSString *)key ofItemAtURL:(NSURL *)fileURL error:(NSError **)error
|
||||
{
|
||||
return [self setAttribute:KeyExtendedAttributeName value:key ofItemAtURL:fileURL error:error];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -814,7 +814,7 @@ BOOL RCTSetProperty(id target, NSString *keypath, id value)
|
||||
|
||||
// Special case for numeric encodings, which may be enums
|
||||
if ([value isKindOfClass:[NSString class]] &&
|
||||
[@"iIsSlLqQ" containsString:[encoding substringToIndex:1]]) {
|
||||
[@"iIsSlLqQ" rangeOfString:[encoding substringToIndex:1]].length) {
|
||||
|
||||
/**
|
||||
* NOTE: the property names below may seem weird, but it's
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import "RCTImageDownloader.h"
|
||||
|
||||
#import "RCTCache.h"
|
||||
#import "RCTUtils.h"
|
||||
|
||||
// TODO: something a bit more sophisticated
|
||||
|
||||
@implementation RCTImageDownloader
|
||||
{
|
||||
RCTCache *_cache;
|
||||
}
|
||||
|
||||
+ (instancetype)sharedInstance
|
||||
{
|
||||
@@ -18,18 +23,63 @@
|
||||
return sharedInstance;
|
||||
}
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_cache = [[RCTCache alloc] initWithName:@"RCTImageDownloader"];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)cacheKeyForURL:(NSURL *)url
|
||||
{
|
||||
return url.absoluteString;
|
||||
}
|
||||
|
||||
- (id)_downloadDataForURL:(NSURL *)url
|
||||
block:(RCTDataDownloadBlock)block
|
||||
{
|
||||
NSString *cacheKey = [self cacheKeyForURL:url];
|
||||
|
||||
__block BOOL cancelled = NO;
|
||||
__block NSURLSessionDataTask *task = nil;
|
||||
dispatch_block_t cancel = ^{
|
||||
cancelled = YES;
|
||||
if (task) {
|
||||
[task cancel];
|
||||
task = nil;
|
||||
}
|
||||
};
|
||||
|
||||
if ([_cache hasDataForKey:cacheKey]) {
|
||||
[_cache fetchDataForKey:cacheKey completionHandler:^(NSData *data) {
|
||||
if (cancelled) return;
|
||||
block(data, nil);
|
||||
}];
|
||||
} else {
|
||||
task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
|
||||
block(data, error);
|
||||
}];
|
||||
|
||||
[task resume];
|
||||
}
|
||||
|
||||
return [cancel copy];
|
||||
}
|
||||
|
||||
- (id)downloadDataForURL:(NSURL *)url
|
||||
block:(RCTDataDownloadBlock)block
|
||||
{
|
||||
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
|
||||
// Dispatch back to main thread
|
||||
NSString *cacheKey = [self cacheKeyForURL:url];
|
||||
__weak RCTImageDownloader *weakSelf = self;
|
||||
return [self _downloadDataForURL:url block:^(NSData *data, NSError *error) {
|
||||
RCTImageDownloader *strongSelf = weakSelf;
|
||||
[strongSelf->_cache setData:data forKey:cacheKey];
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
block(data, error);
|
||||
});
|
||||
}];
|
||||
|
||||
[task resume];
|
||||
return task;
|
||||
}
|
||||
|
||||
- (id)downloadImageForURL:(NSURL *)url
|
||||
@@ -37,45 +87,49 @@
|
||||
scale:(CGFloat)scale
|
||||
block:(RCTImageDownloadBlock)block
|
||||
{
|
||||
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
|
||||
|
||||
UIImage *image = [UIImage imageWithData:data scale:scale];
|
||||
|
||||
// TODO: cache compressed image
|
||||
|
||||
CGSize imageSize = size;
|
||||
if (CGSizeEqualToSize(imageSize, CGSizeZero)) {
|
||||
imageSize = image.size;
|
||||
NSString *cacheKey = [self cacheKeyForURL:url];
|
||||
__weak RCTImageDownloader *weakSelf = self;
|
||||
return [self _downloadDataForURL:url block:^(NSData *data, NSError *error) {
|
||||
if (data) {
|
||||
UIImage *image = [UIImage imageWithData:data scale:scale];
|
||||
|
||||
if (image) {
|
||||
CGSize imageSize = size;
|
||||
if (CGSizeEqualToSize(imageSize, CGSizeZero)) {
|
||||
imageSize = image.size;
|
||||
}
|
||||
|
||||
CGFloat imageScale = scale;
|
||||
if (imageScale == 0 || imageScale > image.scale) {
|
||||
imageScale = image.scale;
|
||||
}
|
||||
|
||||
UIGraphicsBeginImageContextWithOptions(imageSize, NO, imageScale);
|
||||
[image drawInRect:(CGRect){{0, 0}, imageSize}];
|
||||
image = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
|
||||
RCTImageDownloader *strongSelf = weakSelf;
|
||||
[strongSelf->_cache setData:UIImagePNGRepresentation(image) forKey:cacheKey];
|
||||
}
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
block(image, nil);
|
||||
});
|
||||
} else {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
block(nil, error);
|
||||
});
|
||||
}
|
||||
|
||||
CGFloat imageScale = scale;
|
||||
if (imageScale == 0 || imageScale > image.scale) {
|
||||
imageScale = image.scale;
|
||||
}
|
||||
|
||||
if (image) {
|
||||
// Decompress on background thread
|
||||
UIGraphicsBeginImageContextWithOptions(imageSize, NO, imageScale);
|
||||
[image drawInRect:(CGRect){{0, 0}, imageSize}];
|
||||
image = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
|
||||
// TODO: cache decompressed images at each requested size
|
||||
}
|
||||
|
||||
// Dispatch back to main thread
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
block(image, error);
|
||||
});
|
||||
}];
|
||||
|
||||
[task resume];
|
||||
return task;
|
||||
}
|
||||
|
||||
- (void)cancelDownload:(id)downloadToken
|
||||
{
|
||||
[(NSURLSessionDataTask *)downloadToken cancel];
|
||||
if (downloadToken) {
|
||||
dispatch_block_t block = downloadToken;
|
||||
block();
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user