Skip to content

Commit

Permalink
fix: reload on new arch (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
WoLewicki authored May 20, 2024
1 parent 9c0b263 commit b2f9d16
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions ios/RCTLiveMarkdownModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,54 @@
@implementation RCTLiveMarkdownModule {
BOOL installed_;
std::shared_ptr<livemarkdown::MarkdownCommitHook> 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<livemarkdown::MarkdownCommitHook>(scheduler.uiManager);
std::make_shared<livemarkdown::MarkdownCommitHook>(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<RCTSurfacePresenterStub>)surfacePresenter
{
surfacePresenter_ = surfacePresenter;
}


- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params {
return std::make_shared<facebook::react::NativeLiveMarkdownModuleSpecJSI>(
Expand All @@ -40,6 +70,7 @@ - (NSNumber *)install {

- (void)invalidate {
MarkdownShadowFamilyRegistry::reset();
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super invalidate];
}

Expand Down

0 comments on commit b2f9d16

Please sign in to comment.