Skip to content

Commit

Permalink
Add functions to check whether the New Arch is enabled at runtime (#4…
Browse files Browse the repository at this point in the history
…2090)

Summary:
Pull Request resolved: #42090

This change is the last pieces of removing `RCT_NEW_ARCH_ENABLED` flag and defragmenting the build setup on iOS.

Before, 3rd party libraries had to use the `#if RCT_NEW_ARCH_ENABLED` flag to compile in and out segment of code depending on whether the new architecture was turned on or not.

After the recent changes, we can now expose the `RCTIsNewArchEnabled()` function to read whether the New Arch is enabled at runtime or not.
This will promote better code practices as we can replace ugly, compile time, `#if-#else-#endif`s with a more readable and natural regular obj-c code.
We can also use inheritance to have different implementation based on the architecture.

To use the new function, a 3rd party library have to:
1. `#import <React/RCTUtils.h>` (if they use the  `install_modules_dependencies` function we provide, they can already do it)
2. invoke `RCTIsNewArchEnabled()` which returns a BOOL.
3. implement the code accordingly, depending on the New arch state.

**Note:** we implemented also the `RCTSetNewArchEnabled` function. This is called as soon as React Native is initialized in the `RCTAppDelegate`. The method can be called only once per React Native lifecycle. Subsequent calls to that method are ignored.

## Changelog:
[iOS][Added] - Added the `RCTIsNewArchEnabled()` to check whether the New Arch is enabled at runtime.

Reviewed By: cortinico

Differential Revision: D52445107

fbshipit-source-id: 1b432832912d33c85687b4c37f9e360ce9699f59
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Jan 2, 2024
1 parent db9c9ea commit f1a7f08
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <React/RCTLog.h>
#import <React/RCTRootView.h>
#import <React/RCTSurfacePresenterBridgeAdapter.h>
#import <React/RCTUtils.h>
#import <react/renderer/runtimescheduler/RuntimeScheduler.h>
#import "RCTAppSetupUtils.h"

Expand Down Expand Up @@ -79,9 +80,10 @@ - (instancetype)init

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RCTSetNewArchEnabled([self newArchEnabled]);
BOOL enableTM = self.turboModuleEnabled;
BOOL enableBridgeless = self.bridgelessEnabled;
BOOL fabricEnabled = self.fabricEnabled;
BOOL enableBridgeless = self.bridgelessEnabled;

NSDictionary *initProps = updateInitialProps([self prepareInitialProps], fabricEnabled);

Expand Down
4 changes: 4 additions & 0 deletions packages/react-native/React/Base/RCTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

NS_ASSUME_NONNULL_BEGIN

// Whether the New Architecture is enabled or not
RCT_EXTERN BOOL RCTIsNewArchEnabled(void);
RCT_EXTERN void RCTSetNewArchEnabled(BOOL enabled);

// JSON serialization/deserialization
RCT_EXTERN NSString *__nullable RCTJSONStringify(id __nullable jsonObject, NSError **error);
RCT_EXTERN id __nullable RCTJSONParse(NSString *__nullable jsonString, NSError **error);
Expand Down
14 changes: 14 additions & 0 deletions packages/react-native/React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@
// Determines if a given image URL refers to a image in Home directory (~)
BOOL RCTIsHomeAssetURL(NSURL *__nullable imageURL);

// Whether the New Architecture is enabled or not
static BOOL _newArchEnabled = false;
BOOL RCTIsNewArchEnabled(void)
{
return _newArchEnabled;
}
void RCTSetNewArchEnabled(BOOL enabled)
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_newArchEnabled = enabled;
});
}

static NSString *__nullable _RCTJSONStringifyNoRetry(id __nullable jsonObject, NSError **error)
{
if (!jsonObject) {
Expand Down

0 comments on commit f1a7f08

Please sign in to comment.