Skip to content

Commit

Permalink
Disable turbo module so bridgeless works
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Apr 2, 2024
1 parent beb6d26 commit 76c648a
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 90 deletions.
6 changes: 4 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ android {

sourceSets.main {
java {
if (!isNewArchitectureEnabled()) {
// TODO removed the codegen config to allow the package to be built under bridgeless
// Once there is a stable API for C++ Turbo Modules, maybe this can be enabled again
// if (!isNewArchitectureEnabled()) {
srcDirs += 'src/paper/java'
}
// }
}
}
}
Expand Down
37 changes: 20 additions & 17 deletions android/src/main/java/com/op/sqlite/OPSQLiteModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@ package com.op.sqlite
import android.util.Log
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.bridge.ReactContextBaseJavaModule
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream

@ReactModule(name = OPSQLiteModule.NAME)
internal class OPSQLiteModule(context: ReactApplicationContext?) :
NativeOPSQLiteSpec(context) {
//@ReactModule(name = OPSQLiteModule.NAME)
internal class OPSQLiteModule(context: ReactApplicationContext?) : ReactContextBaseJavaModule(context) {
override fun getName(): String {
return NAME
}

override fun getTypedExportedConstants(): MutableMap<String, Any> {
fun getTypedExportedConstants(): MutableMap<String, Any> {
val constants: MutableMap<String, Any> = HashMap()
val context = reactApplicationContext
val dbPath = context
.getDatabasePath("defaultDatabase")
.absolutePath
.replace("defaultDatabase", "")
val dbPath =
context.getDatabasePath("defaultDatabase")
.absolutePath
.replace("defaultDatabase", "")
constants["ANDROID_DATABASE_PATH"] = dbPath
val filesPath = context.filesDir.absolutePath
constants["ANDROID_FILES_PATH"] = filesPath
Expand All @@ -33,8 +32,12 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) :
return constants
}

override fun getConstants(): MutableMap<String, Any>? {
return getTypedExportedConstants()
}

@ReactMethod(isBlockingSynchronousMethod = true)
override fun install(): Boolean {
fun install(): Boolean {
return try {
OPSQLiteBridge.instance.install(reactApplicationContext)
true
Expand All @@ -45,7 +48,7 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) :
}

@ReactMethod(isBlockingSynchronousMethod = true)
override fun moveAssetsDatabase(name: String, extension: String): Boolean {
fun moveAssetsDatabase(name: String, extension: String): Boolean {
val context = reactApplicationContext
val assetsManager = context.assets

Expand All @@ -54,10 +57,10 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) :
val inputStream: InputStream = assetsManager.open("custom/$name.$extension")

// Create the output file in the documents directory
val databasesFolder = context
.getDatabasePath("defaultDatabase")
.absolutePath
.replace("defaultDatabase", "")
val databasesFolder =
context.getDatabasePath("defaultDatabase")
.absolutePath
.replace("defaultDatabase", "")

val outputFile = File(databasesFolder, "$name.$extension")

Expand Down Expand Up @@ -85,7 +88,7 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) :
}
}

override fun invalidate() {
override fun invalidate() {
super.invalidate()
OPSQLiteBridge.instance.clearState()
}
Expand All @@ -97,4 +100,4 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) :

const val NAME = "OPSQLite"
}
}
}
33 changes: 10 additions & 23 deletions android/src/main/java/com/op/sqlite/OPSQLitePackage.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
package com.op.sqlite

import com.facebook.react.TurboReactPackage
import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.model.ReactModuleInfoProvider
import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.uimanager.ViewManager

class OPSQLitePackage : TurboReactPackage() {
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
if(name == OPSQLiteModule.NAME) {
return OPSQLiteModule(reactContext)
} else {
return null
}
class OPSQLitePackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf<NativeModule>(OPSQLiteModule(reactContext))
}

override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
mapOf(
OPSQLiteModule.NAME to ReactModuleInfo(
OPSQLiteModule.NAME,
OPSQLiteModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
true, // hasConstants
false, // isCxxModule
true // isTurboModule
)
)
override fun createViewManagers(
reactContext: ReactApplicationContext
): List<ViewManager<*, *>> {
return emptyList()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.op.sqlite.example;
package com.op.sqlite.example

import android.app.Application;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import android.app.Application
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactHost
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
import com.facebook.react.defaults.DefaultReactNativeHost
Expand All @@ -15,9 +15,7 @@ class MainApplication : Application(), ReactApplication {

override val reactNativeHost: ReactNativeHost =
object : DefaultReactNativeHost(this) {
override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
}
override fun getPackages(): List<ReactPackage> = PackageList(this).packages.apply {}

override fun getJSMainModuleName(): String = "index"

Expand All @@ -34,7 +32,7 @@ class MainApplication : Application(), ReactApplication {
super.onCreate()
SoLoader.init(this, false)
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
load(bridgelessEnabled = false)
load(bridgelessEnabled = true)
}
}
}
}
2 changes: 1 addition & 1 deletion example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ org.gradle.jvmargs=-Xmx2048m -XX:MaxMetaspaceSize=512m
android.useAndroidX=true
android.enableJetifier=true
reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64
newArchEnabled=false
newArchEnabled=true
hermesEnabled=true
5 changes: 5 additions & 0 deletions example/ios/OPSQLiteExample/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ - (BOOL)concurrentRootEnabled
return true;
}

