From b02cdb08dedc527f2569208d8d4add669729cd02 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Mon, 13 Jan 2025 14:05:16 +0100 Subject: [PATCH] Remove `REAInitializerRCTFabricSurface` (#6837) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary This PR cleans up the Reanimated initialization process for iOS on Fabric. The primary motivation for these changes is to remove `REAInitializerRCTFabricSurface`, which was used as a workaround to access the surface presenter on the new architecture in bridge-full mode. ⚠️ Please note that this PR drops support for bridge-full mode on the new architecture. --- .../Fabric/REAInitializerRCTFabricSurface.h | 12 -- .../Fabric/REAInitializerRCTFabricSurface.mm | 73 ----------- .../apple/reanimated/apple/REAModule.h | 4 - .../apple/reanimated/apple/REAModule.mm | 119 ++++-------------- .../apple/reanimated/apple/REANodesManager.h | 1 - .../apple/reanimated/apple/REANodesManager.mm | 7 -- 6 files changed, 25 insertions(+), 191 deletions(-) delete mode 100644 packages/react-native-reanimated/apple/reanimated/apple/Fabric/REAInitializerRCTFabricSurface.h delete mode 100644 packages/react-native-reanimated/apple/reanimated/apple/Fabric/REAInitializerRCTFabricSurface.mm diff --git a/packages/react-native-reanimated/apple/reanimated/apple/Fabric/REAInitializerRCTFabricSurface.h b/packages/react-native-reanimated/apple/reanimated/apple/Fabric/REAInitializerRCTFabricSurface.h deleted file mode 100644 index c27c2106790..00000000000 --- a/packages/react-native-reanimated/apple/reanimated/apple/Fabric/REAInitializerRCTFabricSurface.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifdef RCT_NEW_ARCH_ENABLED - -#import -#import - -@interface REAInitializerRCTFabricSurface : RCTFabricSurface - -@property __weak REAModule *reaModule; - -@end - -#endif // RCT_NEW_ARCH_ENABLED diff --git a/packages/react-native-reanimated/apple/reanimated/apple/Fabric/REAInitializerRCTFabricSurface.mm b/packages/react-native-reanimated/apple/reanimated/apple/Fabric/REAInitializerRCTFabricSurface.mm deleted file mode 100644 index 59e8c8ef7ae..00000000000 --- a/packages/react-native-reanimated/apple/reanimated/apple/Fabric/REAInitializerRCTFabricSurface.mm +++ /dev/null @@ -1,73 +0,0 @@ -#ifdef RCT_NEW_ARCH_ENABLED - -#import -#import -#import -#import -#import - -@implementation REAInitializerRCTFabricSurface { - std::shared_ptr _surfaceHandler; - int _tag; - RCTSurface *_surface; -} - -- (instancetype)init -{ - if (self = [super init]) { - _tag = -1; - _surface = [[RCTSurface alloc] init]; - _surfaceHandler = std::make_shared("REASurface", _tag); - } - return self; -} - -- (NSNumber *)rootViewTag -{ - return @(_tag); -} - -- (NSInteger)rootTag -{ - return (NSInteger)_tag; -} - -- (void)start -{ - // this is only needed method, the rest of them is just for prevent null pointer exceptions - [_reaModule installReanimatedAfterReload]; -} - -- (facebook::react::SurfaceHandler const &)surfaceHandler -{ - return *_surfaceHandler.get(); -} - -- (void)setMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize -{ -} - -- (void)setMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize viewportOffset:(CGPoint)viewportOffset -{ -} - -- (void)stop -{ -} - -- (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize -{ - CGSize size{0, 0}; - return size; -} - -- (nonnull RCTSurfaceView *)view -{ - // This method should never be called. - react_native_assert(false); - return nullptr; -} - -@end - -#endif // RCT_NEW_ARCH_ENABLED diff --git a/packages/react-native-reanimated/apple/reanimated/apple/REAModule.h b/packages/react-native-reanimated/apple/reanimated/apple/REAModule.h index 634be0f8932..6c536802f0b 100644 --- a/packages/react-native-reanimated/apple/reanimated/apple/REAModule.h +++ b/packages/react-native-reanimated/apple/reanimated/apple/REAModule.h @@ -30,8 +30,4 @@ @property (nonatomic, readonly) REANodesManager *nodesManager; @property REAAnimationsManager *animationsManager; -#ifdef RCT_NEW_ARCH_ENABLED -- (void)installReanimatedAfterReload; -#endif // RCT_NEW_ARCH_ENABLED - @end diff --git a/packages/react-native-reanimated/apple/reanimated/apple/REAModule.mm b/packages/react-native-reanimated/apple/reanimated/apple/REAModule.mm index 0db7007fc22..42d1de71a07 100644 --- a/packages/react-native-reanimated/apple/reanimated/apple/REAModule.mm +++ b/packages/react-native-reanimated/apple/reanimated/apple/REAModule.mm @@ -12,10 +12,6 @@ #endif // REACT_NATIVE_MINOR_VERSION >= 75 #endif // RCT_NEW_ARCH_ENABLED -#ifdef RCT_NEW_ARCH_ENABLED -#import -#endif // RCT_NEW_ARCH_ENABLED - #import #import #import @@ -49,7 +45,7 @@ - (void)_tryAndHandleError:(dispatch_block_t)block; #endif // RCT_NEW_ARCH_ENABLED #ifdef RCT_NEW_ARCH_ENABLED -static __strong REAInitializerRCTFabricSurface *reaSurface; +// nothing #else typedef void (^AnimatedOperation)(REANodesManager *nodesManager); #endif // RCT_NEW_ARCH_ENABLED @@ -57,7 +53,6 @@ - (void)_tryAndHandleError:(dispatch_block_t)block; @implementation REAModule { #ifdef RCT_NEW_ARCH_ENABLED __weak RCTSurfacePresenter *_surfacePresenter; - std::weak_ptr weakReanimatedModuleProxy_; #else NSMutableArray *_operations; #endif // RCT_NEW_ARCH_ENABLED @@ -102,45 +97,14 @@ - (dispatch_queue_t)methodQueue - (std::shared_ptr)getUIManager { + react_native_assert(_surfacePresenter != nil); RCTScheduler *scheduler = [_surfacePresenter scheduler]; return scheduler.uiManager; } -- (void)injectDependencies:(jsi::Runtime &)runtime -{ - const auto &uiManager = [self getUIManager]; - react_native_assert(uiManager.get() != nil); - if (auto reanimatedModuleProxy = weakReanimatedModuleProxy_.lock()) { - reanimatedModuleProxy->initializeFabric(uiManager); - } -} - -#pragma mark-- Initialize - -- (void)installReanimatedAfterReload -{ - // called from REAInitializerRCTFabricSurface::start - __weak __typeof__(self) weakSelf = self; - _surfacePresenter = self.bridge.surfacePresenter; - [_nodesManager setSurfacePresenter:_surfacePresenter]; - - // to avoid deadlock we can't use Executor from React Native - // but we can create own and use it because initialization is already synchronized - react_native_assert(self.bridge != nil); - RCTRuntimeExecutorFromBridge(self.bridge)(^(jsi::Runtime &runtime) { - if (__typeof__(self) strongSelf = weakSelf) { - [strongSelf injectDependencies:runtime]; - } - }); -} - -- (void)handleJavaScriptDidLoadNotification:(NSNotification *)notification -{ - [self attachReactEventListener]; -} - -- (void)attachReactEventListener +- (void)attachReactEventListener:(const std::shared_ptr)reanimatedModuleProxy { + std::weak_ptr reanimatedModuleProxyWeak = reanimatedModuleProxy; RCTScheduler *scheduler = [_surfacePresenter scheduler]; __weak __typeof__(self) weakSelf = self; _surfacePresenter.runtimeExecutor(^(jsi::Runtime &runtime) { @@ -148,19 +112,20 @@ - (void)attachReactEventListener if (strongSelf == nil) { return; } - if (auto reanimatedModuleProxy = strongSelf->weakReanimatedModuleProxy_.lock()) { - auto eventListener = - std::make_shared([reanimatedModuleProxy](const RawEvent &rawEvent) { - if (!RCTIsMainQueue()) { - // event listener called on the JS thread, let's ignore this event - // as we cannot safely access worklet runtime here - // and also we don't care about topLayout events - return false; - } + auto eventListener = + std::make_shared([reanimatedModuleProxyWeak](const RawEvent &rawEvent) { + if (!RCTIsMainQueue()) { + // event listener called on the JS thread, let's ignore this event + // as we cannot safely access worklet runtime here + // and also we don't care about topLayout events + return false; + } + if (const auto reanimatedModuleProxy = reanimatedModuleProxyWeak.lock()) { return reanimatedModuleProxy->handleRawEvent(rawEvent, CACurrentMediaTime() * 1000); - }); - [scheduler addEventListener:eventListener]; - } + } + return false; + }); + [scheduler addEventListener:eventListener]; }); } @@ -178,35 +143,10 @@ - (void)setSurfacePresenter:(id)surfacePresenter - (void)setBridge:(RCTBridge *)bridge { [super setBridge:bridge]; - // only within the first loading `self.bridge.surfacePresenter` exists - // during the reload `self.bridge.surfacePresenter` is null - if (self.bridge.surfacePresenter) { - _surfacePresenter = self.bridge.surfacePresenter; - } - - [self setReaSurfacePresenter]; - _nodesManager = [[REANodesManager alloc] initWithModule:self bridge:bridge surfacePresenter:_surfacePresenter]; - - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(handleJavaScriptDidLoadNotification:) - name:RCTJavaScriptDidLoadNotification - object:nil]; - [[self.moduleRegistry moduleForName:"EventDispatcher"] addDispatchObserver:self]; } -- (void)setReaSurfacePresenter -{ - if (_surfacePresenter && ![_surfacePresenter surfaceForRootTag:reaSurface.rootTag]) { - if (!reaSurface) { - reaSurface = [[REAInitializerRCTFabricSurface alloc] init]; - } - [_surfacePresenter registerSurface:reaSurface]; - } - reaSurface.reaModule = self; -} - #else // RCT_NEW_ARCH_ENABLED - (void)setBridge:(RCTBridge *)bridge @@ -302,7 +242,14 @@ - (void)sendEventWithName:(NSString *)eventName body:(id)body auto &uiRuntime = [workletsModule getWorkletsModuleProxy]->getUIWorkletRuntime() -> getJSIRuntime(); jsi::Runtime &rnRuntime = *jsiRuntime; - [self commonInit:reanimatedModuleProxy withRnRuntime:rnRuntime withUIRuntime:uiRuntime]; + WorkletRuntimeCollector::install(rnRuntime); + RNRuntimeDecorator::decorate(rnRuntime, uiRuntime, reanimatedModuleProxy); +#ifdef RCT_NEW_ARCH_ENABLED + [self attachReactEventListener:reanimatedModuleProxy]; + const auto &uiManager = [self getUIManager]; + react_native_assert(uiManager.get() != nil); + reanimatedModuleProxy->initializeFabric(uiManager); +#endif // RCT_NEW_ARCH_ENABLED return @YES; } @@ -315,20 +262,4 @@ - (void)sendEventWithName:(NSString *)eventName body:(id)body } #endif // RCT_NEW_ARCH_ENABLED -- (void)commonInit:(std::shared_ptr)reanimatedModuleProxy - withRnRuntime:(jsi::Runtime &)rnRuntime - withUIRuntime:(jsi::Runtime &)uiRuntime -{ - WorkletRuntimeCollector::install(rnRuntime); - RNRuntimeDecorator::decorate(rnRuntime, uiRuntime, reanimatedModuleProxy); -#ifdef RCT_NEW_ARCH_ENABLED - [self attachReactEventListener]; - weakReanimatedModuleProxy_ = reanimatedModuleProxy; - if (self->_surfacePresenter != nil) { - // reload, uiManager is null right now, we need to wait for `installReanimatedAfterReload` - [self injectDependencies:rnRuntime]; - } -#endif // RCT_NEW_ARCH_ENABLED -} - @end diff --git a/packages/react-native-reanimated/apple/reanimated/apple/REANodesManager.h b/packages/react-native-reanimated/apple/reanimated/apple/REANodesManager.h index 8df3ebeb6ed..9677d3b6faa 100644 --- a/packages/react-native-reanimated/apple/reanimated/apple/REANodesManager.h +++ b/packages/react-native-reanimated/apple/reanimated/apple/REANodesManager.h @@ -42,7 +42,6 @@ typedef void (^REAPerformOperations)(); - (void)dispatchEvent:(id)event; #ifdef RCT_NEW_ARCH_ENABLED -- (void)setSurfacePresenter:(id)surfacePresenter; - (void)registerPerformOperations:(REAPerformOperations)performOperations; - (void)synchronouslyUpdateViewOnUIThread:(nonnull NSNumber *)viewTag props:(nonnull NSDictionary *)uiProps; #else diff --git a/packages/react-native-reanimated/apple/reanimated/apple/REANodesManager.mm b/packages/react-native-reanimated/apple/reanimated/apple/REANodesManager.mm index eb0a70c50ac..21de2d24528 100644 --- a/packages/react-native-reanimated/apple/reanimated/apple/REANodesManager.mm +++ b/packages/react-native-reanimated/apple/reanimated/apple/REANodesManager.mm @@ -242,13 +242,6 @@ - (void)invalidate }]; } -#ifdef RCT_NEW_ARCH_ENABLED -- (void)setSurfacePresenter:(id)surfacePresenter -{ - _surfacePresenter = surfacePresenter; -} -#endif // RCT_NEW_ARCH_ENABLED - - (void)operationsBatchDidComplete { if (![[self getDisplayLink] isPaused]) {