@rnx-kit/react-native-host 0.2.7 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,6 +26,10 @@ npm add --save-dev @rnx-kit/react-native-host
26
26
 
27
27
  ### iOS/macOS
28
28
 
29
+ > To see a working example how to use this library for iOS/macOS, please refer
30
+ > to
31
+ > [react-native-test-app](https://github.com/microsoft/react-native-test-app/tree/trunk/ios/ReactTestApp).
32
+
29
33
  [Autolinking](https://github.com/react-native-community/cli/blob/10.x/docs/autolinking.md)
30
34
  should make this module available to your app project.
31
35
 
@@ -71,7 +75,7 @@ For example, if you previously had something like this:
71
75
  You should instead have:
72
76
 
73
77
  ```objc
74
- // AppDelegate.m
78
+ // AppDelegate.h
75
79
  @import ReactNativeHost;
76
80
  @import UIKit;
77
81
 
@@ -95,3 +99,114 @@ You should instead have:
95
99
 
96
100
  @end
97
101
  ```
102
+
103
+ ## API
104
+
105
+ ### ReactNativeHost
106
+
107
+ Instantiates the appropriate modules required for the setup. It handles New
108
+ Architecture if necessary.
109
+
110
+ #### `initWithConfig:`
111
+
112
+ **Swift name:** `init(_:)`
113
+
114
+ Creates an instance of `ReactNativeHost` using the designated initializer.
115
+
116
+ Objective-C:
117
+
118
+ ```objc
119
+ ReactNativeHost *host = [[ReactNativeHost alloc] initWithConfig:self];
120
+ ```
121
+
122
+ Swift:
123
+
124
+ ```swift
125
+ let host = ReactNativeHost(config: self)
126
+ ```
127
+
128
+ #### `shutdown`
129
+
130
+ Shuts down the React Native instance
131
+
132
+ #### `usingModule:block:`
133
+
134
+ **Swift name:** `using(module:block:)`
135
+
136
+ Retrieves or initializes a desired native module. Parameters:
137
+
138
+ - `moduleClass` - class of the native module to initialize or retrieve
139
+ - `block` - block that gets called when the native module is retrieved
140
+
141
+ Objective-C:
142
+
143
+ ```objc
144
+ [host usingModule:[MyNativeModuleClass class] block:^(id<RCTBridgeModule> module) {
145
+ if (![module isKindOfClass:[MyNativeModuleClass class]]) {
146
+ return;
147
+ }
148
+ MyNativeModuleClass *myNativeModule = (MyNativeModuleClass *)module;
149
+ // Use the native module here
150
+ }];
151
+ ```
152
+
153
+ Swift:
154
+
155
+ ```swift
156
+ host.using(module: MyNativeModuleClass.self) {
157
+ guard let myNativeModule = module as? MyNativeModuleClass else {
158
+ return
159
+ }
160
+ // Use the native module here
161
+ }
162
+ ```
163
+
164
+ #### `hostFromRootView:`
165
+
166
+ **Swift name:** `host(from:)`
167
+
168
+ Retrieves the `ReactNativeHost` instance that view belongs to.
169
+
170
+ #### `viewWithModuleName:initialProperties:`
171
+
172
+ **Swift name:** `view(moduleName:initialProperties:)`
173
+
174
+ Creates a React root view with the specified module and initial properties.
175
+ Parameters:
176
+
177
+ - `moduleName` - name of the module to create root view of
178
+ - `initialProperties` - properties passed to the module
179
+
180
+ Objective-C:
181
+
182
+ ```objc
183
+ ReactNativeHost *host = [[ReactNativeHost alloc] initWithConfig:self];
184
+ UIView *rootView = [host viewWithModuleName:moduleName
185
+ initialProperties:initialProperties];
186
+ ```
187
+
188
+ Swift:
189
+
190
+ ```swift
191
+ let view = host.view(
192
+ moduleName: moduleName,
193
+ initialProperties: initialProperties
194
+ )
195
+ ```
196
+
197
+ ### RNXConfig
198
+
199
+ `RNXHostConfig` is a superset of `RCTBridgeDelegate` and it's backwards
200
+ compatible.
201
+
202
+ #### `isDevLoadingViewEnabled`
203
+
204
+ Returns whether the loading view should be visible while loading JavaScript
205
+
206
+ #### `shouldReleaseBridgeWhenBackgrounded`
207
+
208
+ Returns whether the bridge should be released when the app is in the background
209
+
210
+ #### `onFatalError`
211
+
212
+ Handles a fatal error
@@ -17,7 +17,7 @@ if new_arch_enabled
17
17
  end
18
18
 
19
19
  Pod::Spec.new do |s|
20
- s.name = 'ReactNativeHost'
20
+ s.name = File.basename(__FILE__, '.podspec')
21
21
  s.version = version
22
22
  s.author = { package['author']['name'] => package['author']['email'] }
23
23
  s.license = package['license']
@@ -38,6 +38,7 @@ Pod::Spec.new do |s|
38
38
  if new_arch_enabled
39
39
  s.dependency 'React-RCTAppDelegate'
40
40
  s.dependency 'React-RCTFabric'
41
+ s.dependency 'Yoga'
41
42
  end
42
43
 
43
44
  s.pod_target_xcconfig = {
@@ -50,6 +51,7 @@ Pod::Spec.new do |s|
50
51
  '$(PODS_ROOT)/RCT-Folly',
51
52
  '$(PODS_ROOT)/DoubleConversion',
52
53
  '$(PODS_ROOT)/Headers/Private/React-Core',
54
+ '$(PODS_ROOT)/Headers/Private/Yoga',
53
55
  ],
54
56
  }
55
57
 
@@ -24,15 +24,6 @@ NS_ASSUME_NONNULL_BEGIN
24
24
  message:(NSString *)message
25
25
  __attribute__((__swift_name__("log(_:source:filename:line:message:)")));
26
26
 
27
- // Called when the bridge has been instantiated.
28
- - (void)onBridgeInstantiated:(RCTBridge *)bridge;
29
-
30
- // Called when the bridge is about to be shut down.
31
- - (void)onBridgeWillShutDown:(RCTBridge *)bridge;
32
-
33
- // Called when the bridge has been shut down.
34
- - (void)onBridgeDidShutDown;
35
-
36
27
  /// Handles a fatal error.
37
28
  - (void)onFatalError:(NSError *)error;
38
29
 
@@ -21,13 +21,17 @@
21
21
  // 0.72 bits. AFAICT, `RCTLegacyInteropComponents.h` is a new addition in 0.72
22
22
  // in both react-native and react-native-macos.
23
23
  #if __has_include(<React-RCTAppDelegate/RCTLegacyInteropComponents.h>)
24
+ #import <React-RCTAppDelegate/RCTLegacyInteropComponents.h>
25
+ #import <React/RCTLegacyViewManagerInteropComponentView.h>
24
26
  #import <react/renderer/runtimescheduler/RuntimeScheduler.h>
25
27
  #import <react/renderer/runtimescheduler/RuntimeSchedulerCallInvoker.h>
26
28
  #if __has_include(<React/RCTRuntimeExecutorFromBridge.h>)
27
29
  #import <React/RCTRuntimeExecutorFromBridge.h>
28
30
  #endif // __has_include(<React/RCTRuntimeExecutorFromBridge.h>)
31
+ #define SUPPORTS_LEGACY_COMPONENTS 1
29
32
  #define USE_RUNTIME_SCHEDULER 1
30
33
  #else
34
+ #define SUPPORTS_LEGACY_COMPONENTS 0
31
35
  #define USE_RUNTIME_SCHEDULER 0
32
36
  #endif // __has_include(<React-RCTAppDelegate/RCTLegacyInteropComponents.h>)
33
37
 
@@ -48,31 +52,19 @@
48
52
  (RCTBridge *)bridge
49
53
  {
50
54
  #if USE_TURBOMODULE
55
+ [self registerLegacyViewManagers];
51
56
  // jsExecutorFactoryForBridge: (USE_TURBOMODULE=1)
52
- #if USE_RUNTIME_SCHEDULER
53
- _runtimeScheduler =
54
- std::make_shared<facebook::react::RuntimeScheduler>(RCTRuntimeExecutorFromBridge(bridge));
55
- auto callInvoker =
56
- std::make_shared<facebook::react::RuntimeSchedulerCallInvoker>(_runtimeScheduler);
57
- _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
58
- delegate:self
59
- jsInvoker:callInvoker];
60
- return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager, _runtimeScheduler);
61
- #else
62
- _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
63
- delegate:self
64
- jsInvoker:bridge.jsCallInvoker];
65
- return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager);
66
- #endif // USE_RUNTIME_SCHEDULER
57
+ return [self initJsExecutorFactoryWithBridge:bridge];
67
58
  #else