-(BOOL)bridgelessEnabled
{
return YES;
}

@end
16 changes: 8 additions & 8 deletions ios/OPSQLite.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#ifdef RCT_NEW_ARCH_ENABLED
#import <OPSQLiteSpec/OPSQLiteSpec.h>
#else
// #ifdef RCT_NEW_ARCH_ENABLED
// #import <OPSQLiteSpec/OPSQLiteSpec.h>
// #else
#import <React/RCTBridge.h>
#endif
// #endif

@interface OPSQLite : NSObject
#ifdef RCT_NEW_ARCH_ENABLED
<NativeOPSQLiteSpec>
#else
// #ifdef RCT_NEW_ARCH_ENABLED
// <NativeOPSQLiteSpec>
// #else
<RCTBridgeModule>
#endif
// #endif

@property(nonatomic, assign) BOOL setBridgeOnMainQueue;

Expand Down
14 changes: 7 additions & 7 deletions ios/OPSQLite.mm
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ - (NSDictionary *)getConstants {
return @true;
}

#if RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeOPSQLiteSpecJSI>(params);
}
#endif
// #if RCT_NEW_ARCH_ENABLED
// - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
// (const facebook::react::ObjCTurboModule::InitParams &)params
// {
// return std::make_shared<facebook::react::NativeOPSQLiteSpecJSI>(params);
// }
// #endif

- (void)invalidate {
opsqlite::clearState();
Expand Down
8 changes: 0 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@
"react": "*",
"react-native": "*"
},
"codegenConfig": {
"name": "OPSQLiteSpec",
"type": "modules",
"jsSrcsDir": "src",
"android": {
"javaPackageName": "com.op.sqlite"
}
},
"prettier": {
"quoteProps": "consistent",
"singleQuote": true,
Expand Down
22 changes: 9 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import NativeOPSQLite from './NativeOPSQLite';
// import NativeOPSQLite from './NativeOPSQLite';
import { NativeModules } from 'react-native';

declare global {
function nativeCallSyncHook(): unknown;
var __OPSQLiteProxy: object | undefined;
}

if (global.__OPSQLiteProxy == null) {
if (NativeOPSQLite == null) {
if (NativeModules.OPSQLite == null) {
throw new Error('Base module not found. Maybe try rebuilding the app.');
}

if (NativeOPSQLite.install == null) {
if (NativeModules.OPSQLite.install == null) {
throw new Error(
'Failed to install op-sqlite: React Native is not running on-device. OPSQLite can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'
);
}

// Call the synchronous blocking install() function
const result = NativeOPSQLite.install();
const result = NativeModules.OPSQLite.install();
if (result !== true) {
throw new Error(
`Failed to install op-sqlite: The native OPSQLite Module could not be installed! Looks like something went wrong when installing JSI bindings, check the native logs for more info`
Expand All @@ -36,19 +37,14 @@ const proxy = global.__OPSQLiteProxy;
export const OPSQLite = proxy as ISQLite;

export const {
// @ts-expect-error
IOS_DOCUMENT_PATH,
// @ts-expect-error
IOS_LIBRARY_PATH,
// @ts-expect-error
ANDROID_DATABASE_PATH,
// @ts-expect-error
ANDROID_FILES_PATH,
// @ts-expect-error
ANDROID_EXTERNAL_FILES_PATH,
} = !!NativeOPSQLite.getConstants
? NativeOPSQLite.getConstants()
: NativeOPSQLite;
} = !!NativeModules.OPSQLite.getConstants
? NativeModules.OPSQLite.getConstants()
: NativeModules.OPSQLite;

/**
* Object returned by SQL Query executions {
Expand Down Expand Up @@ -472,5 +468,5 @@ export const moveAssetsDatabase = (
dbName: string,
extension: string
): boolean => {
return NativeOPSQLite.moveAssetsDatabase(dbName, extension);
return NativeModules.OPSQLite.moveAssetsDatabase(dbName, extension);
};

0 comments on commit 76c648a

Please sign in to comment.