Pass RCTEventDispatcher to RCTComponentData [6/n]

Summary:
Problem: `RCTComponentData` accesses event dispatcher via `_bridge.eventDispatcher` which won't work in bridgeless mode

Solution: Pass  `_eventDispatcher` through init

Changelog: [Internal]

Reviewed By: sammy-SC

Differential Revision: D27447532

fbshipit-source-id: 7e39b4f6a57d789df493590538248abb204036a3
This commit is contained in:
Peter Argany 2021-03-31 16:37:02 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 4efdf264d1
Коммит 679f38f1c5
4 изменённых файлов: 35 добавлений и 10 удалений

Просмотреть файл

@ -167,7 +167,9 @@ RCT_EXPORT_MODULE()
_componentDataByName = [NSMutableDictionary new]; _componentDataByName = [NSMutableDictionary new];
for (Class moduleClass in _bridge.moduleClasses) { for (Class moduleClass in _bridge.moduleClasses) {
if ([moduleClass isSubclassOfClass:[RCTViewManager class]]) { if ([moduleClass isSubclassOfClass:[RCTViewManager class]]) {
RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:moduleClass bridge:_bridge]; RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:moduleClass
bridge:_bridge
eventDispatcher:_bridge.eventDispatcher];
_componentDataByName[componentData.name] = componentData; _componentDataByName[componentData.name] = componentData;
} }
} }
@ -1577,7 +1579,9 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(lazilyLoadView : (NSString *)name)
return @{}; return @{};
} }
RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:[module class] bridge:self.bridge]; RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:[module class]
bridge:self.bridge
eventDispatcher:self.bridge.eventDispatcher];
_componentDataByName[componentData.name] = componentData; _componentDataByName[componentData.name] = componentData;
NSMutableDictionary *directEvents = [NSMutableDictionary new]; NSMutableDictionary *directEvents = [NSMutableDictionary new];
NSMutableDictionary *bubblingEvents = [NSMutableDictionary new]; NSMutableDictionary *bubblingEvents = [NSMutableDictionary new];

Просмотреть файл

@ -14,6 +14,7 @@
@class RCTBridge; @class RCTBridge;
@class RCTShadowView; @class RCTShadowView;
@class UIView; @class UIView;
@class RCTEventDispatcherProtocol;
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -29,7 +30,9 @@ NS_ASSUME_NONNULL_BEGIN
*/ */
@property (nonatomic, strong, readonly) RCTViewManager *bridgelessViewManager; @property (nonatomic, strong, readonly) RCTViewManager *bridgelessViewManager;
- (instancetype)initWithManagerClass:(Class)managerClass bridge:(RCTBridge *)bridge NS_DESIGNATED_INITIALIZER; - (instancetype)initWithManagerClass:(Class)managerClass
bridge:(RCTBridge *)bridge
eventDispatcher:(id<RCTEventDispatcherProtocol>)eventDispatcher NS_DESIGNATED_INITIALIZER;
- (UIView *)createViewWithTag:(nullable NSNumber *)tag rootTag:(nullable NSNumber *)rootTag; - (UIView *)createViewWithTag:(nullable NSNumber *)tag rootTag:(nullable NSNumber *)rootTag;
- (RCTShadowView *)createShadowViewWithTag:(NSNumber *)tag; - (RCTShadowView *)createShadowViewWithTag:(NSNumber *)tag;

Просмотреть файл

@ -13,6 +13,7 @@
#import "RCTBridgeModule.h" #import "RCTBridgeModule.h"
#import "RCTComponentEvent.h" #import "RCTComponentEvent.h"
#import "RCTConvert.h" #import "RCTConvert.h"
#import "RCTEventDispatcherProtocol.h"
#import "RCTParserUtils.h" #import "RCTParserUtils.h"
#import "RCTShadowView.h" #import "RCTShadowView.h"
#import "RCTUtils.h" #import "RCTUtils.h"
@ -36,15 +37,19 @@ static SEL selectorForType(NSString *type)
RCTPropBlockDictionary *_viewPropBlocks; RCTPropBlockDictionary *_viewPropBlocks;
RCTPropBlockDictionary *_shadowPropBlocks; RCTPropBlockDictionary *_shadowPropBlocks;
__weak RCTBridge *_bridge; __weak RCTBridge *_bridge;
__weak id<RCTEventDispatcherProtocol> _eventDispatcher;
} }
@synthesize manager = _manager; @synthesize manager = _manager;
@synthesize bridgelessViewManager = _bridgelessViewManager; @synthesize bridgelessViewManager = _bridgelessViewManager;
- (instancetype)initWithManagerClass:(Class)managerClass bridge:(RCTBridge *)bridge - (instancetype)initWithManagerClass:(Class)managerClass
bridge:(RCTBridge *)bridge
eventDispatcher:(id<RCTEventDispatcherProtocol>)eventDispatcher
{ {
if ((self = [super init])) { if ((self = [super init])) {
_bridge = bridge; _bridge = bridge;
_eventDispatcher = eventDispatcher;
_managerClass = managerClass; _managerClass = managerClass;
_viewPropBlocks = [NSMutableDictionary new]; _viewPropBlocks = [NSMutableDictionary new];
_shadowPropBlocks = [NSMutableDictionary new]; _shadowPropBlocks = [NSMutableDictionary new];
@ -104,10 +109,13 @@ RCT_NOT_IMPLEMENTED(-(instancetype)init)
} }
} }
static RCTPropBlock static RCTPropBlock createEventSetter(
createEventSetter(NSString *propName, SEL setter, InterceptorBlock eventInterceptor, RCTBridge *bridge) NSString *propName,
SEL setter,
InterceptorBlock eventInterceptor,
id<RCTEventDispatcherProtocol> eventDispatcher)
{ {
__weak RCTBridge *weakBridge = bridge; __weak id<RCTEventDispatcherProtocol> weakEventDispatcher = eventDispatcher;
return ^(id target, id json) { return ^(id target, id json) {
void (^eventHandler)(NSDictionary *event) = nil; void (^eventHandler)(NSDictionary *event) = nil;
if ([RCTConvert BOOL:json]) { if ([RCTConvert BOOL:json]) {
@ -125,7 +133,7 @@ createEventSetter(NSString *propName, SEL setter, InterceptorBlock eventIntercep
RCTComponentEvent *componentEvent = [[RCTComponentEvent alloc] initWithName:propName RCTComponentEvent *componentEvent = [[RCTComponentEvent alloc] initWithName:propName
viewTag:strongTarget.reactTag viewTag:strongTarget.reactTag
body:event]; body:event];
[weakBridge.eventDispatcher sendEvent:componentEvent]; [weakEventDispatcher sendEvent:componentEvent];
} }
}; };
} }
@ -254,7 +262,8 @@ static RCTPropBlock createNSInvocationSetter(NSMethodSignature *typeSignature, S
if (type == NSSelectorFromString(@"RCTBubblingEventBlock:") || if (type == NSSelectorFromString(@"RCTBubblingEventBlock:") ||
type == NSSelectorFromString(@"RCTDirectEventBlock:")) { type == NSSelectorFromString(@"RCTDirectEventBlock:")) {
// Special case for event handlers // Special case for event handlers
setterBlock = createEventSetter(name, setter, self.eventInterceptor, _bridge); setterBlock =
createEventSetter(name, setter, self.eventInterceptor, _bridge ? _bridge.eventDispatcher : _eventDispatcher);
} else { } else {
// Ordinary property handlers // Ordinary property handlers
NSMethodSignature *typeSignature = [[RCTConvert class] methodSignatureForSelector:type]; NSMethodSignature *typeSignature = [[RCTConvert class] methodSignatureForSelector:type];

Просмотреть файл

@ -8,6 +8,7 @@
#include "LegacyViewManagerInteropComponentDescriptor.h" #include "LegacyViewManagerInteropComponentDescriptor.h"
#include <React/RCTBridge.h> #include <React/RCTBridge.h>
#include <React/RCTComponentData.h> #include <React/RCTComponentData.h>
#include <React/RCTEventDispatcher.h>
#include <React/RCTModuleData.h> #include <React/RCTModuleData.h>
#include <React/RCTUIManager.h> #include <React/RCTUIManager.h>
#include <react/utils/ContextContainer.h> #include <react/utils/ContextContainer.h>
@ -64,8 +65,16 @@ static std::shared_ptr<void> const constructCoordinator(
if (optionalBridge) { if (optionalBridge) {
bridge = unwrapManagedObjectWeakly(optionalBridge.value()); bridge = unwrapManagedObjectWeakly(optionalBridge.value());
} }
auto optionalEventDispatcher = contextContainer->find<std::shared_ptr<void>>("RCTEventDispatcher");
RCTEventDispatcher *eventDispatcher;
if (optionalEventDispatcher) {
eventDispatcher = unwrapManagedObject(optionalEventDispatcher.value());
}
RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:module RCTComponentData *componentData = [[RCTComponentData alloc] initWithManagerClass:module
bridge:bridge]; bridge:bridge
eventDispatcher:eventDispatcher];
return wrapManagedObject([[RCTLegacyViewManagerInteropCoordinator alloc] initWithComponentData:componentData return wrapManagedObject([[RCTLegacyViewManagerInteropCoordinator alloc] initWithComponentData:componentData
bridge:bridge]); bridge:bridge]);
} }