Skip to content

Commit

Permalink
Make RCTLocalAssetImageLoader and RCTGIFImageDecoder TurboModule-comp…
Browse files Browse the repository at this point in the history
…atible

Summary:
Couldn't make RCTImageEditingManager and RCTImageStoreManager TurboModule-compatible because their specs live in fb-internal code. I will tackle them in a subsequent diff. See T54946472.

Changelog: [iOS][Added] Make RCTLocalAssetImageLoader and RCTGIFImageDecoder TurboModule-compatible

Reviewed By: PeteTheHeat

Differential Revision: D17936483

fbshipit-source-id: 2266c9ea1ca7ecd52717d9a963e39245da312312
  • Loading branch information
RSNara authored and facebook-github-bot committed Oct 22, 2019
1 parent d8fda74 commit c8701b6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@

#import <ImageIO/ImageIO.h>
#import <QuartzCore/QuartzCore.h>

#import <React/RCTUtils.h>
#import <React/RCTAnimatedImage.h>
#import <React/RCTUtils.h>
#import <ReactCommon/RCTTurboModule.h>

#import "RCTImagePlugins.h"

@interface RCTGIFImageDecoder() <RCTTurboModule>
@end

@implementation RCTGIFImageDecoder

Expand All @@ -32,14 +37,18 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
{
RCTAnimatedImage *image = [[RCTAnimatedImage alloc] initWithData:imageData scale:scale];

if (!image) {
completionHandler(nil, nil);
return ^{};
}

completionHandler(nil, image);
return ^{};
}

@end

Class RCTGIFImageDecoderCls() {
return RCTGIFImageDecoder.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

#import <React/RCTImageEditingManager.h>

#import <UIKit/UIKit.h>

#import <React/RCTConvert.h>
#import <React/RCTImageLoader.h>
#import <React/RCTImageStoreManager.h>
#import <React/RCTImageUtils.h>
#import <React/RCTImageLoaderProtocol.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import <UIKit/UIKit.h>

#import "RCTImagePlugins.h"

@implementation RCTImageEditingManager

Expand Down Expand Up @@ -79,3 +80,7 @@ @implementation RCTImageEditingManager
}

@end

Class RCTImageEditingManagerCls() {
return RCTImageEditingManager.class;
}
4 changes: 4 additions & 0 deletions Libraries/Image/RCTImagePlugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ extern "C" {
Class RCTImageClassProvider(const char *name);

// Lookup functions
Class RCTGIFImageDecoderCls(void) __attribute__((used));
Class RCTImageEditingManagerCls(void) __attribute__((used));
Class RCTImageLoaderCls(void) __attribute__((used));
Class RCTImageStoreManagerCls(void) __attribute__((used));
Class RCTLocalAssetImageLoaderCls(void) __attribute__((used));

#ifdef __cplusplus
}
Expand Down
4 changes: 4 additions & 0 deletions Libraries/Image/RCTImagePlugins.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

Class RCTImageClassProvider(const char *name) {
static std::unordered_map<std::string, Class (*)(void)> sCoreModuleClassMap = {
{"GIFImageDecoder", RCTGIFImageDecoderCls},
{"ImageEditingManager", RCTImageEditingManagerCls},
{"ImageLoader", RCTImageLoaderCls},
{"ImageStoreManager", RCTImageStoreManagerCls},
{"LocalAssetImageLoader", RCTLocalAssetImageLoaderCls},
};

auto p = sCoreModuleClassMap.find(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@

#import <React/RCTImageStoreManager.h>

#import <stdatomic.h>
#import <atomic>
#import <memory>

#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/UTType.h>

#import <React/RCTAssert.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>

#import <React/RCTImageUtils.h>

#import "RCTImagePlugins.h"

static NSString *const RCTImageStoreURLScheme = @"rct-image-store";

@implementation RCTImageStoreManager
Expand Down Expand Up @@ -140,14 +141,14 @@ - (BOOL)canHandleRequest:(NSURLRequest *)request

- (id)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate>)delegate
{
__block atomic_bool cancelled = ATOMIC_VAR_INIT(NO);
__block auto cancelled = std::make_shared<std::atomic<bool>>(false);
void (^cancellationBlock)(void) = ^{
atomic_store(&cancelled, YES);
cancelled->store(true);
};

// Dispatch async to give caller time to cancel the request
dispatch_async(_methodQueue, ^{
if (atomic_load(&cancelled)) {
if (cancelled->load()) {
return;
}

Expand Down Expand Up @@ -238,3 +239,7 @@ - (RCTImageStoreManager *)imageStoreManager
}

@end

Class RCTImageStoreManagerCls(void) {
return RCTImageStoreManager.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

#import <React/RCTLocalAssetImageLoader.h>

#import <stdatomic.h>
#import <atomic>
#import <memory>

#import <React/RCTUtils.h>
#import <ReactCommon/RCTTurboModule.h>

#import "RCTImagePlugins.h"

@interface RCTLocalAssetImageLoader() <RCTTurboModule>
@end

@implementation RCTLocalAssetImageLoader

Expand Down Expand Up @@ -42,9 +49,9 @@ - (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
{
__block atomic_bool cancelled = ATOMIC_VAR_INIT(NO);
__block auto cancelled = std::make_shared<std::atomic<bool>>(false);
RCTExecuteOnMainQueue(^{
if (atomic_load(&cancelled)) {
if (cancelled->load()) {
return;
}

Expand All @@ -62,8 +69,12 @@ - (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
});

return ^{
atomic_store(&cancelled, YES);
cancelled->store(true);
};
}

@end

Class RCTLocalAssetImageLoaderCls(void) {
return RCTLocalAssetImageLoader.class;
}

0 comments on commit c8701b6

Please sign in to comment.