Skip to content

Migrate LiveMarkdownModule to TurboModuleWithJSIBindings #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions android/src/main/cpp/LiveMarkdownModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "LiveMarkdownModule.h"
#include "RuntimeDecorator.h"

namespace expensify::livemarkdown {

// static
void LiveMarkdownModule::registerNatives() {
javaClassLocal()->registerNatives({
makeNativeMethod(
"getBindingsInstaller",
LiveMarkdownModule::getBindingsInstaller),
});
}

// static
jni::local_ref<react::BindingsInstallerHolder::javaobject>
LiveMarkdownModule::getBindingsInstaller(
jni::alias_ref<LiveMarkdownModule> /*jobj*/) {
return react::BindingsInstallerHolder::newObjectCxxArgs(
[](jsi::Runtime& runtime, const std::shared_ptr<react::CallInvoker>&) {
expensify::livemarkdown::injectJSIBindings(runtime);
});
}

} // namespace expensify::livemarkdown
24 changes: 24 additions & 0 deletions android/src/main/cpp/LiveMarkdownModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <ReactCommon/BindingsInstallerHolder.h>
#include <fbjni/fbjni.h>

using namespace facebook;

namespace expensify::livemarkdown {

class LiveMarkdownModule : public jni::JavaClass<LiveMarkdownModule> {
public:
static constexpr auto kJavaDescriptor =
"Lcom/expensify/livemarkdown/LiveMarkdownModule;";

LiveMarkdownModule() = default;

static void registerNatives();

private:
static jni::local_ref<react::BindingsInstallerHolder::javaobject>
getBindingsInstaller(jni::alias_ref<LiveMarkdownModule> jobj);
};

} // namespace expensify::livemarkdown
13 changes: 5 additions & 8 deletions android/src/main/cpp/OnLoad.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include <fbjni/fbjni.h>

#include "LiveMarkdownModule.h"
#include "MarkdownParser.h"
#include "RuntimeDecorator.h"

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return facebook::jni::initialize(
vm, [] { expensify::livemarkdown::MarkdownParser::registerNatives(); });
}

extern "C" JNIEXPORT void JNICALL Java_com_expensify_livemarkdown_LiveMarkdownModule_injectJSIBindings(JNIEnv *env, jobject thiz, jlong jsiRuntime) {
jsi::Runtime &rt = *reinterpret_cast<jsi::Runtime*>(jsiRuntime);
expensify::livemarkdown::injectJSIBindings(rt);
return facebook::jni::initialize(vm, [] {
expensify::livemarkdown::LiveMarkdownModule::registerNatives();
expensify::livemarkdown::MarkdownParser::registerNatives();
});
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.expensify.livemarkdown;

import androidx.annotation.NonNull;

import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.turbomodule.core.interfaces.BindingsInstallerHolder;
import com.facebook.react.turbomodule.core.interfaces.TurboModuleWithJSIBindings;
import com.facebook.soloader.SoLoader;

import java.util.Objects;

public class LiveMarkdownModule extends NativeLiveMarkdownModuleSpec {
public class LiveMarkdownModule extends NativeLiveMarkdownModuleSpec implements TurboModuleWithJSIBindings {
static {
SoLoader.loadLibrary("livemarkdown");
}
Expand All @@ -14,13 +17,8 @@ public LiveMarkdownModule(ReactApplicationContext reactContext) {
super(reactContext);
}

@DoNotStrip
@NonNull
@Override
public boolean install() {
long jsiRuntime = Objects.requireNonNull(getReactApplicationContext().getJavaScriptContextHolder(), "[react-native-live-markdown] JavaScriptContextHolder is null").get();
injectJSIBindings(jsiRuntime);

return true;
}

private native void injectJSIBindings(long jsiRuntime);
public native BindingsInstallerHolder getBindingsInstaller();
}
3 changes: 2 additions & 1 deletion apple/LiveMarkdownModule.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#import <RNLiveMarkdownSpec/RNLiveMarkdownSpec.h>

#import <React/RCTEventEmitter.h>
#import <ReactCommon/RCTTurboModuleWithJSIBindings.h>

// Without inheriting after RCTEventEmitter we don't get access to bridge
@interface LiveMarkdownModule : RCTEventEmitter <NativeLiveMarkdownModuleSpec>
@interface LiveMarkdownModule : RCTEventEmitter <NativeLiveMarkdownModuleSpec, RCTTurboModuleWithJSIBindings>

@end
8 changes: 3 additions & 5 deletions apple/LiveMarkdownModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ @implementation LiveMarkdownModule

RCT_EXPORT_MODULE()

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install)
- (void)installJSIBindingsWithRuntime:(facebook::jsi::Runtime &)runtime
callInvoker:(const std::shared_ptr<facebook::react::CallInvoker> &)callinvoker
{
RCTCxxBridge *cxxBridge = (RCTCxxBridge *)self.bridge;
jsi::Runtime &rt = *(jsi::Runtime *)cxxBridge.runtime;
expensify::livemarkdown::injectJSIBindings(rt);
return @(1);
expensify::livemarkdown::injectJSIBindings(runtime);
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
Expand Down
4 changes: 2 additions & 2 deletions src/MarkdownTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ function initializeLiveMarkdownIfNeeded() {
if (initialized) {
return;
}
if (NativeLiveMarkdownModule) {
NativeLiveMarkdownModule.install();
if (!NativeLiveMarkdownModule) {
throw new Error('[react-native-live-markdown] NativeLiveMarkdownModule is not available');
}
if (!global.jsi_setMarkdownRuntime) {
throw new Error('[react-native-live-markdown] global.jsi_setMarkdownRuntime is not available');
Expand Down
5 changes: 2 additions & 3 deletions src/NativeLiveMarkdownModule.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type {TurboModule} from 'react-native';
import {TurboModuleRegistry} from 'react-native';

interface Spec extends TurboModule {
install: () => boolean;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Spec extends TurboModule {}

export default TurboModuleRegistry.get<Spec>('LiveMarkdownModule');
Loading