Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions packages/react-native/Libraries/Image/RCTImageLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ - (void)setReactDecodedImageBytes:(NSInteger)bytes
@end

@implementation RCTImageLoader {
std::atomic<BOOL> _isLoaderSetup;
std::mutex _loaderSetupLock;
NSArray<id<RCTImageURLLoader>> * (^_loadersProvider)(RCTModuleRegistry *);
NSArray<id<RCTImageDataDecoder>> * (^_decodersProvider)(RCTModuleRegistry *);
NSArray<id<RCTImageURLLoader>> *_loaders;
Expand Down Expand Up @@ -106,6 +108,7 @@ - (instancetype)initWithRedirectDelegate:(id<RCTImageRedirectProtocol>)redirectD
{
if (self = [super init]) {
_redirectDelegate = redirectDelegate;
_isLoaderSetup = NO;
}
return self;
}
Expand All @@ -123,12 +126,16 @@ - (instancetype)initWithRedirectDelegate:(id<RCTImageRedirectProtocol>)redirectD

- (void)setUp
{
// Set defaults
_maxConcurrentLoadingTasks = _maxConcurrentLoadingTasks ?: 4;
_maxConcurrentDecodingTasks = _maxConcurrentDecodingTasks ?: 2;
_maxConcurrentDecodingBytes = _maxConcurrentDecodingBytes ?: 30 * 1024 * 1024; // 30MB

_URLRequestQueue = dispatch_queue_create("com.facebook.react.ImageLoaderURLRequestQueue", DISPATCH_QUEUE_SERIAL);
std::lock_guard<std::mutex> guard(_loaderSetupLock);
if (!_isLoaderSetup) {
// Set defaults
_maxConcurrentLoadingTasks = _maxConcurrentLoadingTasks ?: 4;
_maxConcurrentDecodingTasks = _maxConcurrentDecodingTasks ?: 2;
_maxConcurrentDecodingBytes = _maxConcurrentDecodingBytes ?: 30 * 1024 * 1024; // 30MB

_URLRequestQueue = dispatch_queue_create("com.facebook.react.ImageLoaderURLRequestQueue", DISPATCH_QUEUE_SERIAL);
_isLoaderSetup = YES;
}
}

- (float)handlerPriority
Expand Down Expand Up @@ -156,7 +163,7 @@ - (void)setImageCache:(id<RCTImageCache>)cache

- (id<RCTImageURLLoader>)imageURLLoaderForURL:(NSURL *)URL
{
if (!_maxConcurrentLoadingTasks) {
if (!_isLoaderSetup) {
[self setUp];
}

Expand Down Expand Up @@ -229,7 +236,7 @@ - (void)setImageCache:(id<RCTImageCache>)cache

- (id<RCTImageDataDecoder>)imageDataDecoderForData:(NSData *)data
{
if (!_maxConcurrentLoadingTasks) {
if (!_isLoaderSetup) {
[self setUp];
}

Expand Down Expand Up @@ -564,7 +571,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
}

// All access to URL cache must be serialized
if (!_URLRequestQueue) {
if (!_isLoaderSetup) {
[self setUp];
}

Expand Down Expand Up @@ -979,7 +986,7 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)data
});
};

if (!_URLRequestQueue) {
if (!_isLoaderSetup) {
[self setUp];
}
dispatch_async(_URLRequestQueue, ^{
Expand Down