Skip to content

Commit

Permalink
Remove REAInitializerRCTFabricSurface (#6837)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
piaskowyk authored Jan 13, 2025
1 parent 15b3351 commit b02cdb0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 191 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
119 changes: 25 additions & 94 deletions packages/react-native-reanimated/apple/reanimated/apple/REAModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
#endif // REACT_NATIVE_MINOR_VERSION >= 75
#endif // RCT_NEW_ARCH_ENABLED

#ifdef RCT_NEW_ARCH_ENABLED
#import <reanimated/apple/Fabric/REAInitializerRCTFabricSurface.h>
#endif // RCT_NEW_ARCH_ENABLED

#import <reanimated/RuntimeDecorators/RNRuntimeDecorator.h>
#import <reanimated/apple/REAModule.h>
#import <reanimated/apple/REANodesManager.h>
Expand Down Expand Up @@ -49,15 +45,14 @@ - (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

@implementation REAModule {
#ifdef RCT_NEW_ARCH_ENABLED
__weak RCTSurfacePresenter *_surfacePresenter;
std::weak_ptr<ReanimatedModuleProxy> weakReanimatedModuleProxy_;
#else
NSMutableArray<AnimatedOperation> *_operations;
#endif // RCT_NEW_ARCH_ENABLED
Expand Down Expand Up @@ -102,65 +97,35 @@ - (dispatch_queue_t)methodQueue

- (std::shared_ptr<UIManager>)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>)reanimatedModuleProxy
{
std::weak_ptr<ReanimatedModuleProxy> reanimatedModuleProxyWeak = reanimatedModuleProxy;
RCTScheduler *scheduler = [_surfacePresenter scheduler];
__weak __typeof__(self) weakSelf = self;
_surfacePresenter.runtimeExecutor(^(jsi::Runtime &runtime) {
__typeof__(self) strongSelf = weakSelf;
if (strongSelf == nil) {
return;
}
if (auto reanimatedModuleProxy = strongSelf->weakReanimatedModuleProxy_.lock()) {
auto eventListener =
std::make_shared<facebook::react::EventListener>([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<facebook::react::EventListener>([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];
});
}

Expand All @@ -178,35 +143,10 @@ - (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)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
Expand Down Expand Up @@ -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;
}
Expand All @@ -315,20 +262,4 @@ - (void)sendEventWithName:(NSString *)eventName body:(id)body
}
#endif // RCT_NEW_ARCH_ENABLED

- (void)commonInit:(std::shared_ptr<ReanimatedModuleProxy>)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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ typedef void (^REAPerformOperations)();
- (void)dispatchEvent:(id<RCTEvent>)event;

#ifdef RCT_NEW_ARCH_ENABLED
- (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter;
- (void)registerPerformOperations:(REAPerformOperations)performOperations;
- (void)synchronouslyUpdateViewOnUIThread:(nonnull NSNumber *)viewTag props:(nonnull NSDictionary *)uiProps;
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,6 @@ - (void)invalidate
}];
}

#ifdef RCT_NEW_ARCH_ENABLED
- (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter
{
_surfacePresenter = surfacePresenter;
}
#endif // RCT_NEW_ARCH_ENABLED

- (void)operationsBatchDidComplete
{
if (![[self getDisplayLink] isPaused]) {
Expand Down

0 comments on commit b02cdb0

Please sign in to comment.