68
59
  // jsExecutorFactoryForBridge: (USE_TURBOMODULE=0)
69
60
  return nullptr;
70
61
  #endif // USE_TURBOMODULE
71
62
  }
72
63
 
73
- // MARK: - RCTTurboModuleManagerDelegate details
74
64
  #if USE_TURBOMODULE
75
65
 
66
+ // MARK: - RCTTurboModuleManagerDelegate details
67
+
76
68
  - (Class)getModuleClassFromName:(const char *)name
77
69
  {
78
70
  return RCTCoreModulesClassProvider(name);
@@ -90,6 +82,36 @@
90
82
  return RCTAppSetupDefaultModuleFromClass(moduleClass);
91
83
  }
92
84
 
85
+ // MARK: - Private
86
+
87
+ - (std::unique_ptr<facebook::react::JSExecutorFactory>)initJsExecutorFactoryWithBridge:(RCTBridge *)bridge
88
+ {
89
+ #if USE_RUNTIME_SCHEDULER
90
+ _runtimeScheduler =
91
+ std::make_shared<facebook::react::RuntimeScheduler>(RCTRuntimeExecutorFromBridge(bridge));
92
+ auto callInvoker =
93
+ std::make_shared<facebook::react::RuntimeSchedulerCallInvoker>(_runtimeScheduler);
94
+ _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
95
+ delegate:self
96
+ jsInvoker:callInvoker];
97
+ return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager, _runtimeScheduler);
98
+ #else
99
+ _turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
100
+ delegate:self
101
+ jsInvoker:bridge.jsCallInvoker];
102
+ return RCTAppSetupDefaultJsExecutorFactory(bridge, _turboModuleManager);
103
+ #endif // USE_RUNTIME_SCHEDULER
104
+ }
105
+
106
+ - (void)registerLegacyViewManagers
107
+ {
108
+ #if SUPPORTS_LEGACY_COMPONENTS
109
+ for (NSString *legacyComponent in [RCTLegacyInteropComponents legacyInteropComponents]) {
110
+ [RCTLegacyViewManagerInteropComponentView supportLegacyViewManagerWithName:legacyComponent];
111
+ }
112
+ #endif // SUPPORTS_LEGACY_COMPONENTS
113
+ }
114
+
93
115
  #endif // USE_TURBOMODULE
