From b2f9d168e36c98203bdaadd424111339e0ea5643 Mon Sep 17 00:00:00 2001 From: Wojciech Lewicki Date: Mon, 20 May 2024 22:32:45 +0200 Subject: [PATCH] fix: reload on new arch (#349) --- ios/RCTLiveMarkdownModule.mm | 45 ++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/ios/RCTLiveMarkdownModule.mm b/ios/RCTLiveMarkdownModule.mm index fa6eb39e..24bbcb84 100644 --- a/ios/RCTLiveMarkdownModule.mm +++ b/ios/RCTLiveMarkdownModule.mm @@ -14,24 +14,54 @@ @implementation RCTLiveMarkdownModule { BOOL installed_; std::shared_ptr commitHook_; + __weak RCTSurfacePresenter *surfacePresenter_; } RCT_EXPORT_MODULE(@"LiveMarkdownModule") - (NSNumber *)install { - if (!installed_) { - installed_ = YES; - - RCTBridge *bridge = self.bridge; - RCTSurfacePresenter *surfacePresenter = bridge.surfacePresenter; - RCTScheduler *scheduler = [surfacePresenter scheduler]; + if (!installed_ && surfacePresenter_ != nil) { + RCTScheduler *scheduler = [surfacePresenter_ scheduler]; commitHook_ = - std::make_shared(scheduler.uiManager); + std::make_shared(scheduler.uiManager); + installed_ = YES; } return @1; } +- (void)handleJavaScriptDidLoadNotification:(NSNotification *)notification +{ + surfacePresenter_ = self.bridge.surfacePresenter; + [self install]; +} + +- (void)setBridge:(RCTBridge *)bridge +{ + [super setBridge:bridge]; + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleJavaScriptDidLoadNotification:) + name:RCTJavaScriptDidLoadNotification + object:nil]; + + // 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; + } +} + +/* + * Taken from RCTNativeAnimatedTurboModule: + * This selector is invoked via BridgelessTurboModuleSetup. + */ +- (void)setSurfacePresenter:(id)surfacePresenter +{ + surfacePresenter_ = surfacePresenter; +} + + - (std::shared_ptr)getTurboModule: (const facebook::react::ObjCTurboModule::InitParams &)params { return std::make_shared( @@ -40,6 +70,7 @@ - (NSNumber *)install { - (void)invalidate { MarkdownShadowFamilyRegistry::reset(); + [[NSNotificationCenter defaultCenter] removeObserver:self]; [super invalidate]; }