Skip to content

Commit 246209d

Browse files
committed
feat: BundleConsumer on iOS/Android with APIs
1 parent d4914b5 commit 246209d

22 files changed

Lines changed: 329 additions & 48 deletions

File tree

packages/react-native/Libraries/AppDelegate/RCTAppSetupUtils.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import <React/RCTHTTPRequestHandler.h>
2020
#import <React/RCTImageLoader.h>
2121
#import <React/RCTNetworking.h>
22+
#import <React/RCTBundleConsumer.h>
2223

2324
// Fabric
2425
#import <React/RCTFabricSurface.h>
@@ -73,7 +74,9 @@ void RCTAppSetupPrepareApp(UIApplication *application, BOOL turboModuleEnabled)
7374
classNames = dependencyProvider ? dependencyProvider.imageDataDecoderClassNames : @[];
7475
} else if (protocol == @protocol(RCTURLRequestHandler)) {
7576
classNames = dependencyProvider ? dependencyProvider.URLRequestHandlerClassNames : @[];
76-
}
77+
} else if (protocol = @protocol(RCTBundleConsumer)) {
78+
classNames = dependencyProvider ? dependencyProvider.bundleConsumerClassNames : @[];
79+
}
7780

7881
NSMutableArray *modules = [NSMutableArray new];
7982

packages/react-native/Libraries/AppDelegate/RCTDependencyProvider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ NS_ASSUME_NONNULL_BEGIN
2020

2121
- (NSArray<NSString *> *)URLRequestHandlerClassNames;
2222

23+
- (NSArray<NSString *> *)bundleConsumerClassNames;
24+
2325
- (NSArray<NSString *> *)unstableModulesRequiringMainQueueSetup;
2426

2527
- (NSDictionary<NSString *, Class<RCTComponentViewProtocol>> *)thirdPartyFabricComponents;

packages/react-native/React/Base/RCTBundleConsumer.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#import <React/RCTJavaScriptLoader.h>
9-
10-
@protocol RCTBundleConsumer <NSObject>
8+
#import <Foundation/Foundation.h>
9+
#import <React/NSBigStringBuffer.h>
1110

1211
/**
13-
* A reference to the RCTModuleRegistry. Useful for modules that require access
14-
* to other NativeModules. To implement this in your module, just add `@synthesize
15-
* moduleRegistry = _moduleRegistry;`. If using Swift, add
16-
* `@objc var moduleRegistry: RCTModuleRegistry!` to your module.
12+
* Provides the interface needed to register a Bundle Consumer module.
1713
*/
18-
@property (nonatomic, weak, readwrite) RCTSource *source;
14+
@protocol RCTBundleConsumer <NSObject>
15+
16+
@property (nonatomic, strong, readwrite) NSBigStringBuffer *scriptBuffer;
17+
18+
@property (nonatomic, strong, readwrite) NSString *sourceURL;
1919

2020
@end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
10+
#ifdef __cplusplus
11+
#import <jsireact/JSIExecutor.h>
12+
#import <memory>
13+
14+
using namespace facebook;
15+
using namespace facebook::react;
16+
#endif // __cplusplus
17+
18+
@interface NSBigStringBuffer : NSObject
19+
#ifdef __cplusplus
20+
21+
{
22+
std::shared_ptr<const BigStringBuffer> _buffer;
23+
}
24+
25+
- (instancetype)initWithSharedPtr:(const std::shared_ptr<const BigStringBuffer> &)buffer;
26+
- (const std::shared_ptr<const BigStringBuffer> &)getBuffer;
27+
#endif // __cplusplus
28+
29+
@end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "NSBigStringBuffer.h"
9+
10+
@implementation NSBigStringBuffer
11+
12+
- (instancetype)initWithSharedPtr:(const std::shared_ptr<const BigStringBuffer>&)buffer {
13+
if (self = [super init]) {
14+
_buffer = buffer;
15+
}
16+
return self;
17+
}
18+
19+
- (const std::shared_ptr<const BigStringBuffer>&)getBuffer {
20+
return _buffer;
21+
}
22+
23+
@end

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactPackageTurboModuleManagerDelegate.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ public abstract class ReactPackageTurboModuleManagerDelegate : TurboModuleManage
149149
return resolvedModule as TurboModule
150150
}
151151

152+
fun <TInterface> getModulesConformingToInterfaceNames(clazz: Class<TInterface>): List<String> {
153+
val moduleNames = mutableListOf<String>()
154+
155+
for (moduleProvider in moduleProviders) {
156+
val moduleInfos = packageModuleInfos[moduleProvider]?.values ?: continue
157+
for (moduleInfo in moduleInfos) {
158+
val module = moduleProvider.getModule(moduleInfo.name)
159+
if (clazz.isInstance(module)) {
160+
moduleNames.add(moduleInfo.name)
161+
}
162+
}
163+
}
164+
return moduleNames
165+
}
166+
152167
override fun unstable_isModuleRegistered(moduleName: String): Boolean {
153168
for (moduleProvider in moduleProviders) {
154169
val moduleInfo: ReactModuleInfo? = packageModuleInfos[moduleProvider]?.get(moduleName)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.bridge
9+
10+
import com.facebook.react.fabric.BigStringBufferWrapper
11+
12+
public interface BundleConsumer {
13+
public fun setScriptWrapper(scriptWrapper: BigStringBufferWrapper)
14+
public fun setSourceURL(sourceURL: String)
15+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.fabric
9+
10+
import android.annotation.SuppressLint
11+
import android.content.res.AssetManager
12+
import com.facebook.jni.HybridData
13+
import com.facebook.proguard.annotations.DoNotStripAny
14+
15+
/** TODO: Description */
16+
@SuppressLint("MissingNativeLoadLibrary")
17+
@DoNotStripAny
18+
public class BigStringBufferWrapper {
19+
20+
private val mHybridData: HybridData
21+
22+
public constructor(fileName: String) {
23+
mHybridData = initHybridFromFile(fileName)
24+
}
25+
26+
public constructor(assetManager: AssetManager, assetURL: String) {
27+
mHybridData = initHybridFromAssets(assetManager, assetURL)
28+
}
29+
30+
private external fun initHybridFromFile(fileName: String): HybridData
31+
32+
private external fun initHybridFromAssets(
33+
assetManager: AssetManager,
34+
assetURL: String
35+
): HybridData
36+
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManager.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ public class TurboModuleManager(
209209
return module
210210
}
211211

212+
public fun <TInterface> getModulesConformingToInterfaceNames(
213+
clazz: Class<TInterface>
214+
): List<String> {
215+
return delegate?.getModulesConformingToInterfaceNames(clazz) ?: emptyList()
216+
}
217+
212218
/**
213219
* Given a ModuleHolder, and the TurboModule's moduleName, return the TurboModule instance.
214220
*

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/turbomodule/core/TurboModuleManagerDelegate.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public abstract class TurboModuleManagerDelegate {
3636
*/
3737
public abstract fun getModule(moduleName: String): TurboModule?
3838

39+
public abstract fun <TInterface> getModulesConformingToInterfaceNames(
40+
clazz: Class<TInterface>
41+
): List<String>
42+
3943
public abstract fun unstable_isModuleRegistered(moduleName: String): Boolean
4044

4145
/**

0 commit comments

Comments
 (0)