94
116
 
95
117
  @end
@@ -1,8 +1,13 @@
1
1
  #import "ReactNativeHost.h"
2
2
 
3
3
  #ifdef USE_FABRIC
4
+ #if __has_include(<React/RCTFabricSurfaceHostingProxyRootView.h>)
4
5
  #import <React/RCTFabricSurfaceHostingProxyRootView.h>
5
6
  #else
7
+ #import <React/RCTFabricSurface.h>
8
+ #import <React/RCTSurfaceHostingProxyRootView.h>
9
+ #endif // __has_include(<React/RCTFabricSurfaceHostingProxyRootView.h>)
10
+ #else
6
11
  #import <React/RCTRootView.h>
7
12
  #endif // USE_FABRIC
8
13
 
@@ -31,9 +36,16 @@
31
36
  initialProperties:(NSDictionary *)initialProperties;
32
37
  {
33
38
  #ifdef USE_FABRIC
39
+ #if __has_include(<React/RCTFabricSurfaceHostingProxyRootView.h>)
34
40
  return [[RCTFabricSurfaceHostingProxyRootView alloc] initWithBridge:self.bridge
35
41
  moduleName:moduleName
36
42
  initialProperties:initialProperties];
43
+ #else
44
+ RCTFabricSurface *surface = [[RCTFabricSurface alloc] initWithBridge:self.bridge
45
+ moduleName:moduleName
46
+ initialProperties:initialProperties];
47
+ return [[RCTSurfaceHostingProxyRootView alloc] initWithSurface:surface];
48
+ #endif // __has_include(<React/RCTFabricSurfaceHostingProxyRootView.h>)
37
49
  #else
38
50
  return [[RCTRootView alloc] initWithBridge:self.bridge
39
51
  moduleName:moduleName
@@ -17,6 +17,9 @@ NS_ASSUME_NONNULL_BEGIN
17
17
  /// Hosts a React Native instance.
18
18
  @interface ReactNativeHost : NSObject <RCTBridgeDelegate>
19
19
 
20
+ /// Returns the current `RCTBridge` instance.
21
+ ///
22
+ /// - Note: This is not forwards compatible and will be removed in the future.
20
23
  @property (nonatomic, readonly, nullable) RCTBridge *bridge;
21
24
 
22
25
  - (instancetype)init NS_UNAVAILABLE;
@@ -80,9 +80,6 @@
80
80
  _bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:nil];
81
81
  _surfacePresenterBridgeAdapter = RNXInstallSurfacePresenterBridgeAdapter(_bridge);
82
82
  [_hostReleaser setBridge:_bridge];
83
- if ([_config respondsToSelector:@selector(onBridgeInstantiated:)]) {
84
- [_config onBridgeInstantiated:_bridge];
85
- }
86
83
  }
