Git Product home page Git Product logo

deepaksprojects / react-native-turbo-starter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from talknagish/react-native-turbo-starter

0.0 0.0 0.0 436 KB

React 0.68+ Turbo Module starter using codegen with typescript for Objective-C and Java/Kotlin with C++ shared library. ๐Ÿš€๐Ÿš€๐Ÿš€

License: MIT License

Shell 0.33% JavaScript 3.06% Ruby 6.01% C++ 16.89% C 2.10% Objective-C 1.08% Java 34.14% Kotlin 6.78% TypeScript 10.71% Objective-C++ 14.17% Swift 0.14% Makefile 3.41% CMake 1.18%

react-native-turbo-starter's Introduction

t-03

React 0.68+ Turbo Module starter using codegen with typescript for Objective-C and Java/Kotlin with C++ shared library. ๐Ÿš€๐Ÿš€๐Ÿš€

Features

  • React Native 0.68+
  • Upgraded react-native-create-library module to TurboModule
  • Codegen with typescript
  • Objective-C using JSI without the bridge
  • Java/Kotlin using JSI without the bridge
  • Shared C++ TurboModule
  • Array/Dictionaries/Promises usage
  • Native usage (battery level)
  • M1 e2e Compilation of RN New Architecture

Considerations and Notes

  1. This module doesn't offer backward compatibility. You will only be able to use it with the new architecture enabled.
  2. The shared C++ library requires JNI bindings on Android, and converting types on iOS from C++ to Objective-C.
  3. We didn't find an easy way to incorporate Swift instead of Objective-C (it's not as easy to call C++ code from Swift).
  4. We pin CMake version to support M1 machines. We also used CMake to compile the shared C++ library because it's the recommended tech (in oppose to ndk-build).
  5. In the .podspec file, the version of folly has to be exactly the same as the react-native's version
  6. In case you are using an M1 machine, you will need to add the following to your local.properties file to build on Android (temporary workaround as mentioned here):
ndk.dir=/Users/{USER}/Library/Android/sdk/ndk/24.0.8215888

Running the example project

yarn bootstrap

iOS

yarn ios

Android

yarn android

Adding new functionality

Native functionality

  1. Open src/NativeTurboStarter.ts
  2. Add your function definition inside the Spec
  3. Open src/index.ts and export your new function via a wrapper
  4. Call your new function from App.tsx to test that it works

iOS

  1. Run RCT_NEW_ARCH_ENABLED=1 pod install in example/ios folder
  2. You can run xed ios to open to workspace, and on the left side-bar navigate to Pods -> Development Pods -> react-native-turbo-starter -> ios -> TurboStarter.mm and next to the @implementation XCode will offer to complete the missing protocol for you
  3. Alternatively, you can open TurboStarter.mm and implement the new function by yourself.
  4. We're done! You can find the generated code in this path: example/ios/build/generated/ios/TurboStarter

Android

  1. run ./gradlew generateCodegenArtifactsFromSchema in example/android
  2. You can open Android Studio and access android/src/main/java/com/reactnativeturbostarter/TurboStarterModule.kt and let android studio complete the missing new function for you
  3. Alternatively, you can open TurboStarterModule.kt and implement it by yourself
  4. We're done! You can find the generated code in this path: android/build/generated/source/codegen

Now you can re-run the example project and watch as your code runs with JSI!

C++ functionality

Follow the steps above, but consider the following:

  1. Declare your C++ function here: cpp/react-native-turbo-starter.h
  2. Implement your C++ function here: cpp/react-native-turbo-starter.cpp

iOS

  1. In TurboStarter.mm, implement the function from the Spec, and you will be able to use it as a wrapper for calling the C++ code.
  2. You will need to convert the types when returning the function like so
- (NSNumber *) turboMultiply:(double)num1 num2:(double)num2{
    double res = turbostarter::multiply(num1, num2);
    return [NSNumber numberWithDouble:res];
}

Android

  1. In TurboStarterModule.kt you will need to declare your native JNI function
private external fun nativeMultiply(num1: Double, num2: Double): Double
  1. Then you will be able to create a wrapper that calls the C++ code
override fun turboMultiply(num1: Double, num2: Double): Double {
  return nativeMultiply(num1, num2)
}
  1. In android/src/main/jni/cpp-adapter.cpp you will need to add the JNI function that wraps around the C++ function
extern "C" JNIEXPORT jdouble JNICALL
Java_com_reactnativeturbostarter_TurboStarterModule_nativeMultiply(JNIEnv *env, jclass type, jdouble num1, jdouble num2)
{
    return turbostarter::multiply(num1, num2);
}

Steps taken to create this repository

  1. Create the repo using react-native-create-library (https://github.com/talknagish/react-native-turbo-starter/commit/f755c6ec6aa653d46a92474592de552cb3ba3f6c)
  2. Upgrade the library to react-native 0.68 (talknagish#1)
  3. Upgrade the example to react-native 0.68 and call 1 turbo module function (talknagish#2)
  4. Expend the Spec and call complex types (talknagish#3)
  5. Call native code (talknagish#4)
  6. Add the shared C++ library (talknagish#5)

Known Issues

  1. Full refresh (cmd + R) on Android crashes the application because of an issue in shadowTreeRegistry.cpp (didn't find a respective issue in their repo)
  2. When building for Android codegen doesn't always generate the new spec, so sometimes you have to delete the build folders until we find a better solution

BridgeSpy

It's all nice but how do we know this is actually not using the bridge anymore? To see that we enabled JSI correctly, you can log out everything that is passing through the bridge and see that it's now clean (besides some data from metro-bundler)

in App.tsx

  1. Add import MessageQueue from 'react-native/Libraries/BatchedBridge/MessageQueue.js';

const spyFunction = (msg) => {
  console.log(msg);
};
  1. Call MessageQueue.spy(spyFunction);

Clone and create your own library

  1. clone react-native-turbo-starter
  2. cd react-native-turbo-starter
  3. rm -rf .git
  4. rename all files containing starter word (script in the future)
  5. git init
  6. git remote add origin <new repo url>
  7. git add .
  8. git commit -m "Initial Commit"
  9. git push -u origin main

We're Hiring

Nagish makes communication more accessible to all.

We believe that people who are Deaf or Hard-of-Hearing deserve to communicate on their own, without relying on anyone else.

We're always looking for talented humans who are interested in building the future of accessible communication with us.

If you like this project and want to work on similar tech click below

https://talknagish.com/careers

License

MIT

react-native-turbo-starter's People

Contributors

alon7 avatar yotamishak avatar yaela251 avatar mrousavy avatar rvasseur31 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.