feat(background-thread): add SharedBridge JSI HostObject#43
feat(background-thread): add SharedBridge JSI HostObject#43huhuanming wants to merge 2 commits intomainfrom
Conversation
…ntime data transfer
Add a shared C++ HostObject that is installed in both the main and background
JSI runtimes, enabling direct memory-level communication without ObjC dispatch
overhead. Provides both push (existing postHostMessage/onHostMessage) and pull
(SharedBridge send/drain) communication models.
- cpp/SharedBridge.{h,cpp}: Cross-platform HostObject with thread-safe queues
- iOS: Install in background runtime via hostDidStart, main runtime via class method
- TypeScript: ISharedBridge type and getSharedBridge() helper
- Example: Updated test page with ping/pong demo and drain loop
…SharedBridge - CMakeLists.txt + cpp-adapter.cpp: JNI bridge for SharedBridge install, postHostMessage/onHostMessage JSI globals, and message passing - BackgroundThreadModule.kt: Full implementation with ReactHostImpl for background JS runtime, JSI bindings install via ReactInstanceEventListener, SharedBridge in both main and background runtimes - build.gradle: Add externalNativeBuild with CMake, prefab for fbjni/jsi - SharedBridge.h: Make constructor public for std::make_shared compatibility
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
| object : JSBundleLoader() { | ||
| override fun loadScript(delegate: com.facebook.react.bridge.JSBundleLoaderDelegate): String { | ||
| delegate.loadScriptFromFile(entryURL, entryURL, false) | ||
| return entryURL | ||
| } | ||
| } |
There was a problem hiding this comment.
🔴 Android dev-mode bundle loading passes HTTP URL as local file path to loadScriptFromFile
In BackgroundThreadModule.kt:93-94, when the entryURL starts with "http", the code calls delegate.loadScriptFromFile(entryURL, entryURL, false). The first parameter of loadScriptFromFile is the local filesystem path to read the script from, not a URL. Passing an HTTP URL (e.g. http://localhost:8082/...) as the file path will cause the native layer to attempt to open it as a local file, which will fail. React Native's own createRemoteDebuggerBundleLoader passes null as the file-name parameter for remote loading, not the URL. This breaks starting the background runtime in development mode on Android.
Prompt for agents
In native-modules/react-native-background-thread/android/src/main/java/com/backgroundthread/BackgroundThreadModule.kt, lines 90-97, the custom JSBundleLoader for HTTP URLs incorrectly passes the URL as the first argument (file path) to delegate.loadScriptFromFile(). The first parameter should be a local file path or null, not an HTTP URL. Replace the custom JSBundleLoader with JSBundleLoader.createRemoteDebuggerBundleLoader(entryURL, entryURL) which correctly passes null as the file name for remote loading, or use a network-aware loader that first downloads the bundle to a cache file and then loads from there.
Was this helpful? React with 👍 or 👎 to provide feedback.
Summary
SharedBridgeHostObject that is installed in both main and background JSI runtimes, sharing the same process-level queuessend/drain/hasMessages) alongside existing push-model (postHostMessage/onHostMessage)hostDidStart, main runtime via+installSharedBridgeInMainRuntime:class methodFiles
cpp/SharedBridge.{h,cpp}src/SharedBridge.tsISharedBridgeinterface +getSharedBridge()ios/BackgroundRunnerReactNativeDelegate.mmios/BackgroundThreadManager.{h,mm}+installSharedBridgeInMainRuntime:BackgroundThread.podspeccpp/sourcesexample/AppDelegate.swifthostDidStartto install in main runtimeexample/background.jsexample/BackgroundThreadTestPage.tsxTest plan
tsc --noEmit)