87
84
 
88
85
  return _bridge;
@@ -93,19 +90,11 @@
93
90
 
94
91
  - (void)shutdown
95
92
  {
96
- if ([_config respondsToSelector:@selector(onBridgeWillShutDown:)]) {
97
- [_config onBridgeWillShutDown:_bridge];
98
- }
99
-
100
93
  [_isShuttingDown lock];
101
94
 
102
95
  @try {
103
96
  [_bridge invalidate];
104
97
  _bridge = nil;
105
-
106
- if ([_config respondsToSelector:@selector(onBridgeDidShutDown)]) {
107
- [_config onBridgeDidShutDown];
108
- }
109
98
  } @finally {
110
99
  [_isShuttingDown unlock];
111
100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rnx-kit/react-native-host",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "Simplify React Native initialization",
5
5
  "homepage": "https://github.com/microsoft/rnx-kit/tree/main/packages/react-native-host#readme",
6
6
  "license": "MIT",
@@ -19,10 +19,15 @@
19
19
  "directory": "packages/react-native-host"
20
20
  },
21
21
  "scripts": {
22
+ "format": "rnx-kit-scripts format",
22
23
  "format:c": "clang-format -i $(git ls-files '*.c' '*.cpp' '*.h' '*.m' '*.mm')",
23
- "lint:kt": "ktlint --relative --verbose 'android/src/**/*.kt'"
24
+ "lint:kt": "ktlint --relative 'android/src/**/*.kt'"
24
25
  },
25
26
  "peerDependencies": {
26
27
  "react-native": ">=0.64"
28
+ },
29
+ "devDependencies": {
30
+ "@rnx-kit/scripts": "*",
31
+ "prettier": "^3.0.0"
27
32
  }
28
33
  }