Skip to content

Commit 4a1bafe

Browse files
RSNarafacebook-github-bot
authored andcommitted
Introduce RCTBundleManager
Summary: NativeModules can read from, or write to, the bridge's bundleURL. To do so, they access the bundleURL property on the bridge directly. **Setter:** https://www.internalfb.com/code/fbsource/[2a74581dfff31febe6e91c5739294705f5244ecb]/xplat/js/react-native-github/React/CoreModules/RCTDevMenu.mm?lines=213-214 **Getter:** https://www.internalfb.com/code/fbsource/[2a74581dfff31febe6e91c5739294705f5244ecb]/xplat/js/react-native-github/React/CoreModules/RCTDevMenu.mm?lines=245%2C256 In bridgeless mode, the bridge is nil. So, this form of access will fail. After this stack lands, every NativeModule will be able to synthesize an RCTBundleManager, like so: ``` synthesize bundleManager = _bundleManager; ``` From then on, all NativeModules will use this bundleManager to access the bundle URL. This should ensure that NativeModules can read from/write to the bundleURL, **even in bridgeless mode**. Changelog: [iOS][Added] - Introduce RCTBundleManager for bundleURL access Reviewed By: PeteTheHeat Differential Revision: D28086318 fbshipit-source-id: 34c15bf05c144341aa912fad9aa25d6d3a364f9e
1 parent 7db89f9 commit 4a1bafe

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

React/Base/RCTBridgeModule.h

+24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@protocol RCTBridgeMethod;
1515
@class RCTModuleRegistry;
1616
@class RCTViewRegistry;
17+
@class RCTBundleManager;
1718

1819
/**
1920
* The type of a block that is capable of sending a response to a bridged
@@ -135,6 +136,16 @@ RCT_EXTERN_C_END
135136
*/
136137
@property (nonatomic, weak, readwrite) RCTViewRegistry *viewRegistry_DEPRECATED;
137138

139+
/**
140+
* A reference to the RCTBundleManager. Useful for modules that need to read
141+
* or write to the app's bundle URL.
142+
*
143+
* To implement this in your module, just add `@synthesize bundleManager =
144+
* _bundleManager;`. If using Swift, add `@objc var bundleManager:
145+
* RCTBundleManager!` to your module.
146+
*/
147+
@property (nonatomic, weak, readwrite) RCTBundleManager *bundleManager;
148+
138149
/**
139150
* A reference to the RCTBridge. Useful for modules that require access
140151
* to bridge features, such as sending events or making JS calls. This
@@ -406,6 +417,19 @@ RCT_EXTERN_C_END
406417
- (id)moduleForName:(const char *)moduleName lazilyLoadIfNecessary:(BOOL)lazilyLoad;
407418
@end
408419

420+
typedef void (^RCTBridgelessBundleURLSetter)(NSURL *bundleURL);
421+
typedef NSURL * (^RCTBridgelessBundleURLGetter)();
422+
423+
/**
424+
* A class that allows NativeModules/TurboModules to read/write the bundleURL, with or without the bridge.
425+
*/
426+
@interface RCTBundleManager : NSObject
427+
- (void)setBridge:(RCTBridge *)bridge;
428+
- (void)setBridgelessBundleURLGetter:(RCTBridgelessBundleURLGetter)getter
429+
andSetter:(RCTBridgelessBundleURLSetter)setter;
430+
@property NSURL *bundleURL;
431+
@end
432+
409433
typedef UIView * (^RCTBridgelessComponentViewProvider)(NSNumber *);
410434

411435
/**

React/Base/RCTBundleManager.m

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its 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 "RCTAssert.h"
9+
#import "RCTBridge+Private.h"
10+
#import "RCTBridge.h"
11+
#import "RCTBridgeModule.h"
12+
13+
@implementation RCTBundleManager {
14+
__weak RCTBridge *_bridge;
15+
RCTBridgelessBundleURLGetter _bridgelessBundleURLGetter;
16+
RCTBridgelessBundleURLSetter _bridgelessBundleURLSetter;
17+
}
18+
19+
- (void)setBridge:(RCTBridge *)bridge
20+
{
21+
_bridge = bridge;
22+
}
23+
24+
- (void)setBridgelessBundleURLGetter:(RCTBridgelessBundleURLGetter)getter andSetter:(RCTBridgelessBundleURLSetter)setter
25+
{
26+
_bridgelessBundleURLGetter = getter;
27+
_bridgelessBundleURLSetter = setter;
28+
}
29+
30+
- (void)setBundleURL:(NSURL *)bundleURL
31+
{
32+
if (_bridge) {
33+
_bridge.bundleURL = bundleURL;
34+
return;
35+
}
36+
37+
RCTAssert(
38+
_bridgelessBundleURLSetter != nil,
39+
@"RCTBundleManager: In bridgeless mode, RCTBridgelessBundleURLSetter must not be nil.");
40+
_bridgelessBundleURLSetter(bundleURL);
41+
}
42+
43+
- (NSURL *)bundleURL
44+
{
45+
if (_bridge) {
46+
return _bridge.bundleURL;
47+
}
48+
49+
RCTAssert(
50+
_bridgelessBundleURLGetter != nil,
51+
@"RCTBundleManager: In bridgeless mode, RCTBridgelessBundleURLGetter must not be nil.");
52+
53+
return _bridgelessBundleURLGetter();
54+
}
55+
56+
@end

0 commit comments

Comments
 (0)