Skip to content

Commit

Permalink
Support launchOptions in bridgeless mode (#43757)
Browse files Browse the repository at this point in the history
Summary:
Support launchOptions in bridgeless mode
bypass-github-export-checks
## Changelog:

[IOS] [FIXED] - Support launchOptions in bridgeless mode

Pull Request resolved: #43757

Test Plan:
```
useEffect(() => {
    const processInitialURL = async () => {
      const url = await Linking.getInitialURL();
      if (url !== null) {
        console.log(`Initial url is: ${url}`);
      }
    };

    processInitialURL();
  }, []);
```

Reviewed By: javache

Differential Revision: D55790758

Pulled By: cipolleschi

fbshipit-source-id: 0f6aa6bdcebfc5bc42d632bea9193f122c1eb84f
  • Loading branch information
zhongwuzw authored and facebook-github-bot committed Apr 8, 2024
1 parent 5ea5117 commit 90296be
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ - (UIView *)viewWithModuleName:(NSString *)moduleName
RCTEnableTurboModuleInterop(YES);
RCTEnableTurboModuleInteropBridgeProxy(YES);

[self createReactHostIfNeeded];
[self createReactHostIfNeeded:launchOptions];

RCTFabricSurface *surface = [_reactHost createSurfaceWithModuleName:moduleName initialProperties:initProps];

Expand Down Expand Up @@ -207,7 +207,7 @@ - (void)createBridgeAdapterIfNeeded

#pragma mark - New Arch Utilities

- (void)createReactHostIfNeeded
- (void)createReactHostIfNeeded:(NSDictionary *)launchOptions
{
if (_reactHost) {
return;
Expand All @@ -219,7 +219,8 @@ - (void)createReactHostIfNeeded
turboModuleManagerDelegate:_turboModuleManagerDelegate
jsEngineProvider:^std::shared_ptr<facebook::react::JSRuntimeFactory>() {
return [weakSelf createJSRuntimeFactory];
}];
}
launchOptions:launchOptions];
[_reactHost setBundleURLProvider:^NSURL *() {
return [weakSelf bundleURL];
}];
Expand Down
7 changes: 6 additions & 1 deletion packages/react-native/React/Base/RCTBridgeProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#import "RCTBridgeModule.h"

NS_ASSUME_NONNULL_BEGIN

@class RCTBundleManager;
@class RCTCallableJSModules;
@class RCTModuleRegistry;
Expand All @@ -22,7 +24,8 @@
callableJSModules:(RCTCallableJSModules *)callableJSModules
dispatchToJSThread:(void (^)(dispatch_block_t))dispatchToJSThread
registerSegmentWithId:(void (^)(NSNumber *, NSString *))registerSegmentWithId
runtime:(void *)runtime NS_DESIGNATED_INITIALIZER;
runtime:(void *)runtime
launchOptions:(nullable NSDictionary *)launchOptions NS_DESIGNATED_INITIALIZER;

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel;
- (void)forwardInvocation:(NSInvocation *)invocation;
Expand All @@ -37,3 +40,5 @@
- (id)moduleForName:(NSString *)moduleName lazilyLoadIfNecessary:(BOOL)lazilyLoad;

@end

