Skip to content

Commit

Permalink
Introduce RCTBundleManager
Browse files Browse the repository at this point in the history
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
RSNara authored and facebook-github-bot committed May 5, 2021
1 parent 7db89f9 commit 4a1bafe
Showing 2 changed files with 80 additions and 0 deletions.
24 changes: 24 additions & 0 deletions React/Base/RCTBridgeModule.h
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
@protocol RCTBridgeMethod;
@class RCTModuleRegistry;
@class RCTViewRegistry;
@class RCTBundleManager;

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

/**
* A reference to the RCTBundleManager. Useful for modules that need to read
* or write to the app's bundle URL.
*
* To implement this in your module, just add `@synthesize bundleManager =
* _bundleManager;`. If using Swift, add `@objc var bundleManager:
* RCTBundleManager!` to your module.
*/
@property (nonatomic, weak, readwrite) RCTBundleManager *bundleManager;

/**
* A reference to the RCTBridge. Useful for modules that require access
* to bridge features, such as sending events or making JS calls. This
@@ -406,6 +417,19 @@ RCT_EXTERN_C_END
- (id)moduleForName:(const char *)moduleName lazilyLoadIfNecessary:(BOOL)lazilyLoad;
@end

typedef void (^RCTBridgelessBundleURLSetter)(NSURL *bundleURL);
typedef NSURL * (^RCTBridgelessBundleURLGetter)();

/**
* A class that allows NativeModules/TurboModules to read/write the bundleURL, with or without the bridge.
*/
@interface RCTBundleManager : NSObject
- (void)setBridge:(RCTBridge *)bridge;
- (void)setBridgelessBundleURLGetter:(RCTBridgelessBundleURLGetter)getter
andSetter:(RCTBridgelessBundleURLSetter)setter;
@property NSURL *bundleURL;
@end

typedef UIView * (^RCTBridgelessComponentViewProvider)(NSNumber *);

/**
56 changes: 56 additions & 0 deletions React/Base/RCTBundleManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "RCTAssert.h"
#import "RCTBridge+Private.h"
#import "RCTBridge.h"
#import "RCTBridgeModule.h"

@implementation RCTBundleManager {
__weak RCTBridge *_bridge;
RCTBridgelessBundleURLGetter _bridgelessBundleURLGetter;
RCTBridgelessBundleURLSetter _bridgelessBundleURLSetter;
}

- (void)setBridge:(RCTBridge *)bridge
{
_bridge = bridge;
}

- (void)setBridgelessBundleURLGetter:(RCTBridgelessBundleURLGetter)getter andSetter:(RCTBridgelessBundleURLSetter)setter
{
_bridgelessBundleURLGetter = getter;
_bridgelessBundleURLSetter = setter;
}

- (void)setBundleURL:(NSURL *)bundleURL
{
if (_bridge) {
_bridge.bundleURL = bundleURL;
return;
}

RCTAssert(
_bridgelessBundleURLSetter != nil,
@"RCTBundleManager: In bridgeless mode, RCTBridgelessBundleURLSetter must not be nil.");
_bridgelessBundleURLSetter(bundleURL);
}

- (NSURL *)bundleURL
{
if (_bridge) {
return _bridge.bundleURL;
}

RCTAssert(
_bridgelessBundleURLGetter != nil,
@"RCTBundleManager: In bridgeless mode, RCTBridgelessBundleURLGetter must not be nil.");

return _bridgelessBundleURLGetter();
}

@end

0 comments on commit 4a1bafe

Please sign in to comment.