Skip to content

Commit 4626732

Browse files
tsapetameta-codesync[bot]
authored andcommitted
Deprecate RCTHostRuntimeDelegate and merge into RCTHostDelegate (react#54915)
Summary: Currently, there are two delegate protocols related to the `RCTHost` class that Frameworks can use: `RCTHostDelegate` and `RCTHostRuntimeDelegate`. `RCTHostDelegate` is easy to set and use as `RCTReactNativeFactory` already conforms to it. However, using the `RCTHostRuntimeDelegate` is less convenient because it can only be set via the host's `runtimeDelegate` property. This requires access to the host instance first, which in turn means having to wait for `hostDidStart` (see how we currently do this in Expo: https://github.com/expo/expo/blob/cacd14059830c00971d90d39cf8aa9e67f5e6de1/packages/expo/ios/AppDelegates/ExpoReactNativeFactory.mm#L28-L36). Additionally it's not clear that `hostDidStart` is called before `host:didInitializeRuntime`, especially since both are called from different threads. Relying on this ordering feels unsafe to me. I'm proposing to merge `RCTHostRuntimeDelegate` into `RCTHostDelegate` as they effectively serve the same purpose. As part of this change, I've deprecated `RCTHostRuntimeDelegate` and its method. It can be removed in 0.85 or 0.86, depending on which release allows breaking changes. Instead, I've added the same method (optional) to `RCTHostDelegate`. All call sites and tests have been updated accordingly. ## Changelog: [IOS] [DEPRECATED] - Deprecate `RCTHostRuntimeDelegate` and merge into `RCTHostDelegate` Pull Request resolved: react#54915 Test Plan: - Manually added `host:didInitializeRuntime:` to `RCTReactNativeFactory` and confirmed it gets called - Tested this change in Expo and our own factory - Native unit tests are passing (for both the deprecated and new method) Reviewed By: cortinico Differential Revision: D94076719 Pulled By: cipolleschi fbshipit-source-id: 946d701727eb46653ce083b66875f9b57baf5743
1 parent 8915d81 commit 4626732

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

packages/react-native/ReactCommon/react/runtime/iostests/RCTHostTests.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ - (void)testDidReceiveErrorStack
162162
}
163163

164164
- (void)testDidInitializeRuntime
165+
{
166+
auto hermesRuntime = facebook::hermes::makeHermesRuntime();
167+
facebook::jsi::Runtime *rt = hermesRuntime.get();
168+
169+
id<RCTInstanceDelegate> instanceDelegate = (id<RCTInstanceDelegate>)_subject;
170+
[instanceDelegate instance:[OCMArg any] didInitializeRuntime:*rt];
171+
172+
OCMVerify(OCMTimes(1), [_mockHostDelegate host:_subject didInitializeRuntime:*rt]);
173+
}
174+
175+
- (void)testDidInitializeRuntime_DEPRECATED
165176
{
166177
id<RCTHostRuntimeDelegate> mockRuntimeDelegate = OCMProtocolMock(@protocol(RCTHostRuntimeDelegate));
167178
_subject.runtimeDelegate = mockRuntimeDelegate;

packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,24 @@ typedef NSURL *_Nullable (^RCTHostBundleURLProvider)(void);
4949
exceptionId:(NSUInteger)exceptionId
5050
isFatal:(BOOL)isFatal
5151
extraData:(NSDictionary<NSString *, id> *)extraData __attribute__((deprecated));
52+
53+
/**
54+
Delegate method invoked after the host has finished initializing the JavaScript runtime. At this stage,
55+
bindings for Turbo Modules and the NativeComponentRegistry are already installed,
56+
but the JavaScript bundle has not yet been executed. This method is called on the JavaScript thread;
57+
accessing the runtime from any other thread is prohibited and will result in crashes.
58+
*/
59+
- (void)host:(RCTHost *)host didInitializeRuntime:(facebook::jsi::Runtime &)runtime;
60+
5261
@end
5362

54-
@protocol RCTHostRuntimeDelegate <NSObject>
63+
// `RCTHostRuntimeDelegate` has been merged into `RCTHostDelegate` in 0.84
64+
[[deprecated("Use 'RCTHostDelegate' instead")]]
65+
@protocol RCTHostRuntimeDelegate<NSObject>
5566

56-
- (void)host:(RCTHost *)host didInitializeRuntime:(facebook::jsi::Runtime &)runtime;
67+
- (void)host:(RCTHost *)host
68+
didInitializeRuntime:(facebook::jsi::Runtime &)runtime
69+
[[deprecated("Use an equivalent method from 'RCTHostDelegate' instead")]];
5770

5871
@end
5972

@@ -84,7 +97,8 @@ typedef std::shared_ptr<facebook::react::JSRuntimeFactory> (^RCTHostJSEngineProv
8497
- (instancetype)init NS_UNAVAILABLE;
8598
+ (instancetype)new NS_UNAVAILABLE;
8699

87-
@property (nonatomic, weak, nullable) id<RCTHostRuntimeDelegate> runtimeDelegate;
100+
@property (nonatomic, weak, nullable) id<RCTHostRuntimeDelegate> runtimeDelegate
101+
[[deprecated("Use 'RCTHostDelegate' instead")]];
88102

89103
@property (nonatomic, readonly) RCTSurfacePresenter *surfacePresenter;
90104

packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,14 @@ - (BOOL)instance:(RCTInstance *)instance
371371

372372
- (void)instance:(RCTInstance *)instance didInitializeRuntime:(facebook::jsi::Runtime &)runtime
373373
{
374+
if ([_hostDelegate respondsToSelector:@selector(host:didInitializeRuntime:)]) {
375+
[_hostDelegate host:self didInitializeRuntime:runtime];
376+
}
377+
// Runtime delegate is deprecated as of 0.84
378+
#pragma clang diagnostic push
379+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
374380
[self.runtimeDelegate host:self didInitializeRuntime:runtime];
381+
#pragma clang diagnostic pop
375382
}
376383

377384
- (void)loadBundleAtURL:(NSURL *)sourceURL

0 commit comments

Comments
 (0)