NS_ASSUME_NONNULL_END
6 changes: 4 additions & 2 deletions packages/react-native/React/Base/RCTBridgeProxy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ @implementation RCTBridgeProxy {
RCTModuleRegistry *_moduleRegistry;
RCTBundleManager *_bundleManager;
RCTCallableJSModules *_callableJSModules;
NSDictionary *_launchOptions;
void (^_dispatchToJSThread)(dispatch_block_t);
void (^_registerSegmentWithId)(NSNumber *, NSString *);
void *_runtime;
Expand All @@ -47,6 +48,7 @@ - (instancetype)initWithViewRegistry:(RCTViewRegistry *)viewRegistry
dispatchToJSThread:(void (^)(dispatch_block_t))dispatchToJSThread
registerSegmentWithId:(void (^)(NSNumber *, NSString *))registerSegmentWithId
runtime:(void *)runtime
launchOptions:(nullable NSDictionary *)launchOptions
{
self = [super self];
if (self) {
Expand All @@ -57,6 +59,7 @@ - (instancetype)initWithViewRegistry:(RCTViewRegistry *)viewRegistry
_dispatchToJSThread = dispatchToJSThread;
_registerSegmentWithId = registerSegmentWithId;
_runtime = runtime;
_launchOptions = [launchOptions copy];
}
return self;
}
Expand Down Expand Up @@ -191,8 +194,7 @@ - (void)registerSegmentWithId:(NSUInteger)segmentId path:(NSString *)path

- (NSDictionary *)launchOptions
{
[self logError:@"This method is not supported. Returning nil." cmd:_cmd];
return nil;
return _launchOptions;
}

- (BOOL)loading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ - (BOOL)_shouldCreateObjCModule:(Class)moduleClass
*/
if (_bridge) {
[(id)module setValue:_bridge forKey:@"bridge"];
} else if (_bridgeProxy && [self _isLegacyModuleClass:[module class]]) {
} else if (_bridgeProxy) {
[(id)module setValue:_bridgeProxy forKey:@"bridge"];
}
} @catch (NSException *exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ - (void)setUp
turboModuleManagerDelegate:OCMProtocolMock(@protocol(RCTTurboModuleManagerDelegate))
jsEngineProvider:^std::shared_ptr<facebook::react::JSRuntimeFactory>() {
return std::make_shared<facebook::react::RCTHermesInstance>();
}];
}
launchOptions:nil];
}

- (void)tearDown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ typedef std::shared_ptr<facebook::react::JSRuntimeFactory> (^RCTHostJSEngineProv
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
hostDelegate:(id<RCTHostDelegate>)hostDelegate
turboModuleManagerDelegate:(id<RCTTurboModuleManagerDelegate>)turboModuleManagerDelegate
jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider NS_DESIGNATED_INITIALIZER;
jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider
launchOptions:(nullable NSDictionary *)launchOptions NS_DESIGNATED_INITIALIZER;

@property (nonatomic, weak, nullable) id<RCTHostRuntimeDelegate> runtimeDelegate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ @implementation RCTHost {
RCTHostBundleURLProvider _bundleURLProvider;
RCTHostJSEngineProvider _jsEngineProvider;

NSDictionary *_launchOptions;

// All the surfaces that need to be started after main bundle execution
NSMutableArray<RCTFabricSurface *> *_surfaceStartBuffer;
std::mutex _surfaceStartBufferMutex;
Expand Down Expand Up @@ -85,6 +87,7 @@ - (instancetype)initWithBundleURL:(NSURL *)bundleURL
hostDelegate:(id<RCTHostDelegate>)hostDelegate
turboModuleManagerDelegate:(id<RCTTurboModuleManagerDelegate>)turboModuleManagerDelegate
jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider
launchOptions:(nullable NSDictionary *)launchOptions
{
if (self = [super init]) {
_hostDelegate = hostDelegate;
Expand All @@ -93,6 +96,7 @@ - (instancetype)initWithBundleURL:(NSURL *)bundleURL
_bundleManager = [RCTBundleManager new];
_moduleRegistry = [RCTModuleRegistry new];
_jsEngineProvider = [jsEngineProvider copy];
_launchOptions = [launchOptions copy];

__weak RCTHost *weakSelf = self;

Expand Down Expand Up @@ -204,7 +208,8 @@ - (void)start
turboModuleManagerDelegate:_turboModuleManagerDelegate
onInitialBundleLoad:_onInitialBundleLoad
moduleRegistry:_moduleRegistry
parentInspectorTarget:_inspectorTarget.get()];
parentInspectorTarget:_inspectorTarget.get()
launchOptions:_launchOptions];
[_hostDelegate hostDidStart:self];
}

Expand Down Expand Up @@ -284,7 +289,8 @@ - (void)didReceiveReloadCommand
turboModuleManagerDelegate:_turboModuleManagerDelegate
onInitialBundleLoad:_onInitialBundleLoad
moduleRegistry:_moduleRegistry
parentInspectorTarget:_inspectorTarget.get()];
parentInspectorTarget:_inspectorTarget.get()
launchOptions:_launchOptions];
[_hostDelegate hostDidStart:self];

