feat(react-native-host): extend RNXHostConfig with optional host hooks (1/3)#4144
feat(react-native-host): extend RNXHostConfig with optional host hooks (1/3)#4144Saadnajmi wants to merge 1 commit into
Conversation
0f20017 to
a1128b2
Compare
Adds three optional lifecycle hooks to RNXHostConfig. All three are opt-in; consumers that don't implement them see no behavior change. * host:didLoadInstanceWithError: and hostWillUnloadInstance: ReactNativeHost subscribes to RCTJavaScriptDidLoad, RCTJavaScriptDidFailToLoad, and RCTBridgeWillBeInvalidated notifications and forwards to the config when the corresponding selectors are implemented. dealloc removes observers. * host:didInitializeRuntime: (Objective-C++ only) fires inside the bridgeless runtime-init lambda, after host bindings install but before the user JS bundle loads. Useful for loading pre-user JS (e.g. platform bundles) via runtime.evaluateJavaScript before the app bundle runs. Wired via an internal _RNXForwardingRCTHostDelegate passed as RCTHost's hostDelegate (was nil); retained as an ivar because RCTHost stores host delegates weakly.
a1128b2 to
1685ee4
Compare
|
|
||
| /// Called after the JS instance has finished loading. ``error`` is ``nil`` | ||
| /// on success. | ||
| - (void)host:(ReactNativeHost *)host didLoadInstanceWithError:(nullable NSError *)error |
There was a problem hiding this comment.
I'm a little iffy with the optional error parameter.. the signature makes it feel like there is always an error despite the comment. Alternative is to also expose failedtoLoadInstanceWithError. Thoughts?
There was a problem hiding this comment.
Shouldn't this be - (void)didLoadInstanceWithHost:(ReactNativeHost *)host error: (nullable NSError **)error, where the user passes a ref to an NSError if they're interested.
| @end | ||
| #endif // USE_CODEGEN_PROVIDER | ||
|
|
||
| #if USE_BRIDGELESS |
There was a problem hiding this comment.
Should this block be in a private header instead?
| if ([config respondsToSelector:@selector(host:didLoadInstanceWithError:)]) { | ||
| [[NSNotificationCenter defaultCenter] | ||
| addObserver:self | ||
| selector:@selector(_rnxInstanceDidLoad:) | ||
| name:RCTJavaScriptDidLoadNotification | ||
| object:nil]; | ||
| [[NSNotificationCenter defaultCenter] | ||
| addObserver:self | ||
| selector:@selector(_rnxInstanceDidFailToLoad:) | ||
| name:RCTJavaScriptDidFailToLoadNotification | ||
| object:nil]; | ||
| } | ||
| if ([config respondsToSelector:@selector(hostWillUnloadInstance:)]) { | ||
| [[NSNotificationCenter defaultCenter] | ||
| addObserver:self | ||
| selector:@selector(_rnxInstanceWillUnload:) | ||
| name:RCTBridgeWillBeInvalidatedNotification | ||
| object:nil]; | ||
| } |
There was a problem hiding this comment.
Shouldn't all of this be contained within _RNXForwardingRCTHostDelegate?
| /// Called when the instance is about to be unloaded. | ||
| - (void)hostWillUnloadInstance:(ReactNativeHost *)host; | ||
|
|
||
| #ifdef __cplusplus |
There was a problem hiding this comment.
Is this allowed? Won't we end up with two definitions of this protocol?
Description
Adds three optional lifecycle hooks to
RNXHostConfig. All three are opt-in viarespondsToSelector:— consumers that don't implement them see no behavior change.host:didLoadInstanceWithError:andhostWillUnloadInstance:ReactNativeHostsubscribes toRCTJavaScriptDidLoadNotification,RCTJavaScriptDidFailToLoadNotification, andRCTBridgeWillBeInvalidatedNotificationwhen the config implements thecorresponding selectors, and forwards.
-deallocremoves the observers.Works in both bridge and bridgeless modes — the same notifications fire in both.
host:didInitializeRuntime:(Objective-C++ only)Fires inside the bridgeless
RCTHostruntime-initialization lambda, after host bindings install (TurboModules, native logger, native component registry) but before the user JSbundle loads. Useful for evaluating pre-user JS on the runtime — e.g., bundles that must be available before app code runs via
runtime.evaluateJavaScript.Bridgeless mode only. Wired via an internal
_RNXForwardingRCTHostDelegatepassed asRCTHost'shostDelegate(wasnil). Retained as an ivar becauseRCTHoststores host delegatesweakly. C++-typed (
jsi::Runtime &), so only visible to Objective-C++ consumers.Test plan
Tested internally