GT2/Ejectable/node_modules/@unimodules/react-native-adapter/ios/UMReactNativeAdapter/UMModuleRegistryAdapter/UMViewManagerAdapterClasses...

58 lines
2.0 KiB
Objective-C

// Copyright 2018-present 650 Industries. All rights reserved.
#import <UMReactNativeAdapter/UMViewManagerAdapterClassesRegistry.h>
#import <UMReactNativeAdapter/UMViewManagerAdapter.h>
#import <objc/runtime.h>
static const NSString *viewManagerAdapterModuleNamePrefix = @"ViewManagerAdapter_";
static IMP directEventBlockImplementation = nil;
static dispatch_once_t directEventBlockImplementationOnceToken;
@interface UMViewManagerAdapterClassesRegistry ()
@property (nonatomic, strong) NSMutableDictionary<Class, Class> *viewManagerAdaptersClasses;
@end
@implementation UMViewManagerAdapterClassesRegistry
- (instancetype)init
{
if (self = [super init]) {
_viewManagerAdaptersClasses = [NSMutableDictionary dictionary];
}
return self;
}
- (Class)viewManagerAdapterClassForViewManager:(UMViewManager *)viewManager
{
Class viewManagerClass = [viewManager class];
if (_viewManagerAdaptersClasses[viewManagerClass] == nil) {
_viewManagerAdaptersClasses[(id <NSCopying>)viewManagerClass] = [self _createViewManagerAdapterClassForViewManager:viewManager];
}
return _viewManagerAdaptersClasses[viewManagerClass];
}
- (Class)_createViewManagerAdapterClassForViewManager:(UMViewManager *)viewManager
{
const char *viewManagerClassName = [[viewManagerAdapterModuleNamePrefix stringByAppendingString:[viewManager viewName]] UTF8String];
Class viewManagerAdapterClass = objc_allocateClassPair([UMViewManagerAdapter class], viewManagerClassName, 0);
[self _ensureDirectEventBlockImplementationIsPresent];
for (NSString *eventName in [viewManager supportedEvents]) {
class_addMethod(object_getClass(viewManagerAdapterClass), NSSelectorFromString([@"propConfig_" stringByAppendingString:eventName]), directEventBlockImplementation, "@@:");
}
return viewManagerAdapterClass;
}
- (void)_ensureDirectEventBlockImplementationIsPresent
{
dispatch_once(&directEventBlockImplementationOnceToken, ^{
directEventBlockImplementation = imp_implementationWithBlock(^{
return @[@"RCTDirectEventBlock"];
});
});
}
@end