for (RCTFabricSurface *surface in [self _getAttachedSurfaces]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ typedef void (^_Null_unspecified RCTInstanceInitialBundleLoadCompletionBlock)();
turboModuleManagerDelegate:(id<RCTTurboModuleManagerDelegate>)turboModuleManagerDelegate
onInitialBundleLoad:(RCTInstanceInitialBundleLoadCompletionBlock)onInitialBundleLoad
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
parentInspectorTarget:(facebook::react::jsinspector_modern::HostTarget *)parentInspectorTarget;
parentInspectorTarget:(facebook::react::jsinspector_modern::HostTarget *)parentInspectorTarget
launchOptions:(nullable NSDictionary *)launchOptions;

- (void)callFunctionOnJSModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ @implementation RCTInstance {
std::mutex _invalidationMutex;
std::atomic<bool> _valid;
RCTJSThreadManager *_jsThreadManager;
NSDictionary *_launchOptions;

// APIs supporting interop with native modules and view managers
RCTBridgeModuleDecorator *_bridgeModuleDecorator;
Expand All @@ -100,6 +101,7 @@ - (instancetype)initWithDelegate:(id<RCTInstanceDelegate>)delegate
onInitialBundleLoad:(RCTInstanceInitialBundleLoadCompletionBlock)onInitialBundleLoad
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
parentInspectorTarget:(jsinspector_modern::HostTarget *)parentInspectorTarget
launchOptions:(nullable NSDictionary *)launchOptions
{
if (self = [super init]) {
_performanceLogger = [RCTPerformanceLogger new];
Expand All @@ -125,6 +127,7 @@ - (instancetype)initWithDelegate:(id<RCTInstanceDelegate>)delegate
[weakSelf callFunctionOnJSModule:moduleName method:methodName args:args];
}];
}
_launchOptions = launchOptions;

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

Expand Down Expand Up @@ -277,7 +280,8 @@ - (void)_start
[strongSelf registerSegmentWithId:segmentId path:path];
}
}
runtime:_reactInstance->getJavaScriptContext()];
runtime:_reactInstance->getJavaScriptContext()
launchOptions:_launchOptions];
bridgeProxy.jsCallInvoker = jsCallInvoker;
[RCTBridge setCurrentBridge:(RCTBridge *)bridgeProxy];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@property int initCount;
@property int invalidateCount;

@property NSDictionary *launchOptions;
@property NSString *jsModuleName;
@property NSString *method;
@property NSArray *args;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ - (instancetype)init
[ShimRCTInstance class],
@selector(initWithDelegate:
jsRuntimeFactory:bundleManager:turboModuleManagerDelegate:onInitialBundleLoad:moduleRegistry
:parentInspectorTarget:));
:parentInspectorTarget:launchOptions:));
RCTSwizzleInstanceSelector([RCTInstance class], [ShimRCTInstance class], @selector(invalidate));
RCTSwizzleInstanceSelector(
[RCTInstance class], [ShimRCTInstance class], @selector(callFunctionOnJSModule:method:args:));
Expand All @@ -40,7 +40,7 @@ - (void)reset
[ShimRCTInstance class],
@selector(initWithDelegate:
jsRuntimeFactory:bundleManager:turboModuleManagerDelegate:onInitialBundleLoad:moduleRegistry
:parentInspectorTarget:));
:parentInspectorTarget:launchOptions:));
RCTSwizzleInstanceSelector([RCTInstance class], [ShimRCTInstance class], @selector(invalidate));
RCTSwizzleInstanceSelector(
[RCTInstance class], [ShimRCTInstance class], @selector(callFunctionOnJSModule:method:args:));
Expand All @@ -55,6 +55,7 @@ - (instancetype)initWithDelegate:(id<RCTInstanceDelegate>)delegate
onInitialBundleLoad:(RCTInstanceInitialBundleLoadCompletionBlock)onInitialBundleLoad
moduleRegistry:(RCTModuleRegistry *)moduleRegistry
parentInspectorTarget:(facebook::react::jsinspector_modern::HostTarget *)parentInspectorTarget
launchOptions:(NSDictionary *)launchOptions
{
weakShim.initCount++;
return self;
Expand Down

0 comments on commit 90296be

Please sign in to comment.