// Copyright 2018-present 650 Industries. All rights reserved. #import #import #import static const NSString *viewManagerAdapterModuleNamePrefix = @"ViewManagerAdapter_"; static IMP directEventBlockImplementation = nil; static dispatch_once_t directEventBlockImplementationOnceToken; @interface UMViewManagerAdapterClassesRegistry () @property (nonatomic, strong) NSMutableDictionary *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 )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