Skip to content
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

Convert library to TurboModule #910

Merged

Conversation

j-piasecki
Copy link
Contributor

@j-piasecki j-piasecki commented Feb 6, 2023

PR adding New Architecture support to the library 🎉

We at Software Mansion have been working on improving support for the new architecture for quite a while now. If you need help with anything related to New Architecture, like:

or you just want to ask any questions, hit us up on projects@swmansion.com


Summary

This PR adds FabricExample app with the new arch enabled and converts the library to TurboModule.

Notable changes:

  • renamed Android module from RNC_AsyncSQLiteDBStorage to RNCAsyncStorage, matching iOS name
  • split the Package file into two: one for the Java implementation, one for the Kotlin one. This is mainly because of @ReactModuleList annotation. Previously reflection and BuildConfig field were used to check if the Kotlin module should be used, but the annotation expects static declaration. I've achieved this using gradle source sets, to conditionally include one of the modules.
  • the generated spec is committed to the repository and used on the old architecture, which simplifies the inheritance. In case the spec changes, this file would need to be upgraded

Test Plan

Check the example app. On Android the new arch is enabled by default, while on iOS the RCT_NEW_ARCH_ENABLED=1 flag is required when installing pods.

@j-piasecki j-piasecki marked this pull request as ready for review February 9, 2023 10:02
@github-actions
Copy link

This PR has been marked as stale due to inactivity. Please address any comments within 7 days or it will be closed.

@github-actions github-actions bot added the Stale label Apr 11, 2023
@github-actions github-actions bot closed this Apr 19, 2023
Shawn Dempsey added 2 commits June 13, 2023 13:54
# Conflicts:
#	android/build.gradle
#	package.json
#	react-native.config.js
#	src/RCTAsyncStorage.ts
#	yarn.lock
@shwanton
Copy link
Contributor

shwanton commented Jun 14, 2023

Hi Jakub,

I have a branch with the iOS Turbomodule working, based on this PR: https://github.com/shwanton/async-storage/tree/%40shwanton/turbomodule-ios-working

CleanShot.2023-06-13.at.22.17.12.mp4

I plan to get the macOS TM working too since macOS now supports TM's & Fabric.
Got macOS working here: shwanton@6d2faaa

I'm not super familiar w/ Android TM's, what is the current state?

Would love to get this PR working so we can use this lib as Turbomodule!

@j-piasecki
Copy link
Contributor Author

As far as I remember, both Android and iOS were ready to be merged at the time of submitting the PR. You can check if the Android version works and if it doesn't I will look into fixing it 😄.

As a side note, what is the reason behind this change? In my opinion tmSpecs is less descriptive than rnasyncstorage as it doesn't in any way point to the library (in case there are any errors during codegen or compilation).

@@ -36,7 +36,7 @@ android.enableJetifier=true

# Enable new architecture, i.e. Fabric + TurboModule - implies USE_FABRIC=1.
# Note that this is incompatible with web debugging.
#newArchEnabled=true
newArchEnabled=true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want all examples to be newArchEnabled?

:hermes_enabled => false,
:turbomodule_enabled => true,
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The podspec requires the RCT_NEW_ARCH_ENABLED be passed to pod install. Enabling these options w/o the flag will not enable new arch.

https://github.com/j-piasecki/react-native-async-storage/blob/8a47c53c974c666419a290ff94b6f3548e797c5e/RNCAsyncStorage.podspec#L5

Copy link
Member

@tido64 tido64 Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: I misread. Let me check again. Will update this post.
Edit 2: I still can't repro this.

Are you sure about this? I've just tested in master and it seems to be working?

diff --git a/RNCAsyncStorage.podspec b/RNCAsyncStorage.podspec
index 5d74402..45be872 100644
--- a/RNCAsyncStorage.podspec
+++ b/RNCAsyncStorage.podspec
@@ -2,6 +2,10 @@ require 'json'

 package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

+p '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
+p ENV['RCT_NEW_ARCH_ENABLED']
+p '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
+
 Pod::Spec.new do |s|
   s.name         = "RNCAsyncStorage"
   s.version      = package['version']
diff --git a/example/macos/Podfile b/example/macos/Podfile
index 19f6452..0eda101 100644
--- a/example/macos/Podfile
+++ b/example/macos/Podfile
@@ -2,7 +2,13 @@ require_relative '../../node_modules/react-native-test-app/macos/test_app.rb'

 workspace 'AsyncStorageExample.xcworkspace'

-use_test_app! do |target|
+options = {
+  :fabric_enabled => true,
+  :hermes_enabled => false,
+  :turbomodule_enabled => true,
+}
+
+use_test_app! options do |target|
   target.app do
     pod 'RNCAsyncStorage', :path => '../..'
     pod 'AsyncStorageExample', :path => '..'

Output:

% pod install
...
Auto-linking React Native module for target `ReactTestApp`: ReactTestApp-DevSupport
Analyzing dependencies
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
"1"
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
Downloading dependencies
Generating Pods project
Setting REACT_NATIVE build settings
Setting CLANG_CXX_LANGUAGE_STANDARD to c++17 on /Users/tido/Source/async-storage/node_modules/.generated/macos/ReactTestApp.xcodeproj
Pod install took 11 [s] to run

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, when I put the log inside of the Pod::Spec block, it returns 0 for macOS

pod install --verbose
...
-> Fetching podspec for `RNCAsyncStorage` from `../..`
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
"0"
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
diff --git a/RNCAsyncStorage.podspec b/RNCAsyncStorage.podspec
index 25c9420..502ae48 100644
--- a/RNCAsyncStorage.podspec
+++ b/RNCAsyncStorage.podspec
@@ -16,6 +16,10 @@ Pod::Spec.new do |s|
   s.source       = { :git => "https://github.com/react-native-async-storage/async-storage.git", :tag => "v#{s.version}" }
   s.source_files  = "ios/**/*.{h,m,mm}"
 
+  p '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
+  p ENV['RCT_NEW_ARCH_ENABLED']
+  p '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
+
   if fabric_enabled
     folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
 
diff --git a/example/macos/Podfile b/example/macos/Podfile
index d25aa3d..a79620d 100644
--- a/example/macos/Podfile
+++ b/example/macos/Podfile
@@ -3,9 +3,9 @@ require_relative '../../node_modules/react-native-test-app/macos/test_app.rb'
 workspace 'AsyncStorageExample.xcworkspace'
 
 options = {
-  :fabric_enabled => false,
+  :fabric_enabled => true,
   :hermes_enabled => false,
-  :turbomodule_enabled => false,
+  :turbomodule_enabled => true,
 }
 
 use_test_app! do |target|

Same Podspec when run from iOS project:

pod install --verbose
...
-> Fetching podspec for `RNCAsyncStorage` from `../..`
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
"1"
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just cleaned out my clone in case I had something stale. Still getting the same results.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker, I can just pass the flag when I install.

@tido64
Copy link
Member

tido64 commented Jun 21, 2023

FYI: pod install --project-directory=… current fails: facebook/react-native#37992

RNCAsyncStorage.podspec Outdated Show resolved Hide resolved
RNCAsyncStorage.podspec Outdated Show resolved Hide resolved
android/build.gradle Outdated Show resolved Hide resolved
android/build.gradle Outdated Show resolved Hide resolved
example/android/build.gradle Outdated Show resolved Hide resolved
example/android/build.gradle Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
src/RCTAsyncStorage.ts Outdated Show resolved Hide resolved
facebook-github-bot pushed a commit to facebook/react-native that referenced this pull request Jun 23, 2023
Summary:
![image](https://user-images.githubusercontent.com/4123478/217618490-4f99e408-1a07-4acf-a05c-e8837562a931.png)

`pod install --project-directory=ios` currently fails when new arch is enabled: react-native-async-storage/async-storage#910

- Patch for 0.71: #37993
- Patch for 0.72: #37994

## Changelog:

[IOS] [FIXED] - Fix `pod install --project-directory=...` when New Arch is enabled

Pull Request resolved: #37992

Test Plan:
```
git clone https://github.com/react-native-async-storage/async-storage.git
cd async-storage
gh pr checkout 910
yarn
RCT_NEW_ARCH_ENABLED=1 pod install --project-directory=example/ios
```

Reviewed By: cortinico

Differential Revision: D46966225

Pulled By: cipolleschi

fbshipit-source-id: 00b4650189175c09c9ec928f85d075890ba4c7c1
@shwanton
Copy link
Contributor

I merged master on my branch and TM worked great on iOS/macOS. Any other blockers to merging?

Copy link
Member

@tido64 tido64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The iOS bits look fine to me. It'd be great if you could get @krizzu to review the Android bits as well.

example/android/build.gradle Outdated Show resolved Hide resolved
example/android/gradle.properties Show resolved Hide resolved
AsyncStorageModule.class,
}
)
public class AsyncStoragePackage extends TurboReactPackage implements ViewManagerOnDemandReactPackage {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to implement ViewManagerOnDemandReactPackage ? Seems unnecessary, as we only provide functional component

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I don't actually remember why I added that. Anyway, removed it in b84f11e.

RNCAsyncStorage.podspec Show resolved Hide resolved
Copy link
Member

@krizzu krizzu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't mind changing paper to oldarch that'd be great, but not a blocker.
Great work on this 👏

Copy link
Member

@tido64 tido64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you both for adding TM support ❤️

@tido64 tido64 merged commit 86a7e90 into react-native-async-storage:master Jul 3, 2023
@AsyncStorageBot
Copy link
Collaborator

🎉 This PR is included in version 1.19.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

sourceDir: path.join('example', 'ios'),
},
macos: {
sourceDir: path.join('example', 'maacos'),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a typo. Does it work correctly on macos?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I'll forward fix & test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants